[mastodon-client] PUT /media/:id

This commit is contained in:
Laura Hausmann 2023-09-29 21:37:35 +02:00
parent a2dce0fa85
commit b98294e5be
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
2 changed files with 37 additions and 12 deletions

View File

@ -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;

View File

@ -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<Packed<"DriveFile">> {
@ -16,8 +17,21 @@ export class MediaHelpers {
.then(p => DriveFiles.pack(p));
}
public static async getMedia(user: ILocalUser, id: string): Promise<Packed<"DriveFile"> | null> {
return DriveFiles.findOneBy({id: id, userId: user.id})
public static async updateMedia(user: ILocalUser, file: DriveFile, body: any): Promise<Packed<"DriveFile">> {
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<Packed<"DriveFile"> | null> {
return this.getMedia(user, id)
.then(p => p ? DriveFiles.pack(p) : null);
}
public static async getMedia(user: ILocalUser, id: string): Promise<DriveFile | null> {
return DriveFiles.findOneBy({id: id, userId: user.id});
}
}