Skip to content

Commit

Permalink
Merge pull request #34 from dandanthedev/dev
Browse files Browse the repository at this point in the history
Add downloadAs type
  • Loading branch information
dandanthedev authored Aug 11, 2024
2 parents 1ab1600 + 0d58fb5 commit acafe28
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/api/generateToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ export default async function handle(
const file = params.get("file");
const type = params.get("type");
const expiresIn = params.get("expiresIn");
const downloadAs = params.get("downloadAs");

if (!buckets.includes(bucket)) return resp(res, 400, "Invalid bucket");

if (type && ["upload", "download", "delete", "rename"].indexOf(type) === -1)
return resp(res, 400, "Invalid type");

const token = await generateSignedToken(bucket, file, type, expiresIn);
if(downloadAs && type && type !== "download") return resp(res, 400, "Downloadas can only be used on a download type")

const token = await generateSignedToken(bucket, file, type, expiresIn, downloadAs);
if (!token) return resp(res, 400, "Error generating token");

resp(res, 200, token);
Expand Down
28 changes: 8 additions & 20 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ async function canAccessFile(
if (timeLeft) res.setHeader("Token-ExpiresIn", timeLeft ?? "never");
if (decoded?.file) res.setHeader("Token-File", decoded.file ?? "any");
if (decoded?.type) res.setHeader("Token-Type", decoded.type ?? "any");
if (decoded?.downloadAs) {
res.setHeader("Token-DownloadAs", decoded.downloadAs ?? "any");
if (type === "download")
res.setHeader(
"ResponseContentDisposition",
`attachment; filename="${decoded.downloadAs}"`
);
}
}

return true;
Expand Down Expand Up @@ -188,28 +196,8 @@ export const requestListener = async function (
else if (req.method === "HEAD") type = "head";
else return resp(res, 400, "Invalid method");

if (
((await canAccessFile(bucket, null, file, "download", res)) === true &&
type === "download") ||
type === "head"
) {
if (req.method === "HEAD") {
const stats = getFileStats(bucket, file);
if (!stats) return resp(res, 404);
res.setHeader("Content-Length", stats.size);
return resp(res, 204);
} else if (req.method === "GET") {
//if accessible without authentication
const foundFile = streamFile(bucket, file);
if (!foundFile) return resp(res, 404);
return resp(res, 200, foundFile, "file");
}
}

const key = params.get("key");

if (!key) return resp(res, 401, "Key required");

const canAccess = await canAccessFile(bucket, key, file, type, res);
if (canAccess !== true) return resp(res, 401, canAccess);

Expand Down
4 changes: 3 additions & 1 deletion src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export async function generateSignedToken(
bucket: string | null,
file: string | null,
type: string | null,
expiry?: string | number | null
expiry?: string | number | null,
downloadAs?: string | null
) {
try {
const token = new jose.SignJWT({
bucket,
file,
type,
downloadAs,
});
token.setExpirationTime(expiry ?? "60s");
token.setProtectedHeader({
Expand Down

0 comments on commit acafe28

Please sign in to comment.