mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-21 09:27:31 -07:00
[mastodon-client] Replace koa-multer with koa-body
This commit is contained in:
parent
60f314cb87
commit
75f6732928
8 changed files with 24 additions and 15 deletions
1
.pnp.cjs
generated
1
.pnp.cjs
generated
|
@ -14389,6 +14389,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
|
|||
["@types/cbor", "npm:6.0.0"],\
|
||||
["@types/escape-regexp", "npm:0.0.1"],\
|
||||
["@types/fluent-ffmpeg", "npm:2.1.21"],\
|
||||
["@types/formidable", "npm:2.0.6"],\
|
||||
["@types/js-yaml", "npm:4.0.5"],\
|
||||
["@types/jsdom", "npm:21.1.1"],\
|
||||
["@types/jsonld", "npm:1.5.9"],\
|
||||
|
|
|
@ -21,7 +21,8 @@
|
|||
},
|
||||
"optionalDependencies": {
|
||||
"@swc/core-android-arm64": "1.3.11",
|
||||
"@tensorflow/tfjs-node": "3.21.1"
|
||||
"@tensorflow/tfjs-node": "3.21.1",
|
||||
"@types/formidable": "^2.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@bull-board/api": "5.6.0",
|
||||
|
|
|
@ -144,3 +144,7 @@ export function toArray<T>(x: T | T[] | undefined): T[] {
|
|||
export function toSingle<T>(x: T | T[] | undefined): T | undefined {
|
||||
return Array.isArray(x) ? x[0] : x;
|
||||
}
|
||||
|
||||
export function toSingleLast<T>(x: T | T[] | undefined): T | undefined {
|
||||
return Array.isArray(x) ? x.at(-1) : x;
|
||||
}
|
||||
|
|
|
@ -43,7 +43,6 @@ 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
|
||||
|
@ -66,7 +65,7 @@ router.use(
|
|||
}),
|
||||
);
|
||||
|
||||
setupMastodonApi(mastoRouter, mastoFileRouter, upload);
|
||||
setupMastodonApi(mastoRouter);
|
||||
|
||||
/**
|
||||
* Register endpoint handlers
|
||||
|
@ -147,7 +146,6 @@ errorRouter.all("(.*)", async (ctx) => {
|
|||
});
|
||||
|
||||
// Register router
|
||||
app.use(mastoFileRouter.routes());
|
||||
app.use(mastoRouter.routes());
|
||||
app.use(mastoRouter.allowedMethods());
|
||||
app.use(router.routes());
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import Router from "@koa/router";
|
||||
import { convertId, IdType } from "@/misc/convert-id.js";
|
||||
import { convertAttachmentId } from "@/server/api/mastodon/converters.js";
|
||||
import multer from "@koa/multer";
|
||||
import authenticate from "@/server/api/authenticate.js";
|
||||
import { MediaHelpers } from "@/server/api/mastodon/helpers/media.js";
|
||||
import { FileConverter } from "@/server/api/mastodon/converters/file.js";
|
||||
import { Files } from "formidable";
|
||||
import { toSingleLast } from "@/prelude/array.js";
|
||||
|
||||
export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: multer.Instance): void {
|
||||
export function setupEndpointsMedia(router: Router): void {
|
||||
router.get<{ Params: { id: string } }>("/v1/media/:id", async (ctx) => {
|
||||
try {
|
||||
const auth = await authenticate(ctx.headers.authorization, null);
|
||||
|
@ -63,7 +64,7 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload:
|
|||
}
|
||||
});
|
||||
|
||||
fileRouter.post(["/v2/media", "/v1/media"], upload.single("file"), async (ctx) => {
|
||||
router.post(["/v2/media", "/v1/media"], async (ctx) => {
|
||||
try {
|
||||
const auth = await authenticate(ctx.headers.authorization, null);
|
||||
const user = auth[0] ?? null;
|
||||
|
@ -73,7 +74,9 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload:
|
|||
return;
|
||||
}
|
||||
|
||||
const file = await ctx.file;
|
||||
//FIXME: why do we have to cast this to any first?
|
||||
const files = (ctx.request as any).files as Files;
|
||||
const file = toSingleLast(files['file']);
|
||||
if (!file) {
|
||||
ctx.body = {error: "No image"};
|
||||
ctx.status = 400;
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
import { addFile } from "@/services/drive/add-file.js";
|
||||
import { ILocalUser } from "@/models/entities/user.js";
|
||||
import multer from "@koa/multer";
|
||||
import { DriveFiles } from "@/models/index.js";
|
||||
import { Packed } from "@/misc/schema.js";
|
||||
import { DriveFile } from "@/models/entities/drive-file.js";
|
||||
import { File } from "formidable";
|
||||
|
||||
export class MediaHelpers {
|
||||
public static async uploadMedia(user: ILocalUser, file: multer.File, body: any): Promise<Packed<"DriveFile">> {
|
||||
public static async uploadMedia(user: ILocalUser, file: File, body: any): Promise<Packed<"DriveFile">> {
|
||||
return await addFile({
|
||||
user: user,
|
||||
path: file.path,
|
||||
name: file.originalname !== null && file.originalname !== 'file' ? file.originalname : undefined,
|
||||
path: file.filepath,
|
||||
name: file.originalFilename !== null && file.originalFilename !== 'file' ? file.originalFilename : undefined,
|
||||
comment: body?.description ?? undefined,
|
||||
sensitive: false, //FIXME: this needs to be updated on from composing a post with the media attached
|
||||
})
|
||||
|
|
|
@ -9,10 +9,9 @@ import { setupEndpointsSearch } from "./endpoints/search.js";
|
|||
import { setupEndpointsMedia } from "@/server/api/mastodon/endpoints/media.js";
|
||||
import { setupEndpointsMisc } from "@/server/api/mastodon/endpoints/misc.js";
|
||||
import { HttpMethodEnum, koaBody } from "koa-body";
|
||||
import multer from "@koa/multer";
|
||||
import { setupEndpointsList } from "@/server/api/mastodon/endpoints/list.js";
|
||||
|
||||
export function setupMastodonApi(router: Router, fileRouter: Router, upload: multer.Instance): void {
|
||||
export function setupMastodonApi(router: Router): void {
|
||||
router.use(
|
||||
koaBody({
|
||||
multipart: true,
|
||||
|
@ -39,7 +38,7 @@ export function setupMastodonApi(router: Router, fileRouter: Router, upload: mul
|
|||
setupEndpointsTimeline(router);
|
||||
setupEndpointsNotifications(router);
|
||||
setupEndpointsSearch(router);
|
||||
setupEndpointsMedia(router, fileRouter, upload);
|
||||
setupEndpointsMedia(router);
|
||||
setupEndpointsList(router);
|
||||
setupEndpointsMisc(router);
|
||||
}
|
||||
|
|
|
@ -5404,6 +5404,7 @@ __metadata:
|
|||
"@types/cbor": 6.0.0
|
||||
"@types/escape-regexp": 0.0.1
|
||||
"@types/fluent-ffmpeg": 2.1.21
|
||||
"@types/formidable": ^2.0.5
|
||||
"@types/js-yaml": 4.0.5
|
||||
"@types/jsdom": 21.1.1
|
||||
"@types/jsonld": 1.5.9
|
||||
|
@ -5562,6 +5563,8 @@ __metadata:
|
|||
optional: true
|
||||
"@tensorflow/tfjs-node":
|
||||
optional: true
|
||||
"@types/formidable":
|
||||
optional: true
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
|
|
Loading…
Reference in a new issue