mirror of
https://github.com/willchen96/mike.git
synced 2026-07-02 22:01:00 +02:00
Add local repo contents
This commit is contained in:
parent
65739ef1ce
commit
d9690965b5
176 changed files with 68998 additions and 0 deletions
36
backend/src/lib/upload.ts
Normal file
36
backend/src/lib/upload.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import type { RequestHandler } from "express";
|
||||
import multer from "multer";
|
||||
|
||||
export const MAX_UPLOAD_SIZE_BYTES = 100 * 1024 * 1024;
|
||||
export const MAX_UPLOAD_SIZE_MB = Math.round(
|
||||
MAX_UPLOAD_SIZE_BYTES / (1024 * 1024),
|
||||
);
|
||||
|
||||
const memoryUpload = multer({
|
||||
storage: multer.memoryStorage(),
|
||||
limits: {
|
||||
fileSize: MAX_UPLOAD_SIZE_BYTES,
|
||||
files: 1,
|
||||
},
|
||||
});
|
||||
|
||||
export function singleFileUpload(fieldName: string): RequestHandler {
|
||||
return (req, res, next) => {
|
||||
memoryUpload.single(fieldName)(req, res, (err) => {
|
||||
if (!err) return next();
|
||||
|
||||
if (err instanceof multer.MulterError) {
|
||||
if (err.code === "LIMIT_FILE_SIZE") {
|
||||
return void res.status(413).json({
|
||||
detail: `File too large. Maximum size is ${MAX_UPLOAD_SIZE_MB} MB.`,
|
||||
});
|
||||
}
|
||||
return void res.status(400).json({
|
||||
detail: `Upload failed: ${err.message}`,
|
||||
});
|
||||
}
|
||||
|
||||
return next(err);
|
||||
});
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue