Minor improvement

Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
Joe Ma 2022-06-03 13:30:57 +08:00
parent d79281a6c6
commit 7c4aacd952
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F
3 changed files with 19 additions and 16 deletions

View file

@ -1,6 +1,6 @@
{
"name": "paste",
"version": "1.0.0",
"version": "1.1",
"dependencies": {
"aws4fetch": "^1.0.13",
"nanoid": "^3.3.4",

View file

@ -26,7 +26,7 @@
<body>
<h2>Paste Service</h2>
<h4>Upload file</h4>
<form action="https://paste.nekoul.com" method="POST" enctype=multipart/form-data>
<form action="https://pb.nekoul.com" method="POST" enctype=multipart/form-data>
<div>
<input id="upload_file" type="file" name="u">
<div><input type="submit" value="Send"> (<span id="file_size">0 byte</span>)</div>
@ -47,7 +47,7 @@
</script>
<h4>Upload text</h4>
<form action="https://paste.nekoul.com" method="POST" enctype=multipart/form-data>
<form action="https://pb.nekoul.com" method="POST" enctype=multipart/form-data>
<div>
<textarea id="text_input" style="width: 30%; max-width: 100%; " rows ="5" cols="50"
name="u" placeholder="Paste your text here..."></textarea>
@ -63,7 +63,7 @@
document.getElementById("text_input").addEventListener("input", update_textarea, false);
</script>
<br>
<a href="https://nekoul.com">[Homepage]</a><a href="https://paste.nekoul.com/api">[API]</a>
<a href="https://nekoul.com">[Homepage]</a><a href="https://pb.nekoul.com/api">[API]</a>
<p>&copy; 2022 rikkaneko</p>
</body>
</html>

View file

@ -18,10 +18,10 @@
import {AwsClient} from "aws4fetch";
import {customAlphabet} from "nanoid";
import {contentType} from "mime-types"
import {contentType} from "mime-types";
// Constants
const SERVICE_URL = "paste.nekoul.com"
const SERVICE_URL = "pb.nekoul.com"
const PASTE_INDEX_HTML_URL = "https://raw.githubusercontent.com/rikkaneko/paste/main/paste.html"
const UUID_LENGTH = 4
@ -71,8 +71,9 @@ export default {
ctx: ExecutionContext
): Promise<Response> {
const {url, method, headers} = request;
const {hostname, pathname, searchParams} = new URL(url);
const path = pathname.replace(/\/+$/, "") || "/"
const {pathname} = new URL(url);
const path = pathname.replace(/\/+$/, "") || "/";
let cache = caches.default;
const s3 = new AwsClient({
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY
@ -168,9 +169,11 @@ export default {
last_modified: Date.now()
};
const counter = await env.PASTE_INDEX.get("__count__") || "0";
await env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor));
await env.PASTE_INDEX.put("__count__", (Number(counter) + 1).toString());
const p1 = env.PASTE_INDEX.get("__count__").then(counter => {
env.PASTE_INDEX.put("__count__", (Number(counter ?? "0") + 1).toString());
});
const p2 = env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor));
await Promise.all([p1, p2]);
return new Response(get_paste_info(uuid, descriptor));
} else {
return new Response("Unable to upload the paste.\n", {
@ -224,7 +227,6 @@ export default {
// Fetch the paste by uuid
case "GET": {
// Enable CF cache for authorized request
let cache = caches.default;
// Match in existing cache
let res = await cache.match(request.url);
if (res === undefined) {
@ -263,7 +265,9 @@ export default {
}
// Cache hit
return res;
let { readable, writable } = new TransformStream();
res.body!.pipeTo(writable);
return new Response(readable, res);
}
// Delete paste by uuid
@ -284,7 +288,6 @@ export default {
await env.PASTE_INDEX.put("__count__", (Number(counter) - 1).toString());
// Invalidate CF cache
let cache = caches.default;
await cache.delete(request.url);
return new Response("OK\n");
} else {
@ -305,13 +308,13 @@ export default {
function get_paste_info(uuid: string, descriptor: PasteIndexEntry): string {
const date = new Date(descriptor.last_modified)
return `https://${SERVICE_URL}/${uuid}
return `link: https://${SERVICE_URL}/${uuid}
id: ${uuid}
title: ${descriptor.title || "<empty>"}
mime-type: ${descriptor.mime_type ?? "application/octet-stream"}
password: ${(!!descriptor.password)}
editable: ${descriptor.editable? descriptor.editable: true}
last modified at ${date.toISOString()}
created at ${date.toISOString()}
`
}