From 90184b3f31f603311910892bcd566715f304201d Mon Sep 17 00:00:00 2001 From: Michael C Date: Thu, 15 Aug 2024 00:03:18 -0400 Subject: [PATCH] add file dropper - add filedropper to root - move functions to external JS --- .gitignore | 3 ++- docs/drop.html | 21 +++++++++++++++++++ docs/editor.html | 2 +- docs/index.html | 52 ++++++++++++++---------------------------------- docs/style.css | 22 ++++++++++++++++++++ docs/upload.js | 30 ++++++++++++++++++++++++++++ worker/consts.ts | 3 ++- worker/index.ts | 1 + 8 files changed, 94 insertions(+), 40 deletions(-) create mode 100644 docs/drop.html create mode 100644 docs/upload.js diff --git a/.gitignore b/.gitignore index 04c01ba..65b7254 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -dist/ \ No newline at end of file +dist/ +.wrangler/ \ No newline at end of file diff --git a/docs/drop.html b/docs/drop.html new file mode 100644 index 0000000..7ea456a --- /dev/null +++ b/docs/drop.html @@ -0,0 +1,21 @@ + + + + + + +
+

File Drop

+ + + +
+ + + \ No newline at end of file diff --git a/docs/editor.html b/docs/editor.html index 86ceb23..6863cf9 100644 --- a/docs/editor.html +++ b/docs/editor.html @@ -11,7 +11,7 @@

Text Editor

+ + + \ No newline at end of file diff --git a/docs/style.css b/docs/style.css index 424adc2..5ed362d 100644 --- a/docs/style.css +++ b/docs/style.css @@ -15,4 +15,26 @@ input, button, textarea { } a, body { color: #ddd; +} +ul { + padding: 0; + display: none; +} +ul:has(>a) { + display: flex; + flex-direction: column; +} +input[type="file"] { + display: none; +} +.center { + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + height: 98vh +} +label { + cursor: pointer; + text-decoration: underline; } \ No newline at end of file diff --git a/docs/upload.js b/docs/upload.js new file mode 100644 index 0000000..c2a2037 --- /dev/null +++ b/docs/upload.js @@ -0,0 +1,30 @@ +const BINURL = "https://b.mmpc.me" +const SIZELIMIT = 1024 * 1024 * 25 // 25MB + +function addFile(binurl, filename) { + const a = document.createElement("a") + a.href = `${binurl}/${filename}` + a.innerText = filename || binurl + document.getElementById("filelist").appendChild(a) + clipboard(a.href) +} +const upload = (body, contentType, fileName, binType = "b") => + fetch(`${BINURL}/${binType}`, { + method: "POST", headers: { "Content-Type": contentType, "File-Name": fileName }, body + }) + .then(async (res) => addFile(`${BINURL}/${binType}/${await res.text()}`, fileName)) + .catch((err) => console.error(err)) +const uploadText = () => upload(document.getElementById("upload_body").value, "text/plain", "text.txt") +const setUrl = () => upload(document.getElementById("dest").value, "", "", "u") +const uploadFile = (file) => upload(file, file.type, file.name) +const fileHandler = (files) => files.forEach((file) => { + if (file.size > SIZELIMIT) alert(`File ${file.name} is too large (${(file.size / 1024 / 1024).toFixed(1)}MB)`) + uploadFile(file) +}) +const dropHandler = (e) => { + e.preventDefault() + fileHandler(e.dataTransfer.files) +}; +const chooseHandler = (e) => fileHandler(e.target.files) +const dragOverHandler = (e) => e.preventDefault() +const clipboard = (str) => navigator.clipboard.writeText(str) \ No newline at end of file diff --git a/worker/consts.ts b/worker/consts.ts index 8937004..471a8f5 100644 --- a/worker/consts.ts +++ b/worker/consts.ts @@ -61,6 +61,7 @@ export const API_DOCS = { "/ping": "pong", "/upload": "mchangrh.github.io/cfkv-bin", "/editor": "mchangrh.github.io/cfkv-bin/editor", + "/drop": "mchangrh.github.io/cfkv-bin/drop", "/version": "short git commit id", "/github": "https://github.com/mchangrh/cfkv-bin", "/api": "this", @@ -74,7 +75,7 @@ export const methods = ["get", "post", "put", "delete"] export const stdheaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", - "Access-Control-Allow-Headers": "Content-Type, File-Name, X-Requested-With", + "Access-Control-Allow-Headers": "Content-Type, File-Name", "Cache-Control": "no-store,max-age=0,no-cache,must-revalidate,proxy-revalidate", "X-Robots-Tag": "noindex, nofollow, noarchive, nosnippet", } diff --git a/worker/index.ts b/worker/index.ts index 11aaa57..6522753 100644 --- a/worker/index.ts +++ b/worker/index.ts @@ -81,6 +81,7 @@ const handleRequest = async (request: Request) => { else if (pathname === '/github') return redirectResponse('https://github.com/mchangrh/cfkv-bin#readme') else if (pathname === '/upload') return redirectResponse('https://mchangrh.github.io/cfkv-bin/') else if (pathname === '/editor') return redirectResponse('https://mchangrh.github.io/cfkv-bin/editor') + else if (pathname === '/drop') return redirectResponse('https://mchangrh.github.io/cfkv-bin/drop') else if (pathname === '/robots.txt') return textResponse('User-agent: *\nDisallow: /') else if (pathname === '/favicon.ico') return new Response(null, { status: 404 }) // send type to handlers