[mastodon-client] Replace koa-multer with koa-body

This commit is contained in:
Laura Hausmann 2023-10-05 18:38:03 +02:00
parent 60f314cb87
commit 75f6732928
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
8 changed files with 24 additions and 15 deletions

1
.pnp.cjs generated
View File

@ -14389,6 +14389,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
["@types/cbor", "npm:6.0.0"],\ ["@types/cbor", "npm:6.0.0"],\
["@types/escape-regexp", "npm:0.0.1"],\ ["@types/escape-regexp", "npm:0.0.1"],\
["@types/fluent-ffmpeg", "npm:2.1.21"],\ ["@types/fluent-ffmpeg", "npm:2.1.21"],\
["@types/formidable", "npm:2.0.6"],\
["@types/js-yaml", "npm:4.0.5"],\ ["@types/js-yaml", "npm:4.0.5"],\
["@types/jsdom", "npm:21.1.1"],\ ["@types/jsdom", "npm:21.1.1"],\
["@types/jsonld", "npm:1.5.9"],\ ["@types/jsonld", "npm:1.5.9"],\

View File

@ -21,7 +21,8 @@
}, },
"optionalDependencies": { "optionalDependencies": {
"@swc/core-android-arm64": "1.3.11", "@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": { "dependencies": {
"@bull-board/api": "5.6.0", "@bull-board/api": "5.6.0",

View File

@ -144,3 +144,7 @@ export function toArray<T>(x: T | T[] | undefined): T[] {
export function toSingle<T>(x: T | T[] | undefined): T | undefined { export function toSingle<T>(x: T | T[] | undefined): T | undefined {
return Array.isArray(x) ? x[0] : x; return Array.isArray(x) ? x[0] : x;
} }
export function toSingleLast<T>(x: T | T[] | undefined): T | undefined {
return Array.isArray(x) ? x.at(-1) : x;
}

View File

@ -43,7 +43,6 @@ app.use(async (ctx, next) => {
// Init router // Init router
const router = new Router(); const router = new Router();
const mastoRouter = new Router(); const mastoRouter = new Router();
const mastoFileRouter = new Router();
const errorRouter = new Router(); const errorRouter = new Router();
// Init multer instance // Init multer instance
@ -66,7 +65,7 @@ router.use(
}), }),
); );
setupMastodonApi(mastoRouter, mastoFileRouter, upload); setupMastodonApi(mastoRouter);
/** /**
* Register endpoint handlers * Register endpoint handlers
@ -147,7 +146,6 @@ errorRouter.all("(.*)", async (ctx) => {
}); });
// Register router // Register router
app.use(mastoFileRouter.routes());
app.use(mastoRouter.routes()); app.use(mastoRouter.routes());
app.use(mastoRouter.allowedMethods()); app.use(mastoRouter.allowedMethods());
app.use(router.routes()); app.use(router.routes());

View File

@ -1,12 +1,13 @@
import Router from "@koa/router"; import Router from "@koa/router";
import { convertId, IdType } from "@/misc/convert-id.js"; import { convertId, IdType } from "@/misc/convert-id.js";
import { convertAttachmentId } from "@/server/api/mastodon/converters.js"; import { convertAttachmentId } from "@/server/api/mastodon/converters.js";
import multer from "@koa/multer";
import authenticate from "@/server/api/authenticate.js"; import authenticate from "@/server/api/authenticate.js";
import { MediaHelpers } from "@/server/api/mastodon/helpers/media.js"; import { MediaHelpers } from "@/server/api/mastodon/helpers/media.js";
import { FileConverter } from "@/server/api/mastodon/converters/file.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) => { router.get<{ Params: { id: string } }>("/v1/media/:id", async (ctx) => {
try { try {
const auth = await authenticate(ctx.headers.authorization, null); 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 { try {
const auth = await authenticate(ctx.headers.authorization, null); const auth = await authenticate(ctx.headers.authorization, null);
const user = auth[0] ?? null; const user = auth[0] ?? null;
@ -73,7 +74,9 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload:
return; 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) { if (!file) {
ctx.body = {error: "No image"}; ctx.body = {error: "No image"};
ctx.status = 400; ctx.status = 400;

View File

@ -1,16 +1,16 @@
import { addFile } from "@/services/drive/add-file.js"; import { addFile } from "@/services/drive/add-file.js";
import { ILocalUser } from "@/models/entities/user.js"; import { ILocalUser } from "@/models/entities/user.js";
import multer from "@koa/multer";
import { DriveFiles } from "@/models/index.js"; import { DriveFiles } from "@/models/index.js";
import { Packed } from "@/misc/schema.js"; import { Packed } from "@/misc/schema.js";
import { DriveFile } from "@/models/entities/drive-file.js"; import { DriveFile } from "@/models/entities/drive-file.js";
import { File } from "formidable";
export class MediaHelpers { export class MediaHelpers {
public static async uploadMedia(user: ILocalUser, file: multer.File, body: any): Promise<Packed<"DriveFile">> { public static async uploadMedia(user: ILocalUser, file: File, body: any): Promise<Packed<"DriveFile">> {
return await addFile({ return await addFile({
user: user, user: user,
path: file.path, path: file.filepath,
name: file.originalname !== null && file.originalname !== 'file' ? file.originalname : undefined, name: file.originalFilename !== null && file.originalFilename !== 'file' ? file.originalFilename : undefined,
comment: body?.description ?? undefined, comment: body?.description ?? undefined,
sensitive: false, //FIXME: this needs to be updated on from composing a post with the media attached sensitive: false, //FIXME: this needs to be updated on from composing a post with the media attached
}) })

View File

@ -9,10 +9,9 @@ import { setupEndpointsSearch } from "./endpoints/search.js";
import { setupEndpointsMedia } from "@/server/api/mastodon/endpoints/media.js"; import { setupEndpointsMedia } from "@/server/api/mastodon/endpoints/media.js";
import { setupEndpointsMisc } from "@/server/api/mastodon/endpoints/misc.js"; import { setupEndpointsMisc } from "@/server/api/mastodon/endpoints/misc.js";
import { HttpMethodEnum, koaBody } from "koa-body"; import { HttpMethodEnum, koaBody } from "koa-body";
import multer from "@koa/multer";
import { setupEndpointsList } from "@/server/api/mastodon/endpoints/list.js"; 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( router.use(
koaBody({ koaBody({
multipart: true, multipart: true,
@ -39,7 +38,7 @@ export function setupMastodonApi(router: Router, fileRouter: Router, upload: mul
setupEndpointsTimeline(router); setupEndpointsTimeline(router);
setupEndpointsNotifications(router); setupEndpointsNotifications(router);
setupEndpointsSearch(router); setupEndpointsSearch(router);
setupEndpointsMedia(router, fileRouter, upload); setupEndpointsMedia(router);
setupEndpointsList(router); setupEndpointsList(router);
setupEndpointsMisc(router); setupEndpointsMisc(router);
} }

View File

@ -5404,6 +5404,7 @@ __metadata:
"@types/cbor": 6.0.0 "@types/cbor": 6.0.0
"@types/escape-regexp": 0.0.1 "@types/escape-regexp": 0.0.1
"@types/fluent-ffmpeg": 2.1.21 "@types/fluent-ffmpeg": 2.1.21
"@types/formidable": ^2.0.5
"@types/js-yaml": 4.0.5 "@types/js-yaml": 4.0.5
"@types/jsdom": 21.1.1 "@types/jsdom": 21.1.1
"@types/jsonld": 1.5.9 "@types/jsonld": 1.5.9
@ -5562,6 +5563,8 @@ __metadata:
optional: true optional: true
"@tensorflow/tfjs-node": "@tensorflow/tfjs-node":
optional: true optional: true
"@types/formidable":
optional: true
languageName: unknown languageName: unknown
linkType: soft linkType: soft