mirror of
https://github.com/rikkaneko/paste.git
synced 2025-08-05 21:50:21 +01:00
Auto remove expire key if paste object expired
Remove mime-types library
This commit is contained in:
parent
825f59f00f
commit
78e6707b4b
3 changed files with 382 additions and 376 deletions
|
@ -4,7 +4,6 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"aws4fetch": "^1.0.13",
|
"aws4fetch": "^1.0.13",
|
||||||
"nanoid": "^3.3.4",
|
"nanoid": "^3.3.4",
|
||||||
"mime-types": "^2.1.35",
|
|
||||||
"js-sha256": "^0.9.0"
|
"js-sha256": "^0.9.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
35
src/index.ts
35
src/index.ts
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
import {AwsClient} from "aws4fetch";
|
import {AwsClient} from "aws4fetch";
|
||||||
import {customAlphabet} from "nanoid";
|
import {customAlphabet} from "nanoid";
|
||||||
import {contentType} from "mime-types";
|
|
||||||
import {sha256} from "js-sha256";
|
import {sha256} from "js-sha256";
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
|
@ -34,7 +33,7 @@ export interface Env {
|
||||||
}
|
}
|
||||||
|
|
||||||
const API_SPEC_TEXT =
|
const API_SPEC_TEXT =
|
||||||
`Paste service https://${SERVICE_URL}
|
`Paste service https://${SERVICE_URL}
|
||||||
|
|
||||||
[API Specification]
|
[API Specification]
|
||||||
GET / Fetch the Web frontpage for uploading text/file [x]
|
GET / Fetch the Web frontpage for uploading text/file [x]
|
||||||
|
@ -149,7 +148,6 @@ export default {
|
||||||
if (data instanceof File) {
|
if (data instanceof File) {
|
||||||
if (data.name) {
|
if (data.name) {
|
||||||
title = data.name;
|
title = data.name;
|
||||||
mime_type = contentType(title) || undefined;
|
|
||||||
}
|
}
|
||||||
buffer = await data.arrayBuffer();
|
buffer = await data.arrayBuffer();
|
||||||
// Text
|
// Text
|
||||||
|
@ -173,7 +171,6 @@ export default {
|
||||||
} else {
|
} else {
|
||||||
if (headers.has("x-title")) {
|
if (headers.has("x-title")) {
|
||||||
title = headers.get("x-title") || "";
|
title = headers.get("x-title") || "";
|
||||||
mime_type = contentType(title) || undefined;
|
|
||||||
}
|
}
|
||||||
mime_type = headers.get("content-type") || mime_type;
|
mime_type = headers.get("content-type") || mime_type;
|
||||||
password = headers.get("x-pass") || undefined;
|
password = headers.get("x-pass") || undefined;
|
||||||
|
@ -223,7 +220,8 @@ export default {
|
||||||
mime_type
|
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));
|
return new Response(get_paste_info(uuid, descriptor));
|
||||||
} else {
|
} else {
|
||||||
return new Response("Unable to upload the paste.\n", {
|
return new Response("Unable to upload the paste.\n", {
|
||||||
|
@ -318,7 +316,7 @@ export default {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
descriptor.read_count_remain--;
|
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
|
// Enable CF cache for authorized request
|
||||||
|
@ -332,8 +330,17 @@ export default {
|
||||||
|
|
||||||
res = new Response(origin.body, origin);
|
res = new Response(origin.body, origin);
|
||||||
|
|
||||||
if (!res.ok) {
|
if (res.status == 404) {
|
||||||
// UUID exists in index but not found in remote object storage service
|
// 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", {
|
return new Response("Internal server error.\n", {
|
||||||
status: 500
|
status: 500
|
||||||
});
|
});
|
||||||
|
@ -348,10 +355,10 @@ export default {
|
||||||
|
|
||||||
res.headers.set("cache-control", "public, max-age=18000");
|
res.headers.set("cache-control", "public, max-age=18000");
|
||||||
// Alter content type to text/plain
|
// Alter content type to text/plain
|
||||||
if (option === "raw") {
|
if (option === "raw" || descriptor.mime_type === undefined) {
|
||||||
res.headers.set("content-type", "text/plain; charset=UTF-8;");
|
res.headers.delete("content-type");
|
||||||
} else {
|
} 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",
|
res.headers.set("content-disposition",
|
||||||
|
@ -425,10 +432,10 @@ export default {
|
||||||
|
|
||||||
function get_paste_info(uuid: string, descriptor: PasteIndexEntry): string {
|
function get_paste_info(uuid: string, descriptor: PasteIndexEntry): string {
|
||||||
const date = new Date(descriptor.last_modified)
|
const date = new Date(descriptor.last_modified)
|
||||||
return `link: https://${SERVICE_URL}/${uuid}
|
return`id: ${uuid}
|
||||||
id: ${uuid}
|
link: https://${SERVICE_URL}/${uuid}
|
||||||
title: ${descriptor.title || "<empty>"}
|
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)})
|
size: ${descriptor.size} bytes (${to_human_readable_size(descriptor.size)})
|
||||||
password: ${(!!descriptor.password)}
|
password: ${(!!descriptor.password)}
|
||||||
editable: ${descriptor.editable? descriptor.editable: true}
|
editable: ${descriptor.editable? descriptor.editable: true}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue