From 2e685ceccb505a58a509502ff359f14e7e6e6c2a Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 30 May 2026 19:03:13 +0800 Subject: [PATCH] perf(workflow): optimize getMimeType to read first 4100 bytes instead of whole file --- src/workflow/activities/db.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/workflow/activities/db.ts b/src/workflow/activities/db.ts index 142d4e11..507aebc3 100644 --- a/src/workflow/activities/db.ts +++ b/src/workflow/activities/db.ts @@ -630,9 +630,15 @@ export async function updateWorkflowTaskActivity(params: UpdateWorkflowTaskParam function getMimeType(filePath: string): string { try { - const buffer = fs.readFileSync(filePath) - const detected = detectSupportedMimeType(new Uint8Array(buffer)) - if (detected) return detected + const fd = fs.openSync(filePath, 'r') + try { + const buffer = Buffer.alloc(4100) + const bytesRead = fs.readSync(fd, buffer, 0, 4100, 0) + const detected = detectSupportedMimeType(new Uint8Array(buffer.subarray(0, bytesRead))) + if (detected) return detected + } finally { + fs.closeSync(fd) + } } catch { /* Ignore S3 detection errors and fallback to extension mapping */ }