mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
fix: use multipart middleware
This commit is contained in:
1 parent
5fb177ee72
commit
7516eb4bde
2 files changed
+7
-23
No files matched your search
@@ -1,15 +1,14 @@
|
||||
import Router from "koa-router";
|
||||
import { bytesToHumanReadable } from "@shared/utils/files";
|
||||
import env from "@server/env";
|
||||
import { AuthenticationError, InvalidRequestError } from "@server/errors";
|
||||
import { AuthenticationError } from "@server/errors";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import multipart from "@server/middlewares/multipart";
|
||||
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
||||
import validate from "@server/middlewares/validate";
|
||||
import { Attachment } from "@server/models";
|
||||
import { authorize } from "@server/policies";
|
||||
import { APIContext } from "@server/types";
|
||||
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
|
||||
import { getFileFromRequest } from "@server/utils/koa";
|
||||
import { createRootDirForLocalStorage } from "../utils";
|
||||
import * as T from "./schema";
|
||||
|
||||
@@ -22,28 +21,11 @@ router.post(
|
||||
rateLimiter(RateLimiterStrategy.TenPerMinute),
|
||||
auth(),
|
||||
validate(T.FilesCreateSchema),
|
||||
multipart({ maximumFileSize: env.FILE_STORAGE_UPLOAD_MAX_SIZE }),
|
||||
async (ctx: APIContext<T.FilesCreateReq>) => {
|
||||
if (!ctx.is("multipart/form-data")) {
|
||||
throw InvalidRequestError("Request type must be multipart/form-data");
|
||||
}
|
||||
const file = getFileFromRequest(ctx.request);
|
||||
if (!file) {
|
||||
throw InvalidRequestError("Request must include a file parameter");
|
||||
}
|
||||
|
||||
if (
|
||||
env.FILE_STORAGE_UPLOAD_MAX_SIZE &&
|
||||
file.size > env.FILE_STORAGE_UPLOAD_MAX_SIZE
|
||||
) {
|
||||
throw InvalidRequestError(
|
||||
`The selected file was larger than the ${bytesToHumanReadable(
|
||||
env.FILE_STORAGE_UPLOAD_MAX_SIZE
|
||||
)} maximum size`
|
||||
);
|
||||
}
|
||||
|
||||
const { key } = ctx.input.body;
|
||||
const actor = ctx.state.auth.user;
|
||||
const { key } = ctx.input.body;
|
||||
const file = ctx.input.file;
|
||||
|
||||
const attachment = await Attachment.findByKey(key);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import formidable from "formidable";
|
||||
import isEmpty from "lodash/isEmpty";
|
||||
import { z } from "zod";
|
||||
import { ValidateKey } from "@server/validation";
|
||||
@@ -9,6 +10,7 @@ export const FilesCreateSchema = z.object({
|
||||
.refine(ValidateKey.isValid, { message: ValidateKey.message })
|
||||
.transform(ValidateKey.sanitize),
|
||||
}),
|
||||
file: z.custom<formidable.File>(),
|
||||
});
|
||||
|
||||
export type FilesCreateReq = z.infer<typeof FilesCreateSchema>;
|
||||
|
||||
Reference in new issue
Block a user