mirror of
https://github.com/rikkaneko/paste.git
synced 2025-08-03 20:50:08 +01:00
Minor improvement
Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
parent
d79281a6c6
commit
7c4aacd952
3 changed files with 19 additions and 16 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "paste",
|
"name": "paste",
|
||||||
"version": "1.0.0",
|
"version": "1.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"aws4fetch": "^1.0.13",
|
"aws4fetch": "^1.0.13",
|
||||||
"nanoid": "^3.3.4",
|
"nanoid": "^3.3.4",
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
<body>
|
<body>
|
||||||
<h2>Paste Service</h2>
|
<h2>Paste Service</h2>
|
||||||
<h4>Upload file</h4>
|
<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>
|
<div>
|
||||||
<input id="upload_file" type="file" name="u">
|
<input id="upload_file" type="file" name="u">
|
||||||
<div><input type="submit" value="Send"> (<span id="file_size">0 byte</span>)</div>
|
<div><input type="submit" value="Send"> (<span id="file_size">0 byte</span>)</div>
|
||||||
|
@ -47,7 +47,7 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h4>Upload text</h4>
|
<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>
|
<div>
|
||||||
<textarea id="text_input" style="width: 30%; max-width: 100%; " rows ="5" cols="50"
|
<textarea id="text_input" style="width: 30%; max-width: 100%; " rows ="5" cols="50"
|
||||||
name="u" placeholder="Paste your text here..."></textarea>
|
name="u" placeholder="Paste your text here..."></textarea>
|
||||||
|
@ -63,7 +63,7 @@
|
||||||
document.getElementById("text_input").addEventListener("input", update_textarea, false);
|
document.getElementById("text_input").addEventListener("input", update_textarea, false);
|
||||||
</script>
|
</script>
|
||||||
<br>
|
<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>© 2022 rikkaneko</p>
|
<p>© 2022 rikkaneko</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
27
src/index.ts
27
src/index.ts
|
@ -18,10 +18,10 @@
|
||||||
|
|
||||||
import {AwsClient} from "aws4fetch";
|
import {AwsClient} from "aws4fetch";
|
||||||
import {customAlphabet} from "nanoid";
|
import {customAlphabet} from "nanoid";
|
||||||
import {contentType} from "mime-types"
|
import {contentType} from "mime-types";
|
||||||
|
|
||||||
// Constants
|
// 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 PASTE_INDEX_HTML_URL = "https://raw.githubusercontent.com/rikkaneko/paste/main/paste.html"
|
||||||
const UUID_LENGTH = 4
|
const UUID_LENGTH = 4
|
||||||
|
|
||||||
|
@ -71,8 +71,9 @@ export default {
|
||||||
ctx: ExecutionContext
|
ctx: ExecutionContext
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const {url, method, headers} = request;
|
const {url, method, headers} = request;
|
||||||
const {hostname, pathname, searchParams} = new URL(url);
|
const {pathname} = new URL(url);
|
||||||
const path = pathname.replace(/\/+$/, "") || "/"
|
const path = pathname.replace(/\/+$/, "") || "/";
|
||||||
|
let cache = caches.default;
|
||||||
const s3 = new AwsClient({
|
const s3 = new AwsClient({
|
||||||
accessKeyId: env.AWS_ACCESS_KEY_ID,
|
accessKeyId: env.AWS_ACCESS_KEY_ID,
|
||||||
secretAccessKey: env.AWS_SECRET_ACCESS_KEY
|
secretAccessKey: env.AWS_SECRET_ACCESS_KEY
|
||||||
|
@ -168,9 +169,11 @@ export default {
|
||||||
last_modified: Date.now()
|
last_modified: Date.now()
|
||||||
};
|
};
|
||||||
|
|
||||||
const counter = await env.PASTE_INDEX.get("__count__") || "0";
|
const p1 = env.PASTE_INDEX.get("__count__").then(counter => {
|
||||||
await env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor));
|
env.PASTE_INDEX.put("__count__", (Number(counter ?? "0") + 1).toString());
|
||||||
await env.PASTE_INDEX.put("__count__", (Number(counter) + 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));
|
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", {
|
||||||
|
@ -224,7 +227,6 @@ export default {
|
||||||
// Fetch the paste by uuid
|
// Fetch the paste by uuid
|
||||||
case "GET": {
|
case "GET": {
|
||||||
// Enable CF cache for authorized request
|
// Enable CF cache for authorized request
|
||||||
let cache = caches.default;
|
|
||||||
// Match in existing cache
|
// Match in existing cache
|
||||||
let res = await cache.match(request.url);
|
let res = await cache.match(request.url);
|
||||||
if (res === undefined) {
|
if (res === undefined) {
|
||||||
|
@ -263,7 +265,9 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache hit
|
// Cache hit
|
||||||
return res;
|
let { readable, writable } = new TransformStream();
|
||||||
|
res.body!.pipeTo(writable);
|
||||||
|
return new Response(readable, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete paste by uuid
|
// Delete paste by uuid
|
||||||
|
@ -284,7 +288,6 @@ export default {
|
||||||
await env.PASTE_INDEX.put("__count__", (Number(counter) - 1).toString());
|
await env.PASTE_INDEX.put("__count__", (Number(counter) - 1).toString());
|
||||||
|
|
||||||
// Invalidate CF cache
|
// Invalidate CF cache
|
||||||
let cache = caches.default;
|
|
||||||
await cache.delete(request.url);
|
await cache.delete(request.url);
|
||||||
return new Response("OK\n");
|
return new Response("OK\n");
|
||||||
} else {
|
} else {
|
||||||
|
@ -305,13 +308,13 @@ 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 `https://${SERVICE_URL}/${uuid}
|
return `link: https://${SERVICE_URL}/${uuid}
|
||||||
id: ${uuid}
|
id: ${uuid}
|
||||||
title: ${descriptor.title || "<empty>"}
|
title: ${descriptor.title || "<empty>"}
|
||||||
mime-type: ${descriptor.mime_type ?? "application/octet-stream"}
|
mime-type: ${descriptor.mime_type ?? "application/octet-stream"}
|
||||||
password: ${(!!descriptor.password)}
|
password: ${(!!descriptor.password)}
|
||||||
editable: ${descriptor.editable? descriptor.editable: true}
|
editable: ${descriptor.editable? descriptor.editable: true}
|
||||||
last modified at ${date.toISOString()}
|
created at ${date.toISOString()}
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue