mirror of
https://iceshrimp.dev/crimekillz/iceshrimp-161sh.git
synced 2024-11-25 13:39:06 +01:00
[mastodon-client] POST /{v1,v2}/media
This commit is contained in:
parent
366311a8b1
commit
9b223abeda
@ -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 };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
18
packages/backend/src/server/api/mastodon/helpers/media.ts
Normal file
18
packages/backend/src/server/api/mastodon/helpers/media.ts
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user