From b98294e5be876955480f92daeee2dc5c662be9b9 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 29 Sep 2023 21:37:35 +0200 Subject: [PATCH] [mastodon-client] PUT /media/:id --- .../server/api/mastodon/endpoints/media.ts | 31 +++++++++++++------ .../src/server/api/mastodon/helpers/media.ts | 18 +++++++++-- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/media.ts b/packages/backend/src/server/api/mastodon/endpoints/media.ts index 52fce25a8..cd1c9433f 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/media.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/media.ts @@ -1,5 +1,4 @@ import Router from "@koa/router"; -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"; @@ -19,7 +18,7 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: } const id = convertId(ctx.params.id, IdType.IceshrimpId); - const file = await MediaHelpers.getMedia(user, id); + const file = await MediaHelpers.getMediaPacked(user, id); if (!file) { ctx.status = 404; @@ -36,15 +35,27 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: } }); router.put<{ Params: { id: string } }>("/v1/media/:id", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.updateMedia( - convertId(ctx.params.id, IdType.IceshrimpId), - ctx.request.body as any, - ); - ctx.body = convertAttachment(data.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + const id = convertId(ctx.params.id, IdType.IceshrimpId); + const file = await MediaHelpers.getMedia(user, id); + + if (!file) { + ctx.status = 404; + ctx.body = { error: "File not found" }; + return; + } + + const result = await MediaHelpers.updateMedia(user, file, ctx.request.body) + .then(p => FileConverter.encode(p)); + ctx.body = convertAttachment(result); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/helpers/media.ts b/packages/backend/src/server/api/mastodon/helpers/media.ts index f4f0146cf..9760a5ded 100644 --- a/packages/backend/src/server/api/mastodon/helpers/media.ts +++ b/packages/backend/src/server/api/mastodon/helpers/media.ts @@ -3,6 +3,7 @@ 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"; export class MediaHelpers { public static async uploadMedia(user: ILocalUser, file: multer.File, body: any): Promise> { @@ -16,8 +17,21 @@ export class MediaHelpers { .then(p => DriveFiles.pack(p)); } - public static async getMedia(user: ILocalUser, id: string): Promise | null> { - return DriveFiles.findOneBy({id: id, userId: user.id}) + public static async updateMedia(user: ILocalUser, file: DriveFile, body: any): Promise> { + await DriveFiles.update(file.id, { + comment: body?.description ?? undefined + }); + + return DriveFiles.findOneByOrFail({id: file.id, userId: user.id}) + .then(p => DriveFiles.pack(p)); + } + + public static async getMediaPacked(user: ILocalUser, id: string): Promise | null> { + return this.getMedia(user, id) .then(p => p ? DriveFiles.pack(p) : null); } + + public static async getMedia(user: ILocalUser, id: string): Promise { + return DriveFiles.findOneBy({id: id, userId: user.id}); + } }