[mastodon-client] GET /media/:id
This commit is contained in:
parent
9b223abeda
commit
a2dce0fa85
2 changed files with 25 additions and 8 deletions
|
|
@ -9,17 +9,29 @@ 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) => {
|
||||||
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
||||||
const accessTokens = ctx.headers.authorization;
|
|
||||||
const client = getClient(BASE_URL, accessTokens);
|
|
||||||
try {
|
try {
|
||||||
const data = await client.getMedia(
|
const auth = await authenticate(ctx.headers.authorization, null);
|
||||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
const user = auth[0] ?? null;
|
||||||
);
|
|
||||||
ctx.body = convertAttachment(data.data);
|
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) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
ctx.status = 401;
|
ctx.status = 500;
|
||||||
ctx.body = e.response.data;
|
ctx.body = e.response.data;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,9 @@ 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> {
|
||||||
|
return DriveFiles.findOneBy({id: id, userId: user.id})
|
||||||
|
.then(p => p ? DriveFiles.pack(p) : null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue