mirror of
https://iceshrimp.dev/crimekillz/iceshrimp-161sh.git
synced 2024-11-25 13:39:06 +01:00
[mastodon-client] GET /media/:id
This commit is contained in:
parent
9b223abeda
commit
a2dce0fa85
@ -9,17 +9,29 @@ 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) => {
|
||||
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
||||
const accessTokens = ctx.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const data = await client.getMedia(
|
||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
||||
);
|
||||
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 attachment = FileConverter.encode(file);
|
||||
ctx.body = convertAttachment(attachment);
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
ctx.status = 401;
|
||||
ctx.status = 500;
|
||||
ctx.body = e.response.data;
|
||||
}
|
||||
});
|
||||
|
@ -15,4 +15,9 @@ 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})
|
||||
.then(p => p ? DriveFiles.pack(p) : null);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user