diff --git a/front/components/Home/File/FileUploadProgressView/index.vue b/front/components/Home/File/FileUploadProgressView/index.vue index a0dc096..cd8a334 100644 --- a/front/components/Home/File/FileUploadProgressView/index.vue +++ b/front/components/Home/File/FileUploadProgressView/index.vue @@ -6,6 +6,7 @@ import getFileSize from '~/lib/getFileSize' import { cx } from 'class-variance-authority' import asyncWorker from '@/lib/asyncWorker' import calcFileHashWorker from '@/lib/calcFileHashWorker?worker' +import { detectSupportedEngines } from '@/lib/calcFileHash' import { clamp, get, isEmpty, isNumber, sample, shuffle, throttle, times } from 'lodash-es' import { nanoid } from 'nanoid' import { toast } from 'vue-sonner' @@ -143,8 +144,14 @@ const handleHash = async (fileId: string) => { const uploadfile = uploadfiles.value.find((item) => item.fileId === fileId) if (!uploadfile?.file) return uploadfile.procressType = 'hash' - const engine = uploadfile.file.size >= LARGE_FILE_THRESHOLD ? 'wasm' : 'native' - const res = await asyncWorker(calcFileHashWorker, { data: { file: uploadfile.file, engine } }) + const supportedEngines = detectSupportedEngines() + if (supportedEngines.length === 0) { + throw new Error(t('page.progress.file.hashEngineNotFound')) + } + const preferredEngine = uploadfile.file.size >= LARGE_FILE_THRESHOLD ? 'wasm' : 'native' + const res = await asyncWorker(calcFileHashWorker, { + data: { file: uploadfile.file, engine: supportedEngines.includes(preferredEngine) ? preferredEngine : supportedEngines?.[0] }, + }) const { hash } = res?.data || {} uploadfile.hash = hash } diff --git a/front/i18n/locales/de.json b/front/i18n/locales/de.json index f81062f..b900f76 100644 --- a/front/i18n/locales/de.json +++ b/front/i18n/locales/de.json @@ -132,6 +132,7 @@ "chunkUploadRetry": "Block {1} der Datei {0} konnte nicht hochgeladen werden, wir versuchen es später erneut", "fileUploadFailed": "Der Upload der Datei {0} ist fehlgeschlagen, bitte versuchen Sie es erneut", "uploadFailed": "Upload fehlgeschlagen", + "hashEngineNotFound": "In der aktuellen Umgebung ist keine Engine zur Dateihash-Berechnung verfügbar", "processing": { "hash": "Hash wird berechnet...", "create": "Upload wird initialisiert...", diff --git a/front/i18n/locales/en.json b/front/i18n/locales/en.json index 36b51c3..85aa08c 100644 --- a/front/i18n/locales/en.json +++ b/front/i18n/locales/en.json @@ -132,6 +132,7 @@ "chunkUploadRetry": "Chunk {1} of file {0} failed to upload, we will try again later", "fileUploadFailed": "File {0} upload failed, please try again", "uploadFailed": "Upload Failed", + "hashEngineNotFound": "No file hash engine is available in the current environment", "processing": { "hash": "Calculating Hash...", "create": "Initializing Upload...", diff --git a/front/i18n/locales/fr.json b/front/i18n/locales/fr.json index 6b0ea48..231b964 100644 --- a/front/i18n/locales/fr.json +++ b/front/i18n/locales/fr.json @@ -132,6 +132,7 @@ "chunkUploadRetry": "Le bloc {1} du fichier {0} n'a pas pu être téléversé, une nouvelle tentative sera effectuée plus tard", "fileUploadFailed": "Le téléversement du fichier {0} a échoué, veuillez réessayer", "uploadFailed": "Échec du téléversement", + "hashEngineNotFound": "Aucun moteur de calcul de hash de fichier n'est disponible dans l'environnement actuel", "processing": { "hash": "Calcul du hash...", "create": "Initialisation du téléversement...", diff --git a/front/i18n/locales/ja.json b/front/i18n/locales/ja.json index 171154b..52e16dc 100644 --- a/front/i18n/locales/ja.json +++ b/front/i18n/locales/ja.json @@ -132,6 +132,7 @@ "chunkUploadRetry": "ファイル {0} のチャンク {1} のアップロードに失敗しました。後でもう一度試します", "fileUploadFailed": "ファイル {0} のアップロードに失敗しました。もう一度お試しください", "uploadFailed": "アップロード失敗", + "hashEngineNotFound": "現在の環境ではファイルハッシュ計算エンジンを利用できません", "processing": { "hash": "ハッシュを計算中...", "create": "アップロードを初期化中...", diff --git a/front/i18n/locales/ko.json b/front/i18n/locales/ko.json index f798a19..d59b64d 100644 --- a/front/i18n/locales/ko.json +++ b/front/i18n/locales/ko.json @@ -132,6 +132,7 @@ "chunkUploadRetry": "파일 {0}의 청크 {1} 업로드에 실패했습니다. 잠시 후 다시 시도합니다", "fileUploadFailed": "파일 {0} 업로드에 실패했습니다. 다시 시도해 주세요", "uploadFailed": "업로드 실패", + "hashEngineNotFound": "현재 환경에서는 파일 해시 계산 엔진을 사용할 수 없습니다", "processing": { "hash": "해시 계산 중...", "create": "업로드 초기화 중...", diff --git a/front/i18n/locales/zh-CN.json b/front/i18n/locales/zh-CN.json index e68bd90..1656698 100644 --- a/front/i18n/locales/zh-CN.json +++ b/front/i18n/locales/zh-CN.json @@ -132,6 +132,7 @@ "chunkUploadRetry": "文件 {0} 的{1}分块上传失败, 我们将在稍后再次尝试上传", "fileUploadFailed": "文件{0}上传失败, 请重试", "uploadFailed": "上传失败", + "hashEngineNotFound": "当前环境不支持文件哈希计算引擎", "processing": { "hash": "计算Hash...", "create": "初始化上传...", diff --git a/front/i18n/locales/zh-TW.json b/front/i18n/locales/zh-TW.json index 6b00088..7103d27 100644 --- a/front/i18n/locales/zh-TW.json +++ b/front/i18n/locales/zh-TW.json @@ -132,6 +132,7 @@ "chunkUploadRetry": "檔案 {0} 的第 {1} 個分塊上傳失敗,稍後將再次嘗試", "fileUploadFailed": "檔案 {0} 上傳失敗,請重試", "uploadFailed": "上傳失敗", + "hashEngineNotFound": "目前環境不支援檔案雜湊計算引擎", "processing": { "hash": "計算雜湊中...", "create": "初始化上傳中...", diff --git a/front/lib/calcFileHash.ts b/front/lib/calcFileHash.ts index 695dd49..27ceb34 100644 --- a/front/lib/calcFileHash.ts +++ b/front/lib/calcFileHash.ts @@ -35,4 +35,15 @@ export const calcNativeHash = async (buffer: BufferSource) => { .join('') } +export const detectSupportedEngines = (): ('native' | 'wasm')[] => { + const engines: ('native' | 'wasm')[] = [] + if (typeof crypto !== 'undefined' && typeof crypto.subtle !== 'undefined') { + engines.push('native') + } + if (typeof WebAssembly !== 'undefined' && typeof WebAssembly.instantiate === 'function') { + engines.push('wasm') + } + return engines +} + export default calcFileHash