From 8d6c6a5d7c1b4529a65517e2d8cd4621ff958a14 Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Tue, 26 Mar 2024 22:58:09 -0700 Subject: [PATCH] IPPEE --- src/server/lib/files.ts | 2 +- src/server/routes/api/v1/file/index.ts | 18 ++++++++++-------- src/server/routes/api/v1/file/individual.ts | 20 ++++++++++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/server/lib/files.ts b/src/server/lib/files.ts index 0e97811..8edd2ac 100644 --- a/src/server/lib/files.ts +++ b/src/server/lib/files.ts @@ -381,7 +381,7 @@ export class UploadStream extends Writable { async _destroy(error: Error | null, callback: (err?: Error|null) => void) { this.error = error || undefined await this.abort() - callback() + callback(error) } /** diff --git a/src/server/routes/api/v1/file/index.ts b/src/server/routes/api/v1/file/index.ts index 3a63af8..5a2ee98 100644 --- a/src/server/routes/api/v1/file/index.ts +++ b/src/server/routes/api/v1/file/index.ts @@ -52,6 +52,16 @@ export default function(files: Files) { return resolve(ctx.body("body must be supplied", 400)) let file = files.createWriteStream(acc?.id) + + file + .on("error", escalate) + .on("finish", async () => { + if (!ctx.env.incoming.readableEnded) await new Promise(res => ctx.env.incoming.once("end", res)) + file.commit() + .then(id => resolve(ctx.body(id!))) + .catch(escalate) + }) + let parser = formidable({ maxFieldsSize: 65536, maxFileSize: files.config.maxDiscordFileSize*files.config.maxDiscordFiles, @@ -137,14 +147,6 @@ export default function(files: Files) { escalate(err) if (!file.destroyed) file.destroy(err) }) - file.on("error", escalate) - - file.on("finish", async () => { - if (!ctx.env.incoming.readableEnded) await new Promise(res => ctx.env.incoming.once("end", res)) - file.commit() - .then(id => resolve(ctx.body(id!))) - .catch(escalate) - }) })} ) diff --git a/src/server/routes/api/v1/file/individual.ts b/src/server/routes/api/v1/file/individual.ts index 82dd6b5..68c7a31 100644 --- a/src/server/routes/api/v1/file/individual.ts +++ b/src/server/routes/api/v1/file/individual.ts @@ -20,7 +20,7 @@ const router = new Hono<{ }>() router.all("*", getAccount) -export default function(files: Files) { +export default function(files: Files, apiRoot: Hono) { router.get("/:id", async (ctx) => { const fileId = ctx.req.param("id") @@ -106,7 +106,23 @@ export default function(files: Files) { } }) - router.post("/:id") + router.on(["PUT", "POST"], "/:id", async (ctx) => { + ctx.env.incoming.push( + `--${ctx.req.header("content-type")?.match(/boundary=(\S+)/)?.[1]}\r\n` + + `Content-Disposition: form-data; name="uploadId"\r\n\r\n` + + ctx.req.param("id") + + "\r\n" + ) + + return apiRoot.fetch( + new Request( + (new URL( + `/api/v1/file`, ctx.req.raw.url)).href, + ctx.req.raw + ), + ctx.env + ) + }) return router }