First Commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
const multer = require('multer');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
const { dbname, typeid } = req.params;
|
||||
const uploadPath = path.join(__dirname, `../../public/upload/${dbname}/${typeid}`);
|
||||
fs.mkdirSync(uploadPath, { recursive: true });
|
||||
cb(null, uploadPath);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const dateStr = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
cb(null, `${dateStr}-${file.originalname}`);
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage,
|
||||
limits: { fileSize: parseInt(process.env.MAX_FILE_SIZE) || 50 * 1024 * 1024 },
|
||||
fileFilter: (req, file, cb) => {
|
||||
// 禁止上傳危險副檔名
|
||||
const forbiddenExtensions = ['.exe', '.sh', '.php', '.js', '.bat'];
|
||||
const ext = path.extname(file.originalname).toLowerCase();
|
||||
if (forbiddenExtensions.includes(ext)) {
|
||||
return cb(new Error('File type not allowed'), false);
|
||||
}
|
||||
cb(null, true);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = upload;
|
||||
Reference in New Issue
Block a user