use multer upload instead

This commit is contained in:
CutestNekoAqua 2023-02-23 16:55:38 +01:00
parent 2c6368afc4
commit 2dbbd1d39d

View File

@ -295,21 +295,18 @@ export function apiStatusMastodon(router: Router): void {
} }
}, },
); );
router.post("/v1/media", async (ctx) => { router.post("/v1/media", upload.single("file"), async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization; const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
let multipartData = await ctx.request.files; let multipartData = await ctx.request.file;
if (!multipartData) { if (!multipartData) {
ctx.body = { error: "No image" }; ctx.body = { error: "No image" };
ctx.status = 401; ctx.status = 401;
return; return;
} }
if ((multipartData as any).file) { const image = fs.readFileSync((multipartData).path);
multipartData = (multipartData as any).file;
}
const image = fs.readFileSync((multipartData as any).path);
const data = await client.uploadMedia(image); const data = await client.uploadMedia(image);
ctx.body = data.data; ctx.body = data.data;
} catch (e: any) { } catch (e: any) {
@ -318,20 +315,18 @@ export function apiStatusMastodon(router: Router): void {
ctx.body = e.response.data; ctx.body = e.response.data;
} }
}); });
router.post("/v2/media", async (ctx) => { router.post("/v2/media", upload.single("file"), async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization; const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const multipartData = await ctx.file; let multipartData = await ctx.request.file;
if (!multipartData) { if (!multipartData) {
ctx.body = { error: "No image" }; ctx.body = { error: "No image" };
ctx.status = 401; ctx.status = 401;
return; return;
} }
const [path] = await createTemp(); const image = fs.readFileSync((multipartData).path);
await pump(multipartData.buffer, fs.createWriteStream(path));
const image = fs.readFileSync(path);
const data = await client.uploadMedia(image); const data = await client.uploadMedia(image);
ctx.body = data.data; ctx.body = data.data;
} catch (e: any) { } catch (e: any) {
@ -340,6 +335,7 @@ export function apiStatusMastodon(router: Router): void {
ctx.body = e.response.data; ctx.body = e.response.data;
} }
}); });
});
router.get<{ Params: { id: string } }>( router.get<{ Params: { id: string } }>(
"/v1/media/:id", "/v1/media/:id",
async (ctx) => { async (ctx) => {