Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add downloadAs type #34

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading