Files
delivery-desk/api/upload.ts
yuzhe b0c498fbb6 feat(auth): 添加认证模块和图片批注功能(项目初始化)
- 实现用户登录、登出、密码修改等认证功能
- 添加会话管理和权限控制中间件
- 创建图片批注组件和相关API路由
- 实现批注的增删改查功能
- 添加Docker和Git忽略配置文件
- 创建系统架构文档和开发约定说明
- 集成认证模块到前端应用路由中
2026-07-21 15:28:55 +08:00

52 lines
1.2 KiB
TypeScript

/**
* multer 文件上传配置
*/
import multer from 'multer';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const uploadsDir = path.resolve(__dirname, '..', 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
const storage = multer.diskStorage({
destination: (_req, _file, cb) => {
cb(null, uploadsDir);
},
filename: (_req, file, cb) => {
const ext = path.extname(file.originalname).toLowerCase() || '.jpg';
const unique = `${Date.now()}_${Math.round(Math.random() * 1e6)}${ext}`;
cb(null, unique);
},
});
const allowedMime = new Set([
'image/jpeg',
'image/png',
'image/gif',
'image/webp',
'image/avif',
]);
export const upload = multer({
storage,
limits: {
fileSize: 20 * 1024 * 1024, // 20MB / 单图
files: 30, // 最多 30 张
},
fileFilter: (_req, file, cb) => {
if (allowedMime.has(file.mimetype)) {
cb(null, true);
} else {
cb(new Error(`不支持的文件类型: ${file.mimetype}`));
}
},
});
export const UPLOADS_DIR = uploadsDir;