From 8951e32a0eb2fabc3e067b4f7bbff8d16ea9f89f Mon Sep 17 00:00:00 2001 From: LaoShui <79132480+laoshuikaixue@users.noreply.github.com> Date: Wed, 18 Feb 2026 17:02:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(server):=20=E4=BC=98=E5=8C=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E9=85=8D=E7=BD=AE=E5=92=8CMD5?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除cloud.js中的异步Promise包装,直接同步计算MD5哈希值 - 在server.js中提取上传大小限制为常量配置 - 统一使用字节单位常量管理文件上传大小限制 - 简化代码结构,提高可读性和维护性 --- module/cloud.js | 6 +----- server.js | 9 ++++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/module/cloud.js b/module/cloud.js index 2f80fc8..bb5f157 100644 --- a/module/cloud.js +++ b/module/cloud.js @@ -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 } diff --git a/server.js b/server.js index a02d4d8..1bc6e86 100644 --- a/server.js +++ b/server.js @@ -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(),