Spaces:
Sleeping
Sleeping
Download backend/src/lib/upload.ts from bardd/mike-backend: direct link, hf CLI and curl.
- Browser
- Download file 948 Bytes
-
https://huggingface.co/spaces/bardd/mike-backend/resolve/main/backend/src/lib/upload.ts
- Command line
-
hf download hf://spaces/bardd/mike-backend/backend/src/lib/upload.ts
-
curl -L -o upload.ts https://huggingface.co/spaces/bardd/mike-backend/resolve/main/backend/src/lib/upload.ts
948 Bytes
| 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); | |
| }); | |
| }; | |
| } | |