From d00547341e0f89d81ace55d53e72d0d83f13d472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Fri, 27 Mar 2026 22:16:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0hash=E8=AE=A1=E7=AE=97=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E6=97=B6=E4=BD=BF=E7=94=A8=E4=BC=AA=E5=93=88=E5=B8=8C=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=A4=87=E9=80=89=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apps/web-antd/src/utils/oss-upload.ts | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/ecom-dmb-frontend/apps/web-antd/src/utils/oss-upload.ts b/ecom-dmb-frontend/apps/web-antd/src/utils/oss-upload.ts index 82b84713..005cbda9 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/utils/oss-upload.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/utils/oss-upload.ts @@ -24,22 +24,24 @@ export interface UploadOptions { * @returns 返回文件的SHA-256哈希值(十六进制字符串) */ async function calculateFileHash(file: File): Promise { - const arrayBuffer = await file.arrayBuffer(); - // 兼容不同环境的 crypto.subtle - const cryptoSubtle = typeof crypto !== 'undefined' && crypto.subtle - ? crypto.subtle - : (window as any).crypto?.subtle; - - if (!cryptoSubtle) { - throw new Error('当前环境不支持文件加密计算,请使用现代浏览器'); + // 尝试使用标准 Web Crypto API + if (typeof crypto !== 'undefined' && crypto.subtle) { + try { + const arrayBuffer = await file.arrayBuffer(); + const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer); + const hashArray = [...new Uint8Array(hashBuffer)]; + return hashArray + .map((b) => b.toString(16).padStart(2, '0')) + .join(''); + } catch { + // Fall through to fallback + } } - - const hashBuffer = await cryptoSubtle.digest('SHA-256', arrayBuffer); - const hashArray = [...new Uint8Array(hashBuffer)]; - const hashHex = hashArray - .map((b) => b.toString(16).padStart(2, '0')) - .join(''); - return hashHex; + + // 备选方案:使用文件名+大小+最后修改时间作为伪哈希 + // 这种方式不能用于文件去重,但可以保证同一文件每次生成相同的标识 + const pseudoHash = `${file.name}_${file.size}_${file.lastModified}`; + return pseudoHash; } /**