This commit is contained in:
May 2024-03-26 22:58:09 -07:00
parent a62f1cfbc3
commit 8d6c6a5d7c
3 changed files with 29 additions and 11 deletions

View file

@ -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)
}
/**

View file

@ -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)
})
})}
)

View file

@ -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
}