From 2dbbd1d39dc72b1e5a58cd9bbc0372d1599b3b01 Mon Sep 17 00:00:00 2001 From: CutestNekoAqua Date: Thu, 23 Feb 2023 16:55:38 +0100 Subject: [PATCH] use multer upload instead --- .../server/api/mastodon/endpoints/status.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index 7da2a2d7b0..b2c6f97815 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -295,21 +295,18 @@ export function apiStatusMastodon(router: Router): void { } }, ); - router.post("/v1/media", async (ctx) => { + router.post("/v1/media", upload.single("file"), async (ctx) => { const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - let multipartData = await ctx.request.files; + let multipartData = await ctx.request.file; if (!multipartData) { ctx.body = { error: "No image" }; ctx.status = 401; return; } - if ((multipartData as any).file) { - multipartData = (multipartData as any).file; - } - const image = fs.readFileSync((multipartData as any).path); + const image = fs.readFileSync((multipartData).path); const data = await client.uploadMedia(image); ctx.body = data.data; } catch (e: any) { @@ -318,20 +315,18 @@ export function apiStatusMastodon(router: Router): void { ctx.body = e.response.data; } }); - router.post("/v2/media", async (ctx) => { + router.post("/v2/media", upload.single("file"), async (ctx) => { const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - const multipartData = await ctx.file; + let multipartData = await ctx.request.file; if (!multipartData) { ctx.body = { error: "No image" }; ctx.status = 401; return; } - const [path] = await createTemp(); - await pump(multipartData.buffer, fs.createWriteStream(path)); - const image = fs.readFileSync(path); + const image = fs.readFileSync((multipartData).path); const data = await client.uploadMedia(image); ctx.body = data.data; } catch (e: any) { @@ -340,6 +335,7 @@ export function apiStatusMastodon(router: Router): void { ctx.body = e.response.data; } }); + }); router.get<{ Params: { id: string } }>( "/v1/media/:id", async (ctx) => {