From a0885013bbc91bf61c4a635de8a784ed545d487b Mon Sep 17 00:00:00 2001 From: C4illin Date: Fri, 17 May 2024 19:23:05 +0200 Subject: [PATCH] working file upload --- .gitignore | 5 ++++- bun.lockb | Bin 15389 -> 15742 bytes package.json | 3 ++- src/index.ts | 42 +++++++++++++++++++++++++++++++++--------- src/pages/index.html | 7 ++++--- src/public/index.js | 24 +++++++++++++++++++++++- 6 files changed, 66 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 87e5610..bc34871 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,7 @@ yarn-error.log* **/*.tgz **/*.log package-lock.json -**/*.bun \ No newline at end of file +**/*.bun +/src/uploads +/uploads +/mydb.sqlite \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index bd8f292c040cb6e3f9ffac9766146c4a9285136e..501b456b8636c0ea43bac9badc697ad1db853932 100644 GIT binary patch delta 2881 zcmb_eeQZ-z6o2=%-S)PXZFC>)x-l3q+HGyujjh}GC@3hS5W*j%W3s{M7FQ@62ns`G zgQ#H;4CNSP(a91+5ET@rC?5(K0t|tOAejOogzpH%DWXI`f9JJDNsJNWNzOa(ckj9P zo^$R!=iZJ*_ZP2rSSE})edhG$IsPTv^CuqARbOllo*I4ISG{^?sUcy1?XH`{C6%l} zZ&r)W8Umqjk|eb@);2cz>m;c@&^+H?D@i}2OON(Tkcp5-AWe{a!tK7MrrG{_N!kL; z<1JymE3zcT0{a3@jp&xr!F_-?Xe22WQWogf;-@{>o7PgSlcacnW@l+56eVjICr5>5 zsS3+`XitOQkC1VYMo3;&flPp`pYN*;)YjGe0{&WI5N~(x=i#^m9@0N9ObU41e zh2tOD1C}s_cTFXhh&gY*O8+DgdOl35K90OfED?9f0Oq-kU?!HVkK^6KSUu;w@BkG( zEO~-KU^ZlMxc^ZkkGFtX=7D*h{IIlz*OzQ)U}DLZIl!z~0A^yz?Fh8mUKEx%!4HT% zWM*RduOb=p|KSk-$55&ZbH#X+NVfccp;Xs@)JVok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+vlB__&3oH<2L{R diff --git a/package.json b/package.json index ec10e89..f573fa1 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "dependencies": { "@elysiajs/html": "^1.0.2", "@elysiajs/static": "^1.0.2", - "elysia": "latest" + "elysia": "latest", + "nanoid": "^5.0.7" }, "devDependencies": { "bun-types": "latest" diff --git a/src/index.ts b/src/index.ts index 6fba87e..ca7096d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,39 @@ import { Elysia } from "elysia"; -import { staticPlugin } from '@elysiajs/static' -import { html } from '@elysiajs/html' +import { staticPlugin } from "@elysiajs/static"; +import { html } from "@elysiajs/html"; +import { Database } from "bun:sqlite"; + +const db = new Database("./mydb.sqlite"); +const baseDir = import.meta.dir; +const uploadsDir = "./uploads/"; const app = new Elysia() - .use(html()) - .use(staticPlugin({ - assets: "src/public/", prefix: "/" - })) - .get("/", () => Bun.file("src/pages/index.html")) - .listen(3000); + .use(html()) + .use( + staticPlugin({ + assets: "src/public/", + prefix: "/", + }), + ) + .get("/", () => Bun.file("src/pages/index.html")) + .post("/upload", async (ctx) => { + console.log(ctx.body); + if (ctx.body?.file) { + await Bun.write(`${uploadsDir}${ctx.body.file.name}`, ctx.body.file); + } else if (ctx.body?.files) { + if (Array.isArray(ctx.body.files)) { + console.log("Found array of files"); + for (const file of ctx.body.files) { + console.log(file); + await Bun.write(`${uploadsDir}${file.name}`, file); + } + } else { + await Bun.write(`${uploadsDir}${ctx.body.files.name}`, ctx.body.files); + } + } + }) + .listen(3000); console.log( - `🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}` + `🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`, ); diff --git a/src/pages/index.html b/src/pages/index.html index f7e3c7e..de49eb3 100644 --- a/src/pages/index.html +++ b/src/pages/index.html @@ -27,13 +27,13 @@
-
+
- +
@@ -48,7 +48,8 @@
- + +
diff --git a/src/public/index.js b/src/public/index.js index 218563c..3d758b5 100644 --- a/src/public/index.js +++ b/src/public/index.js @@ -1,9 +1,11 @@ // Select the file input element const fileInput = document.querySelector('input[type="file"]'); +let filesToUpload = []; + // Add a 'change' event listener to the file input element fileInput.addEventListener("change", (e) => { - console.log(e.target.files); + console.log(e.target.files); // Get the selected files from the event target const files = e.target.files; @@ -23,4 +25,24 @@ fileInput.addEventListener("change", (e) => { // Append the row to the file-list table fileList.appendChild(row); } + + uploadFiles(files); }); + +const uploadFiles = (files) => { + const formData = new FormData(); + + for (let i = 0; i < files.length; i++) { + formData.append("files", files[i], files[i].name); + } + + fetch("/upload", { + method: "POST", + body: formData, + }) + .then((res) => res.json()) + .then((data) => { + console.log(data); + }) + .catch((err) => console.log(err)); +};