This commit is contained in:
luk.lu
2021-10-20 14:44:38 +08:00
commit 97563fdf30
4 changed files with 85 additions and 0 deletions

39
index.js Normal file
View File

@@ -0,0 +1,39 @@
const multer = require('multer')
const path = require('path')
const crypto = require('crypto')
const wo = global.wo
module.exports = {
MulterStore: multer({
// dest:'./File/', // 这样,不能自定义文件名。
storage: Multer.diskStorage({
destination (req, file, cb) {
// 如果直接提供字符串Multer会负责创建该目录。如果提供函数你要负责确保该目录存在。
const folder = wo?.envi?.uploadroot // 目录是相对于本应用的入口js的即相对于 server.js 的位置。
cb(null, folder)
},
filename (req, file, cb) {
// 注意req.body 也许还没有信息因为这取决于客户端发送body和file的顺序。必要的信息请从 req.headers 传递。
const fileNameExtension = path.extname(file.originalname)
const filename = `${Date.now()}_${crypto.randomBytes(32).toString('hex')}${fileNameExtension}`
cb(null, filename)
},
}),
// fileFilter:function(req, file, cb) {},
limits: { fileSize: 10485760 },
}).single('file'),
api: {
receiveFile () {
const file = wo._req?.file
if (file?.path) {
file.path = file.path.replace('\\', '/')
return Object.assign(file, { _state: 'SUCCESS' })
} else {
return { _state: 'BACKEND_FAIL_FILE_NOT_RECEIVED' }
}
}
}
}