From 9b223abeda3ec7f79ec1f8cd66efb42c9ba44cfe Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 29 Sep 2023 21:39:02 +0200 Subject: [PATCH] [mastodon-client] POST /{v1,v2}/media --- .../server/api/mastodon/endpoints/media.ts | 44 +++++++------------ .../src/server/api/mastodon/helpers/media.ts | 18 ++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 packages/backend/src/server/api/mastodon/helpers/media.ts diff --git a/packages/backend/src/server/api/mastodon/endpoints/media.ts b/packages/backend/src/server/api/mastodon/endpoints/media.ts index a6819e825..6584996c9 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/media.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/media.ts @@ -3,6 +3,9 @@ import { getClient } from "@/server/api/mastodon/index.js"; import { convertId, IdType } from "@/misc/convert-id.js"; import { convertAttachment } 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"; export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: multer.Instance): void { router.get<{ Params: { id: string } }>("/v1/media/:id", async (ctx) => { @@ -37,42 +40,29 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: } }); - fileRouter.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); + fileRouter.post(["/v2/media", "/v1/media"], upload.single("file"), async (ctx) => { try { - const multipartData = await ctx.file; - if (!multipartData) { - ctx.body = { error: "No image" }; + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { ctx.status = 401; return; } - const data = await client.uploadMedia(multipartData); - ctx.body = convertAttachment(data.data as MastodonEntity.Attachment); - } catch (e: any) { - console.error(e); - ctx.status = 401; - ctx.body = e.response.data; - } - }); - fileRouter.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; - if (!multipartData) { + + const file = await ctx.file; + if (!file) { ctx.body = { error: "No image" }; - ctx.status = 401; + ctx.status = 400; return; } - const data = await client.uploadMedia(multipartData, ctx.request.body); - ctx.body = convertAttachment(data.data as MastodonEntity.Attachment); + const result = await MediaHelpers.uploadMedia(user, file, ctx.request.body) + .then(p => FileConverter.encode(p)); + ctx.body = convertAttachment(result); } catch (e: any) { console.error(e); - ctx.status = 401; - ctx.body = e.response.data; + ctx.status = 500; + ctx.body = { error: e.message }; } }); } diff --git a/packages/backend/src/server/api/mastodon/helpers/media.ts b/packages/backend/src/server/api/mastodon/helpers/media.ts new file mode 100644 index 000000000..fd1bc772b --- /dev/null +++ b/packages/backend/src/server/api/mastodon/helpers/media.ts @@ -0,0 +1,18 @@ +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"; + +export class MediaHelpers { + public static async uploadMedia(user: ILocalUser, file: multer.File, body: any): Promise> { + return await addFile({ + user: user, + path: file.path, + name: file.originalname !== null && file.originalname !== 'file' ? file.originalname : undefined, + comment: body?.description ?? undefined, + sensitive: false, //FIXME: this needs to be updated on from composing a post with the media attached + }) + .then(p => DriveFiles.pack(p)); + } +}