37
main.go
37
main.go
@@ -37,7 +37,6 @@ type meta struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Ensure correct types for webmanifest on some systems
|
|
||||||
_ = mime.AddExtensionType(".webmanifest", "application/manifest+json")
|
_ = mime.AddExtensionType(".webmanifest", "application/manifest+json")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,17 +88,13 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
<title>slenpaste</title>
|
<title>slenpaste</title>
|
||||||
|
|
||||||
<!-- Icons / PWA -->
|
|
||||||
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
|
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
|
||||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
|
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
|
||||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
|
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
|
||||||
<link rel="icon" href="/favicon.ico">
|
<link rel="icon" href="/favicon.ico">
|
||||||
<link rel="manifest" href="/site.webmanifest">
|
<link rel="manifest" href="/site.webmanifest">
|
||||||
<meta name="theme-color" content="#ffffff">
|
<meta name="theme-color" content="#ffffff">
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 2rem auto; padding: 0 1rem; }
|
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 2rem auto; padding: 0 1rem; }
|
||||||
pre { background: #f6f6f6; padding: .75rem 1rem; border-radius: .5rem; overflow: auto; }
|
pre { background: #f6f6f6; padding: .75rem 1rem; border-radius: .5rem; overflow: auto; }
|
||||||
@@ -130,12 +125,9 @@ Upload from stdin (no file param, expire after 5m):
|
|||||||
Upload from stdin and expire on first view:
|
Upload from stdin and expire on first view:
|
||||||
cat yourfile.txt | curl --data-binary @- "%s/?expiry=view"
|
cat yourfile.txt | curl --data-binary @- "%s/?expiry=view"
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<a href="https://git.slendi.dev/Slendi/slenpaste.koplugin">Download the new KOReader plugin!</a>
|
<a href="https://git.slendi.dev/Slendi/slenpaste.koplugin">Download the new KOReader plugin!</a>
|
||||||
|
|
||||||
<form id="form" enctype="multipart/form-data" method="post" style="margin-top: 13px;">
|
<form id="form" enctype="multipart/form-data" method="post" style="margin-top: 13px;">
|
||||||
<input type="file" name="file" id="fileInput">
|
<input type="file" name="file" id="fileInput">
|
||||||
|
|
||||||
<div class="controls" style="margin-top: .75rem">
|
<div class="controls" style="margin-top: .75rem">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend>Expiry</legend>
|
<legend>Expiry</legend>
|
||||||
@@ -145,16 +137,13 @@ Upload from stdin and expire on first view:
|
|||||||
<label><input type="radio" name="expiry" value="24h"> 1 day</label>
|
<label><input type="radio" name="expiry" value="24h"> 1 day</label>
|
||||||
<label><input type="radio" name="expiry" value="view"> Expire on first view</label>
|
<label><input type="radio" name="expiry" value="view"> Expire on first view</label>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
||||||
<div class="inline">
|
<div class="inline">
|
||||||
<label for="fname">Filename (optional):</label>
|
<label for="fname">Filename (optional):</label>
|
||||||
<input type="text" id="fname" placeholder="example.txt">
|
<input type="text" id="fname" placeholder="example.txt">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<input type="submit" value="Upload file">
|
<input type="submit" value="Upload file">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<h2 style="margin-top:1.5rem">Quick text</h2>
|
<h2 style="margin-top:1.5rem">Quick text</h2>
|
||||||
<p class="small">Type or paste text and upload as a .txt (no file chooser needed).</p>
|
<p class="small">Type or paste text and upload as a .txt (no file chooser needed).</p>
|
||||||
<textarea id="textArea" placeholder="Type or paste here..."></textarea>
|
<textarea id="textArea" placeholder="Type or paste here..."></textarea>
|
||||||
@@ -162,19 +151,15 @@ Upload from stdin and expire on first view:
|
|||||||
<button id="uploadTextBtn" type="button">Upload text</button>
|
<button id="uploadTextBtn" type="button">Upload text</button>
|
||||||
<label class="inline small"><input type="checkbox" id="textAsMd"> Save as .md</label>
|
<label class="inline small"><input type="checkbox" id="textAsMd"> Save as .md</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h2 style="margin-top:1.5rem">Paste or drop images</h2>
|
<h2 style="margin-top:1.5rem">Paste or drop images</h2>
|
||||||
<div id="dropzone" tabindex="0">
|
<div id="dropzone" tabindex="0">
|
||||||
Paste an image (Ctrl/Cmd+V) or drag & drop files here
|
Paste an image (Ctrl/Cmd+V) or drag & drop files here
|
||||||
<div class="small">Images are uploaded as files. Other files dropped here work too.</div>
|
<div class="small">Images are uploaded as files. Other files dropped here work too.</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="result"></div>
|
<div id="result"></div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
(function() {
|
(function() {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const form = document.getElementById("form");
|
const form = document.getElementById("form");
|
||||||
const fileInput = document.getElementById("fileInput");
|
const fileInput = document.getElementById("fileInput");
|
||||||
const fname = document.getElementById("fname");
|
const fname = document.getElementById("fname");
|
||||||
@@ -183,18 +168,15 @@ Upload from stdin and expire on first view:
|
|||||||
const textAsMd = document.getElementById("textAsMd");
|
const textAsMd = document.getElementById("textAsMd");
|
||||||
const dropzone = document.getElementById("dropzone");
|
const dropzone = document.getElementById("dropzone");
|
||||||
const result = document.getElementById("result");
|
const result = document.getElementById("result");
|
||||||
|
|
||||||
function getExpiry() {
|
function getExpiry() {
|
||||||
const checked = form.querySelector('input[name="expiry"]:checked');
|
const checked = form.querySelector('input[name="expiry"]:checked');
|
||||||
return checked ? checked.value : "0";
|
return checked ? checked.value : "0";
|
||||||
}
|
}
|
||||||
|
|
||||||
function setBusy(b) {
|
function setBusy(b) {
|
||||||
if (b) {
|
if (b) {
|
||||||
result.textContent = "Uploading...";
|
result.textContent = "Uploading...";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function showLink(url) {
|
function showLink(url) {
|
||||||
result.innerHTML = 'URL: <a href="' + url + '" target="_blank" rel="noopener">' + url + '</a>';
|
result.innerHTML = 'URL: <a href="' + url + '" target="_blank" rel="noopener">' + url + '</a>';
|
||||||
try {
|
try {
|
||||||
@@ -202,7 +184,6 @@ Upload from stdin and expire on first view:
|
|||||||
result.innerHTML += ' <span class="small">(copied)</span>';
|
result.innerHTML += ' <span class="small">(copied)</span>';
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadFormData(fd, expiry) {
|
async function uploadFormData(fd, expiry) {
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
const res = await fetch("/?expiry=" + encodeURIComponent(expiry), {
|
const res = await fetch("/?expiry=" + encodeURIComponent(expiry), {
|
||||||
@@ -217,38 +198,29 @@ Upload from stdin and expire on first view:
|
|||||||
const url = (await res.text()).trim();
|
const url = (await res.text()).trim();
|
||||||
showLink(url);
|
showLink(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadBlobAsFile(blob, filename, expiry) {
|
async function uploadBlobAsFile(blob, filename, expiry) {
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
const file = new File([blob], filename, { type: blob.type || "application/octet-stream" });
|
const file = new File([blob], filename, { type: blob.type || "application/octet-stream" });
|
||||||
fd.append("file", file);
|
fd.append("file", file);
|
||||||
return uploadFormData(fd, expiry);
|
return uploadFormData(fd, expiry);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enhance file input submit (without relying on default form submit)
|
|
||||||
form.addEventListener("submit", async (e) => {
|
form.addEventListener("submit", async (e) => {
|
||||||
// Use JS path when possible; the default submit still works if JS fails.
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!fileInput.files || fileInput.files.length === 0) {
|
if (!fileInput.files || fileInput.files.length === 0) {
|
||||||
result.textContent = "Choose a file first.";
|
result.textContent = "Choose a file first.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const expiry = getExpiry();
|
const expiry = getExpiry();
|
||||||
|
|
||||||
// If user provided a filename, recreate the File with that name.
|
|
||||||
let file = fileInput.files[0];
|
let file = fileInput.files[0];
|
||||||
if (fname.value.trim()) {
|
if (fname.value.trim()) {
|
||||||
file = new File([file], fname.value.trim(), { type: file.type });
|
file = new File([file], fname.value.trim(), { type: file.type });
|
||||||
}
|
}
|
||||||
|
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append("file", file);
|
fd.append("file", file);
|
||||||
await uploadFormData(fd, expiry);
|
await uploadFormData(fd, expiry);
|
||||||
fileInput.value = "";
|
fileInput.value = "";
|
||||||
fname.value = "";
|
fname.value = "";
|
||||||
});
|
});
|
||||||
|
|
||||||
// Upload text as a .txt (or .md)
|
|
||||||
uploadTextBtn.addEventListener("click", async () => {
|
uploadTextBtn.addEventListener("click", async () => {
|
||||||
const text = textArea.value;
|
const text = textArea.value;
|
||||||
if (!text) {
|
if (!text) {
|
||||||
@@ -258,11 +230,8 @@ Upload from stdin and expire on first view:
|
|||||||
const expiry = getExpiry();
|
const expiry = getExpiry();
|
||||||
const name = (fname.value.trim() || (textAsMd.checked ? "text.md" : "text.txt"));
|
const name = (fname.value.trim() || (textAsMd.checked ? "text.md" : "text.txt"));
|
||||||
await uploadBlobAsFile(new Blob([text], { type: "text/plain" }), name, expiry);
|
await uploadBlobAsFile(new Blob([text], { type: "text/plain" }), name, expiry);
|
||||||
// clear only the filename; keep text in case they want to tweak
|
|
||||||
fname.value = "";
|
fname.value = "";
|
||||||
});
|
});
|
||||||
|
|
||||||
// Drag & drop support
|
|
||||||
;["dragenter","dragover"].forEach(ev => {
|
;["dragenter","dragover"].forEach(ev => {
|
||||||
dropzone.addEventListener(ev, (e) => {
|
dropzone.addEventListener(ev, (e) => {
|
||||||
e.preventDefault(); e.stopPropagation();
|
e.preventDefault(); e.stopPropagation();
|
||||||
@@ -280,14 +249,11 @@ Upload from stdin and expire on first view:
|
|||||||
const files = Array.from(e.dataTransfer.files || []);
|
const files = Array.from(e.dataTransfer.files || []);
|
||||||
if (files.length === 0) return;
|
if (files.length === 0) return;
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
// support multi-file; server will handle each request, so send one by one for URLs
|
|
||||||
for (const f of files) {
|
for (const f of files) {
|
||||||
fd.set("file", f);
|
fd.set("file", f);
|
||||||
await uploadFormData(fd, expiry);
|
await uploadFormData(fd, expiry);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Paste image (or any file) support
|
|
||||||
window.addEventListener("paste", async (e) => {
|
window.addEventListener("paste", async (e) => {
|
||||||
const items = e.clipboardData && e.clipboardData.items ? Array.from(e.clipboardData.items) : [];
|
const items = e.clipboardData && e.clipboardData.items ? Array.from(e.clipboardData.items) : [];
|
||||||
if (!items.length) return;
|
if (!items.length) return;
|
||||||
@@ -301,9 +267,6 @@ Upload from stdin and expire on first view:
|
|||||||
const name = fname.value.trim() || ("pasted." + ext);
|
const name = fname.value.trim() || ("pasted." + ext);
|
||||||
await uploadBlobAsFile(blob, name, expiry);
|
await uploadBlobAsFile(blob, name, expiry);
|
||||||
handled = true;
|
handled = true;
|
||||||
} else if (it.kind === "string") {
|
|
||||||
// If user copies text, allow quick text upload via paste when textarea is focused
|
|
||||||
// We'll let browser put text into the textarea naturally; no upload here.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (handled) e.preventDefault();
|
if (handled) e.preventDefault();
|
||||||
|
|||||||
Reference in New Issue
Block a user