use multer upload instead

This commit is contained in:
CutestNekoAqua 2023-02-23 16:55:38 +01:00
parent 6b97e42a7f
commit b712b67df3

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