Update paste.html to allow password and read limit settings

Handle empty form data and header values

Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
Joe Ma 2022-06-04 16:08:06 +08:00
parent 00951be84d
commit 78806e331d
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F
2 changed files with 82 additions and 36 deletions

View file

@ -25,14 +25,39 @@
<body> <body>
<h2>Paste Service</h2> <h2>Paste Service</h2>
<h4>Upload file</h4> <form id="upload_file_form" action="https://pb.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"> <h4>Upload file</h4>
<div><input type="submit" value="Send"> (<span id="file_size">0 byte</span>)</div> <div><input id="upload_file" type="file" name="u"></div>
<div>
<h4>Upload text</h4>
<textarea id="text_input" style="width: 30%; max-width: 100%; " rows ="5" cols="50"
name="u" placeholder="Paste your text here..." spellcheck="false"></textarea>
</div>
<h4>Settings</h4>
<div><label for="pass_input">Password: </label>
<input id="pass_input" type="password" name="pass">
<input id="show_pass_button" type="checkbox">
<label for="show_pass_button">Show</label>
</div>
<div>
<label for="read_limit_input">Read limit: </label>
<input id="read_limit_input" type="number" name="read-limit" min="1" style="width: 3em">
</div>
<br>
<div>
<input id="reset_button" type="reset" value="Reset">
<input id="sumbit_form_button" type="submit" value="Sumbit"> (<span id="file_size">0 bytes</span>)
</div>
</div> </div>
</form> </form>
<script> <script>
function update_textarea() {
this.style.height = "auto"
this.style.height = this.scrollHeight + "px";
}
function update_file_size() { function update_file_size() {
let bytes = this.files[0]?.size ?? 0; let bytes = this.files[0]?.size ?? 0;
let size = bytes + " bytes"; let size = bytes + " bytes";
@ -43,24 +68,45 @@
document.getElementById("file_size").innerHTML = size; document.getElementById("file_size").innerHTML = size;
} }
document.getElementById("upload_file").addEventListener("change", update_file_size, false); function toggle_password() {
</script> let input_field = document.getElementById("pass_input");
if (this.checked) {
<h4>Upload text</h4> input_field.type = "text";
<form action="https://pb.nekoul.com" method="POST" enctype=multipart/form-data> } else {
<div> input_field.type = "password";
<textarea id="text_input" style="width: 30%; max-width: 100%; " rows ="5" cols="50" }
name="u" placeholder="Paste your text here..."></textarea>
<div><input type="submit" value="Send"></div>
</div>
</form>
<script>
function update_textarea() {
this.style.height = "auto"
this.style.height = this.scrollHeight + "px";
} }
function reset_form() {
// Re-enable all input elements
let elements = document.getElementById("upload_file_form").elements;
for (let i = 0; i < elements.length; i++) {
elements[i].disabled = false;
}
let size = document.getElementById("file_size");
size.innerHTML = "0 bytes";
}
function handle_submit_form(event) {
let elements = this.elements;
let select_file = elements.namedItem("upload_file");
let text = elements.namedItem("text_input");
if (!!select_file.value.length ^ !!text.value.length) {
for (let i = 0; i < elements.length; i++) {
elements[i].disabled = elements[i].value.length === 0;
}
} else {
alert("You must either upload a file or upload text, but not bothor neither.");
// Prevent default submission
event.preventDefault();
}
}
document.getElementById("upload_file").addEventListener("input", update_file_size, false);
document.getElementById("text_input").addEventListener("input", update_textarea, false); document.getElementById("text_input").addEventListener("input", update_textarea, false);
document.getElementById("show_pass_button").addEventListener("change", toggle_password, false);
document.getElementById("reset_button").addEventListener("click", reset_form, false);
document.getElementById("upload_file_form").addEventListener("submit", handle_submit_form, false)
</script> </script>
<br> <br>
<a href="https://nekoul.com">[Homepage]</a><a href="https://pb.nekoul.com/api">[API]</a> <a href="https://nekoul.com">[Homepage]</a><a href="https://pb.nekoul.com/api">[API]</a>

View file

@ -129,7 +129,7 @@ export default {
const content_type = headers.get("content-type") || ""; const content_type = headers.get("content-type") || "";
let mime_type: string | undefined; let mime_type: string | undefined;
let password: string | undefined; let password: string | undefined;
let max_access_count: number | undefined; let read_limit: number | undefined;
// Content-Type: multipart/form-data // Content-Type: multipart/form-data
if (content_type.includes("form")) { if (content_type.includes("form")) {
const formdata = await request.formData(); const formdata = await request.formData();
@ -155,26 +155,26 @@ export default {
// Set password // Set password
const pass = formdata.get("pass"); const pass = formdata.get("pass");
if (typeof pass === "string") { if (typeof pass === "string") {
password = pass; password = pass || undefined;
} }
const count = formdata.get("max-access-count"); const count = formdata.get("read-limit");
if (typeof count === "string" && !isNaN(+count)) { if (typeof count === "string" && !isNaN(+count)) {
max_access_count = Number(count); read_limit = Number(count) || undefined;
} }
// Raw body // Raw body
} else { } else {
if (headers.has("title")) { if (headers.has("title")) {
title = headers.get("title")!; title = headers.get("title") || "";
mime_type = contentType(title) || undefined; mime_type = contentType(title) || undefined;
} }
mime_type = headers.get("content-type") ?? mime_type; mime_type = headers.get("content-type") || mime_type;
password = headers.get("pass") ?? undefined; password = headers.get("pass") || undefined;
// Handle max-access-count:access_count_remain // Handle read-limit:read_count_remain
const count = headers.get("max-access-count") ?? undefined; const count = headers.get("read-limit") || undefined;
if (count !== undefined && !isNaN(+count)) { if (count !== undefined && !isNaN(+count)) {
max_access_count = Number(count); read_limit = Number(count) || undefined;
} }
buffer = await request.arrayBuffer(); buffer = await request.arrayBuffer();
} }
@ -212,7 +212,7 @@ export default {
title: title ?? undefined, title: title ?? undefined,
last_modified: Date.now(), last_modified: Date.now(),
password: password? sha256(password).slice(0, 16): undefined, password: password? sha256(password).slice(0, 16): undefined,
access_count_remain: max_access_count, read_count_remain: read_limit,
mime_type mime_type
}; };
@ -299,13 +299,13 @@ export default {
} }
// Check if access_count_remain entry present // Check if access_count_remain entry present
if (descriptor.access_count_remain !== undefined) { if (descriptor.read_count_remain !== undefined) {
if (descriptor.access_count_remain <= 0) { if (descriptor.read_count_remain <= 0) {
return new Response("Paste expired.\n", { return new Response("Paste expired.\n", {
status: 410 status: 410
}) })
} }
descriptor.access_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)));
} }
@ -413,8 +413,8 @@ 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}
access count remain: ${descriptor.access_count_remain !== undefined? remaining read count: ${descriptor.read_count_remain !== undefined?
descriptor.access_count_remain? descriptor.access_count_remain: `0 (expired)`: "-"} descriptor.read_count_remain? descriptor.read_count_remain: `0 (expired)`: "-"}
created at ${date.toISOString()} created at ${date.toISOString()}
` `
} }
@ -455,5 +455,5 @@ interface PasteIndexEntry {
last_modified: number, last_modified: number,
password?: string password?: string
editable?: boolean, // Default: True editable?: boolean, // Default: True
access_count_remain?: number read_count_remain?: number
} }