From 75f6732928eba1bdef8baf4a4686df158348af41 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 5 Oct 2023 18:38:03 +0200 Subject: [PATCH] [mastodon-client] Replace koa-multer with koa-body --- .pnp.cjs | 1 + packages/backend/package.json | 3 ++- packages/backend/src/prelude/array.ts | 4 ++++ packages/backend/src/server/api/index.ts | 4 +--- .../src/server/api/mastodon/endpoints/media.ts | 11 +++++++---- .../backend/src/server/api/mastodon/helpers/media.ts | 8 ++++---- packages/backend/src/server/api/mastodon/index.ts | 5 ++--- yarn.lock | 3 +++ 8 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.pnp.cjs b/.pnp.cjs index c9b898d36..772edc33d 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -14389,6 +14389,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["@types/cbor", "npm:6.0.0"],\ ["@types/escape-regexp", "npm:0.0.1"],\ ["@types/fluent-ffmpeg", "npm:2.1.21"],\ + ["@types/formidable", "npm:2.0.6"],\ ["@types/js-yaml", "npm:4.0.5"],\ ["@types/jsdom", "npm:21.1.1"],\ ["@types/jsonld", "npm:1.5.9"],\ diff --git a/packages/backend/package.json b/packages/backend/package.json index f4f1648cf..f029350e0 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -21,7 +21,8 @@ }, "optionalDependencies": { "@swc/core-android-arm64": "1.3.11", - "@tensorflow/tfjs-node": "3.21.1" + "@tensorflow/tfjs-node": "3.21.1", + "@types/formidable": "^2.0.5" }, "dependencies": { "@bull-board/api": "5.6.0", diff --git a/packages/backend/src/prelude/array.ts b/packages/backend/src/prelude/array.ts index 24270038b..5f68ba380 100644 --- a/packages/backend/src/prelude/array.ts +++ b/packages/backend/src/prelude/array.ts @@ -144,3 +144,7 @@ export function toArray(x: T | T[] | undefined): T[] { export function toSingle(x: T | T[] | undefined): T | undefined { return Array.isArray(x) ? x[0] : x; } + +export function toSingleLast(x: T | T[] | undefined): T | undefined { + return Array.isArray(x) ? x.at(-1) : x; +} diff --git a/packages/backend/src/server/api/index.ts b/packages/backend/src/server/api/index.ts index 64412ac90..976191599 100644 --- a/packages/backend/src/server/api/index.ts +++ b/packages/backend/src/server/api/index.ts @@ -43,7 +43,6 @@ app.use(async (ctx, next) => { // Init router const router = new Router(); const mastoRouter = new Router(); -const mastoFileRouter = new Router(); const errorRouter = new Router(); // Init multer instance @@ -66,7 +65,7 @@ router.use( }), ); -setupMastodonApi(mastoRouter, mastoFileRouter, upload); +setupMastodonApi(mastoRouter); /** * Register endpoint handlers @@ -147,7 +146,6 @@ errorRouter.all("(.*)", async (ctx) => { }); // Register router -app.use(mastoFileRouter.routes()); app.use(mastoRouter.routes()); app.use(mastoRouter.allowedMethods()); app.use(router.routes()); diff --git a/packages/backend/src/server/api/mastodon/endpoints/media.ts b/packages/backend/src/server/api/mastodon/endpoints/media.ts index 3b4ac7c00..eb162d879 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/media.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/media.ts @@ -1,12 +1,13 @@ import Router from "@koa/router"; import { convertId, IdType } from "@/misc/convert-id.js"; import { convertAttachmentId } from "@/server/api/mastodon/converters.js"; -import multer from "@koa/multer"; import authenticate from "@/server/api/authenticate.js"; import { MediaHelpers } from "@/server/api/mastodon/helpers/media.js"; import { FileConverter } from "@/server/api/mastodon/converters/file.js"; +import { Files } from "formidable"; +import { toSingleLast } from "@/prelude/array.js"; -export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: multer.Instance): void { +export function setupEndpointsMedia(router: Router): void { router.get<{ Params: { id: string } }>("/v1/media/:id", async (ctx) => { try { const auth = await authenticate(ctx.headers.authorization, null); @@ -63,7 +64,7 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: } }); - fileRouter.post(["/v2/media", "/v1/media"], upload.single("file"), async (ctx) => { + router.post(["/v2/media", "/v1/media"], async (ctx) => { try { const auth = await authenticate(ctx.headers.authorization, null); const user = auth[0] ?? null; @@ -73,7 +74,9 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: return; } - const file = await ctx.file; + //FIXME: why do we have to cast this to any first? + const files = (ctx.request as any).files as Files; + const file = toSingleLast(files['file']); if (!file) { ctx.body = {error: "No image"}; ctx.status = 400; diff --git a/packages/backend/src/server/api/mastodon/helpers/media.ts b/packages/backend/src/server/api/mastodon/helpers/media.ts index 0bdc0cc42..973b1bdd6 100644 --- a/packages/backend/src/server/api/mastodon/helpers/media.ts +++ b/packages/backend/src/server/api/mastodon/helpers/media.ts @@ -1,16 +1,16 @@ import { addFile } from "@/services/drive/add-file.js"; import { ILocalUser } from "@/models/entities/user.js"; -import multer from "@koa/multer"; import { DriveFiles } from "@/models/index.js"; import { Packed } from "@/misc/schema.js"; import { DriveFile } from "@/models/entities/drive-file.js"; +import { File } from "formidable"; export class MediaHelpers { - public static async uploadMedia(user: ILocalUser, file: multer.File, body: any): Promise> { + public static async uploadMedia(user: ILocalUser, file: File, body: any): Promise> { return await addFile({ user: user, - path: file.path, - name: file.originalname !== null && file.originalname !== 'file' ? file.originalname : undefined, + path: file.filepath, + name: file.originalFilename !== null && file.originalFilename !== 'file' ? file.originalFilename : undefined, comment: body?.description ?? undefined, sensitive: false, //FIXME: this needs to be updated on from composing a post with the media attached }) diff --git a/packages/backend/src/server/api/mastodon/index.ts b/packages/backend/src/server/api/mastodon/index.ts index 31b770ee6..7f98b4ffb 100644 --- a/packages/backend/src/server/api/mastodon/index.ts +++ b/packages/backend/src/server/api/mastodon/index.ts @@ -9,10 +9,9 @@ import { setupEndpointsSearch } from "./endpoints/search.js"; import { setupEndpointsMedia } from "@/server/api/mastodon/endpoints/media.js"; import { setupEndpointsMisc } from "@/server/api/mastodon/endpoints/misc.js"; import { HttpMethodEnum, koaBody } from "koa-body"; -import multer from "@koa/multer"; import { setupEndpointsList } from "@/server/api/mastodon/endpoints/list.js"; -export function setupMastodonApi(router: Router, fileRouter: Router, upload: multer.Instance): void { +export function setupMastodonApi(router: Router): void { router.use( koaBody({ multipart: true, @@ -39,7 +38,7 @@ export function setupMastodonApi(router: Router, fileRouter: Router, upload: mul setupEndpointsTimeline(router); setupEndpointsNotifications(router); setupEndpointsSearch(router); - setupEndpointsMedia(router, fileRouter, upload); + setupEndpointsMedia(router); setupEndpointsList(router); setupEndpointsMisc(router); } diff --git a/yarn.lock b/yarn.lock index a3b4628c8..da119ad4d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5404,6 +5404,7 @@ __metadata: "@types/cbor": 6.0.0 "@types/escape-regexp": 0.0.1 "@types/fluent-ffmpeg": 2.1.21 + "@types/formidable": ^2.0.5 "@types/js-yaml": 4.0.5 "@types/jsdom": 21.1.1 "@types/jsonld": 1.5.9 @@ -5562,6 +5563,8 @@ __metadata: optional: true "@tensorflow/tfjs-node": optional: true + "@types/formidable": + optional: true languageName: unknown linkType: soft