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 bd8f292..501b456 100644 Binary files a/bun.lockb and b/bun.lockb differ 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)); +};