move file upload to new router

This commit is contained in:
CutestNekoAqua 2023-02-23 17:02:02 +01:00
parent cc44cc9da7
commit 1728419130
2 changed files with 46 additions and 59 deletions

View File

@ -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());

View File

@ -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) => {