From 50687ef209035589ef6d6b3051dac7a98013f2fe Mon Sep 17 00:00:00 2001 From: CutestNekoAqua Date: Thu, 23 Feb 2023 17:02:02 +0100 Subject: [PATCH] move file upload to new router --- packages/backend/src/server/api/index.ts | 46 ++++++++++++++- .../server/api/mastodon/endpoints/status.ts | 59 +------------------ 2 files changed, 46 insertions(+), 59 deletions(-) diff --git a/packages/backend/src/server/api/index.ts b/packages/backend/src/server/api/index.ts index 0f58d3acf..9b6c62924 100644 --- a/packages/backend/src/server/api/index.ts +++ b/packages/backend/src/server/api/index.ts @@ -7,7 +7,7 @@ import Router from "@koa/router"; import multer from "@koa/multer"; import bodyParser from "koa-bodyparser"; import cors from "@koa/cors"; -import { apiMastodonCompatible } from "./mastodon/ApiMastodonCompatibleService.js"; +import { apiMastodonCompatible, getClient } from "./mastodon/ApiMastodonCompatibleService.js"; import { Instances, AccessTokens, Users } from "@/models/index.js"; import config from "@/config/index.js"; import endpoints from "./endpoints.js"; @@ -39,6 +39,7 @@ app.use(async (ctx, next) => { // Init router const router = new Router(); const mastoRouter = new Router(); +const mastoFileRouter = new Router(); const errorRouter = new Router(); // Init multer instance @@ -68,6 +69,48 @@ mastoRouter.use( }), ); + +mastoFileRouter.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.file; + if (!multipartData) { + ctx.body = { error: "No image" }; + ctx.status = 401; + return; + } + const image = fs.readFileSync((multipartData).path); + const data = await client.uploadMedia(image); + ctx.body = data.data; + } catch (e: any) { + console.error(e); + ctx.status = 401; + ctx.body = e.response.data; + } +}); +mastoFileRouter.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 { + let multipartData = await ctx.request.file; + if (!multipartData) { + ctx.body = { error: "No image" }; + ctx.status = 401; + return; + } + const image = fs.readFileSync((multipartData).path); + const data = await client.uploadMedia(image); + ctx.body = data.data; + } catch (e: any) { + console.error(e); + ctx.status = 401; + ctx.body = e.response.data; + } +}); + mastoRouter.use(async (ctx, next) => { if (ctx.request.query) { if (!ctx.request.body || Object.keys(ctx.request.body).length === 0) { @@ -170,6 +213,7 @@ errorRouter.all("(.*)", async (ctx) => { }); // Register router +app.use(mastoFileRouter.routes()); app.use(mastoRouter.routes()); app.use(mastoRouter.allowedMethods()); app.use(router.routes()); diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index c86132364..3981a1781 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -1,24 +1,7 @@ import Router from "@koa/router"; -import megalodon, { MegalodonInterface } from "@calckey/megalodon"; import { getClient } from "../ApiMastodonCompatibleService.js"; -import fs from "fs"; -import { pipeline } from "node:stream"; -import { promisify } from "node:util"; -import { createTemp } from "@/misc/create-temp.js"; -import config from "@/config/index.js"; -import multer from "@koa/multer"; -import { emojiRegex, emojiRegexAtStartToEnd } from "@/misc/emoji-regex.js"; +import { emojiRegexAtStartToEnd } from "@/misc/emoji-regex.js"; import axios from "axios"; -const pump = promisify(pipeline); - -// Init multer instance -const upload = multer({ - storage: multer.diskStorage({}), - limits: { - fileSize: config.maxFileSize || 262144000, - files: 1, - }, -}); export function apiStatusMastodon(router: Router): void { router.post("/v1/statuses", async (ctx) => { @@ -295,46 +278,6 @@ export function apiStatusMastodon(router: Router): void { } }, ); - 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.file; - if (!multipartData) { - ctx.body = { error: "No image" }; - ctx.status = 401; - return; - } - const image = fs.readFileSync((multipartData).path); - const data = await client.uploadMedia(image); - ctx.body = data.data; - } catch (e: any) { - console.error(e); - ctx.status = 401; - ctx.body = e.response.data; - } - }); - 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 { - let multipartData = await ctx.request.file; - if (!multipartData) { - ctx.body = { error: "No image" }; - ctx.status = 401; - return; - } - const image = fs.readFileSync((multipartData).path); - const data = await client.uploadMedia(image); - ctx.body = data.data; - } catch (e: any) { - console.error(e); - ctx.status = 401; - ctx.body = e.response.data; - } - }); router.get<{ Params: { id: string } }>( "/v1/media/:id", async (ctx) => {