refactor(server): 优化文件上传配置和MD5计算逻辑

- 移除cloud.js中的异步Promise包装,直接同步计算MD5哈希值
- 在server.js中提取上传大小限制为常量配置
- 统一使用字节单位常量管理文件上传大小限制
- 简化代码结构,提高可读性和维护性
This commit is contained in:
LaoShui 2026-02-18 17:02:15 +08:00
parent 33ccc83615
commit 8951e32a0e
2 changed files with 7 additions and 8 deletions

View File

@ -69,11 +69,7 @@ module.exports = async (query, request) => {
}
} else {
if (!fileMd5) {
fileMd5 = await new Promise((resolve) => {
setImmediate(() => {
resolve(crypto.createHash('md5').update(query.songFile.data).digest('hex'))
})
})
fileMd5 = crypto.createHash('md5').update(query.songFile.data).digest('hex')
}
fileSize = query.songFile.data.byteLength
}

View File

@ -178,12 +178,15 @@ async function consturctServer(moduleDefs) {
/**
* Body Parser and File Upload
*/
app.use(express.json({ limit: '500mb' }))
app.use(express.urlencoded({ extended: false, limit: '500mb' }))
const MAX_UPLOAD_SIZE_MB = 500
const MAX_UPLOAD_SIZE_BYTES = MAX_UPLOAD_SIZE_MB * 1024 * 1024
app.use(express.json({ limit: `${MAX_UPLOAD_SIZE_MB}mb` }))
app.use(express.urlencoded({ extended: false, limit: `${MAX_UPLOAD_SIZE_MB}mb` }))
app.use(fileUpload({
limits: {
fileSize: 500 * 1024 * 1024
fileSize: MAX_UPLOAD_SIZE_BYTES
},
useTempFiles: true,
tempFileDir: require('os').tmpdir(),