Auto remove expire key if paste object expired

Remove mime-types library
This commit is contained in:
Joe Ma 2022-08-19 00:59:28 +08:00
parent 825f59f00f
commit 78e6707b4b
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F
3 changed files with 382 additions and 376 deletions

View file

@ -4,7 +4,6 @@
"dependencies": {
"aws4fetch": "^1.0.13",
"nanoid": "^3.3.4",
"mime-types": "^2.1.35",
"js-sha256": "^0.9.0"
},
"devDependencies": {

View file

@ -18,7 +18,6 @@
import {AwsClient} from "aws4fetch";
import {customAlphabet} from "nanoid";
import {contentType} from "mime-types";
import {sha256} from "js-sha256";
// Constants
@ -34,7 +33,7 @@ export interface Env {
}
const API_SPEC_TEXT =
`Paste service https://${SERVICE_URL}
`Paste service https://${SERVICE_URL}
[API Specification]
GET / Fetch the Web frontpage for uploading text/file [x]
@ -149,7 +148,6 @@ export default {
if (data instanceof File) {
if (data.name) {
title = data.name;
mime_type = contentType(title) || undefined;
}
buffer = await data.arrayBuffer();
// Text
@ -173,7 +171,6 @@ export default {
} else {
if (headers.has("x-title")) {
title = headers.get("x-title") || "";
mime_type = contentType(title) || undefined;
}
mime_type = headers.get("content-type") || mime_type;
password = headers.get("x-pass") || undefined;
@ -223,7 +220,8 @@ export default {
mime_type
};
ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor)));
// Key will be expired after 28 day if unmodified
ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor), {expirationTtl: 100800}));
return new Response(get_paste_info(uuid, descriptor));
} else {
return new Response("Unable to upload the paste.\n", {
@ -318,7 +316,7 @@ export default {
});
}
descriptor.read_count_remain--;
ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor)));
ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor), {expirationTtl: 100800}));
}
// Enable CF cache for authorized request
@ -332,8 +330,17 @@ export default {
res = new Response(origin.body, origin);
if (!res.ok) {
// UUID exists in index but not found in remote object storage service
if (res.status == 404) {
// UUID exists in index but not found in remote object storage service, probably expired
// Remove expired key
ctx.waitUntil(env.PASTE_INDEX.delete(uuid));
// Invalidate CF cache
ctx.waitUntil(cache.delete(url));
return new Response("Paste expired.\n", {
status: 410
});
} else if (!res.ok) {
// Other error
return new Response("Internal server error.\n", {
status: 500
});
@ -348,10 +355,10 @@ export default {
res.headers.set("cache-control", "public, max-age=18000");
// Alter content type to text/plain
if (option === "raw") {
res.headers.set("content-type", "text/plain; charset=UTF-8;");
if (option === "raw" || descriptor.mime_type === undefined) {
res.headers.delete("content-type");
} else {
res.headers.set("content-type", descriptor.mime_type ?? "application/octet-stream");
res.headers.set("content-type", descriptor.mime_type);
}
res.headers.set("content-disposition",
@ -425,10 +432,10 @@ export default {
function get_paste_info(uuid: string, descriptor: PasteIndexEntry): string {
const date = new Date(descriptor.last_modified)
return `link: https://${SERVICE_URL}/${uuid}
id: ${uuid}
return`id: ${uuid}
link: https://${SERVICE_URL}/${uuid}
title: ${descriptor.title || "<empty>"}
mime-type: ${descriptor.mime_type ?? "application/octet-stream"}
mime-type: ${descriptor.mime_type ?? "-"}
size: ${descriptor.size} bytes (${to_human_readable_size(descriptor.size)})
password: ${(!!descriptor.password)}
editable: ${descriptor.editable? descriptor.editable: true}