From 5e43c5fc4c1be799ecf03b3104c52f598283188a Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sun, 21 Dec 2025 13:48:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20busboy=20=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=20multipart/form-data=EF=BC=8C=E6=B7=BB=E5=8A=A0=20pr?= =?UTF-8?q?eservePath=20=E9=80=89=E9=A1=B9=E4=BB=A5=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes-simple/code/upload.ts | 4 ++-- src/routes-simple/handle-request.ts | 4 ++-- src/routes-simple/resources/chunk.ts | 4 ++-- src/routes-simple/resources/upload.ts | 6 ++---- src/utils/sleep.ts | 3 +++ 5 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 src/utils/sleep.ts diff --git a/src/routes-simple/code/upload.ts b/src/routes-simple/code/upload.ts index b646d9c..48ae9af 100644 --- a/src/routes-simple/code/upload.ts +++ b/src/routes-simple/code/upload.ts @@ -20,7 +20,7 @@ router.post('/api/micro-app/upload', async (req, res) => { if (!tokenUser) return; // 使用 busboy 解析 multipart/form-data - const busboy = Busboy({ headers: req.headers }); + const busboy = Busboy({ headers: req.headers, preservePath: true }); const fields: any = {}; let file: any = null; let filePromise: Promise | null = null; @@ -33,7 +33,7 @@ router.post('/api/micro-app/upload', async (req, res) => { busboy.on('file', (fieldname, fileStream, info) => { const { filename, encoding, mimeType } = info; - const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`); + const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`); const writeStream = createWriteStream(tempPath); const hash = crypto.createHash('md5'); let size = 0; diff --git a/src/routes-simple/handle-request.ts b/src/routes-simple/handle-request.ts index 67564ca..a3f8830 100644 --- a/src/routes-simple/handle-request.ts +++ b/src/routes-simple/handle-request.ts @@ -28,7 +28,7 @@ router.post('/api/app/upload', async (req, res) => { if (!tokenUser) return; // 使用 busboy 解析 multipart/form-data - const busboy = Busboy({ headers: req.headers }); + const busboy = Busboy({ headers: req.headers, preservePath: true }); const fields: any = {}; const files: any = []; const filePromises: Promise[] = []; @@ -41,7 +41,7 @@ router.post('/api/app/upload', async (req, res) => { busboy.on('file', (fieldname, fileStream, info) => { const { filename, encoding, mimeType } = info; - const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`); + const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`); const writeStream = createWriteStream(tempPath); const filePromise = new Promise((resolve, reject) => { diff --git a/src/routes-simple/resources/chunk.ts b/src/routes-simple/resources/chunk.ts index 604838c..4bad8e5 100644 --- a/src/routes-simple/resources/chunk.ts +++ b/src/routes-simple/resources/chunk.ts @@ -35,7 +35,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => { } // 使用 busboy 解析 multipart/form-data - const busboy = Busboy({ headers: req.headers }); + const busboy = Busboy({ headers: req.headers, preservePath: true }); const fields: any = {}; let file: any = null; let tempPath = ''; @@ -47,7 +47,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => { busboy.on('file', (fieldname, fileStream, info) => { const { filename, encoding, mimeType } = info; - tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`); + tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`); const writeStream = createWriteStream(tempPath); filePromise = new Promise((resolve, reject) => { diff --git a/src/routes-simple/resources/upload.ts b/src/routes-simple/resources/upload.ts index a1d91f5..193ac8e 100644 --- a/src/routes-simple/resources/upload.ts +++ b/src/routes-simple/resources/upload.ts @@ -107,22 +107,20 @@ router.post('/api/s1/resources/upload', async (req, res) => { const meta = parseIfJson(url.searchParams.get('meta')); const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles'); // 使用 busboy 解析 multipart/form-data - const busboy = Busboy({ headers: req.headers }); + const busboy = Busboy({ headers: req.headers, preservePath: true }); const fields: any = {}; const files: any[] = []; const filePromises: Promise[] = []; let bytesReceived = 0; let bytesExpected = parseInt(req.headers['content-length'] || '0'); - busboy.on('field', (fieldname, value) => { fields[fieldname] = value; }); busboy.on('file', (fieldname, fileStream, info) => { const { filename, encoding, mimeType } = info; - const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`); + const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`); const writeStream = createWriteStream(tempPath); - const filePromise = new Promise((resolve, reject) => { fileStream.on('data', (chunk) => { bytesReceived += chunk.length; diff --git a/src/utils/sleep.ts b/src/utils/sleep.ts new file mode 100644 index 0000000..461b12f --- /dev/null +++ b/src/utils/sleep.ts @@ -0,0 +1,3 @@ +export const sleep = (ms: number = 1000) => { + return new Promise((resolve) => setTimeout(resolve, ms)); +} \ No newline at end of file