From 2ecea6e81db1a59448ed72c0dd77b86425dbde36 Mon Sep 17 00:00:00 2001 From: Hajamieli Date: Sat, 12 Aug 2023 06:37:40 +0300 Subject: [PATCH] small cleanup --- routes.ts | 6 +-- site/index.html | 114 +----------------------------------------------- site/script.js | 64 +++++++++++++++++---------- site/style.css | 3 ++ 4 files changed, 50 insertions(+), 137 deletions(-) create mode 100644 site/style.css diff --git a/routes.ts b/routes.ts index d57e596..61124ad 100644 --- a/routes.ts +++ b/routes.ts @@ -14,14 +14,14 @@ export const site = async (ctx: RouterContext) => { if(ctx.params.filename == "sw.js" || ctx.params.filename == "script.js"){ ctx.response.type = "text/javascript" } + if(ctx.params.filename == "style.css"){ + ctx.response.type = "text/css" + } ctx.response.body = await renderFileToString(`${Deno.cwd()}/site/${ctx.params.filename}`, {}) } export const icons = async (ctx: RouterContext, next: any) => { const identifier = ctx.params.filename - - console.log(["t_192.png", "t_48.png", "t_96.png"].indexOf(identifier)) - if (["t_192.png", "t_48.png", "t_96.png"].indexOf(identifier) != -1){ const img = await Deno.readFile(`${Deno.cwd()}/site/i/${ctx.params.filename}`); ctx.response.type = "image/png"; diff --git a/site/index.html b/site/index.html index d4f0e3f..96894be 100644 --- a/site/index.html +++ b/site/index.html @@ -25,12 +25,7 @@ - + + \ No newline at end of file diff --git a/site/script.js b/site/script.js index 2f6afbe..b784e67 100644 --- a/site/script.js +++ b/site/script.js @@ -24,7 +24,7 @@ const splitter = (file, hash) => { chunkForm.append('file', new Blob([chunk], {type:"video/webm"})) chunks.push( - fetch("/test", { + fetch("/uploadchunk", { method: "POST", headers: { "Content-Number": i, @@ -32,7 +32,8 @@ const splitter = (file, hash) => { "File-Hash": hash }, body: chunkForm - })) + }).then(res => Promise.resolve(res.status)) + ) } return chunks @@ -50,35 +51,54 @@ const processFile = file => { fr.readAsArrayBuffer(file) fr.addEventListener("loadend", e => { - digest(e.target.result).then(digestBuffer => { - const hashAsString = Array.from(new Uint8Array(digestBuffer)).map((b) => b.toString(16).padStart(2, "0")).join(""); // hex the buffer for readability - Promise.all(splitter(e.target.result,hashAsString)) - .then((values) => { - fetch(`/finish`, { - method: "POST", - headers: { - "Content-Type": "json", - }, - body: JSON.stringify({"hash": hashAsString, "type": file.type}) - }) + let details = JSON.stringify({"usercode": "anonymous", "hash": hashAsString, "size": file.size, "type": file.type}) + + fetch(`/announce`, { + method: "POST", + headers:{ + "Content-Type": "json", + }, + body: details + }) + .then(res => { + if(res.status == 200){ + Promise.all(splitter(e.target.result,hashAsString)) + .then((values) => { + if(values.every(x => x === 200)){ + fetch(`/finish`, { + method: "POST", + headers: { + "Content-Type": "json", + }, + body: details + }).then(res => res.json()) + .then(res => { + console.log(res) + results.innerHTML += `https://i.hjmt.xyz/${res.file}
` + }) + }else{ + alert("upload failed") + } + }) + }else if(res.status == 409){ + alert("file exists!") + }else if(res.status == 403){ + alert("get a usertoken for bigger files") + } }) - }) + }) + }) } submit.addEventListener("click", e => { - e.preventDefault(); - let fd = new FormData(); - + e.preventDefault(); for (const file of form.media.files){ - processFile(file) - - fd.append('file', file) - console.log(file) } -}) \ No newline at end of file +}) + diff --git a/site/style.css b/site/style.css new file mode 100644 index 0000000..da37fbe --- /dev/null +++ b/site/style.css @@ -0,0 +1,3 @@ +:root { + color-scheme: light dark; +} \ No newline at end of file