[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 { 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 };
}
});
}

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));
}
}