mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-29 05:17:39 -07:00
[mastodon-client] PUT /media/:id
This commit is contained in:
parent
a2dce0fa85
commit
b98294e5be
2 changed files with 37 additions and 12 deletions
|
@ -1,5 +1,4 @@
|
||||||
import Router from "@koa/router";
|
import Router from "@koa/router";
|
||||||
import { getClient } from "@/server/api/mastodon/index.js";
|
|
||||||
import { convertId, IdType } from "@/misc/convert-id.js";
|
import { convertId, IdType } from "@/misc/convert-id.js";
|
||||||
import { convertAttachment } from "@/server/api/mastodon/converters.js";
|
import { convertAttachment } from "@/server/api/mastodon/converters.js";
|
||||||
import multer from "@koa/multer";
|
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 id = convertId(ctx.params.id, IdType.IceshrimpId);
|
||||||
const file = await MediaHelpers.getMedia(user, id);
|
const file = await MediaHelpers.getMediaPacked(user, id);
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
ctx.status = 404;
|
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) => {
|
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 {
|
try {
|
||||||
const data = await client.updateMedia(
|
const auth = await authenticate(ctx.headers.authorization, null);
|
||||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
const user = auth[0] ?? null;
|
||||||
ctx.request.body as any,
|
|
||||||
);
|
if (!user) {
|
||||||
ctx.body = convertAttachment(data.data);
|
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) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
ctx.status = 401;
|
ctx.status = 401;
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { ILocalUser } from "@/models/entities/user.js";
|
||||||
import multer from "@koa/multer";
|
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";
|
||||||
|
|
||||||
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: multer.File, body: any): Promise<Packed<"DriveFile">> {
|
||||||
|
@ -16,8 +17,21 @@ export class MediaHelpers {
|
||||||
.then(p => DriveFiles.pack(p));
|
.then(p => DriveFiles.pack(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async getMedia(user: ILocalUser, id: string): Promise<Packed<"DriveFile"> | null> {
|
public static async updateMedia(user: ILocalUser, file: DriveFile, body: any): Promise<Packed<"DriveFile">> {
|
||||||
return DriveFiles.findOneBy({id: id, userId: user.id})
|
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);
|
.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});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue