[mastodon-client] POST /{v1,v2}/media

This commit is contained in:
Laura Hausmann 2023-09-29 21:39:02 +02:00
parent 366311a8b1
commit 9b223abeda
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 35 additions and 27 deletions

View file

@ -3,6 +3,9 @@ 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";
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 { export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: multer.Instance): void {
router.get<{ Params: { id: string } }>("/v1/media/:id", async (ctx) => { 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) => { fileRouter.post(["/v2/media", "/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);
try { try {
const multipartData = await ctx.file; const auth = await authenticate(ctx.headers.authorization, null);
if (!multipartData) { const user = auth[0] ?? null;
ctx.body = { error: "No image" };
if (!user) {
ctx.status = 401; ctx.status = 401;
return; return;
} }
const data = await client.uploadMedia(multipartData);
ctx.body = convertAttachment(data.data as MastodonEntity.Attachment); const file = await ctx.file;
} catch (e: any) { if (!file) {
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) {
ctx.body = { error: "No image" }; ctx.body = { error: "No image" };
ctx.status = 401; ctx.status = 400;
return; return;
} }
const data = await client.uploadMedia(multipartData, ctx.request.body); const result = await MediaHelpers.uploadMedia(user, file, ctx.request.body)
ctx.body = convertAttachment(data.data as MastodonEntity.Attachment); .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 = 500;
ctx.body = e.response.data; ctx.body = { error: e.message };
} }
}); });
} }

View file

@ -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<Packed<"DriveFile">> {
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));
}
}