mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
feat(front): implement engine detection for file hash calculation and add error handling for unsupported environments
This commit is contained in:
@@ -6,6 +6,7 @@ import getFileSize from '~/lib/getFileSize'
|
|||||||
import { cx } from 'class-variance-authority'
|
import { cx } from 'class-variance-authority'
|
||||||
import asyncWorker from '@/lib/asyncWorker'
|
import asyncWorker from '@/lib/asyncWorker'
|
||||||
import calcFileHashWorker from '@/lib/calcFileHashWorker?worker'
|
import calcFileHashWorker from '@/lib/calcFileHashWorker?worker'
|
||||||
|
import { detectSupportedEngines } from '@/lib/calcFileHash'
|
||||||
import { clamp, get, isEmpty, isNumber, sample, shuffle, throttle, times } from 'lodash-es'
|
import { clamp, get, isEmpty, isNumber, sample, shuffle, throttle, times } from 'lodash-es'
|
||||||
import { nanoid } from 'nanoid'
|
import { nanoid } from 'nanoid'
|
||||||
import { toast } from 'vue-sonner'
|
import { toast } from 'vue-sonner'
|
||||||
@@ -143,8 +144,14 @@ const handleHash = async (fileId: string) => {
|
|||||||
const uploadfile = uploadfiles.value.find((item) => item.fileId === fileId)
|
const uploadfile = uploadfiles.value.find((item) => item.fileId === fileId)
|
||||||
if (!uploadfile?.file) return
|
if (!uploadfile?.file) return
|
||||||
uploadfile.procressType = 'hash'
|
uploadfile.procressType = 'hash'
|
||||||
const engine = uploadfile.file.size >= LARGE_FILE_THRESHOLD ? 'wasm' : 'native'
|
const supportedEngines = detectSupportedEngines()
|
||||||
const res = await asyncWorker(calcFileHashWorker, { data: { file: uploadfile.file, engine } })
|
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 || {}
|
const { hash } = res?.data || {}
|
||||||
uploadfile.hash = hash
|
uploadfile.hash = hash
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
"chunkUploadRetry": "Block {1} der Datei {0} konnte nicht hochgeladen werden, wir versuchen es später erneut",
|
"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",
|
"fileUploadFailed": "Der Upload der Datei {0} ist fehlgeschlagen, bitte versuchen Sie es erneut",
|
||||||
"uploadFailed": "Upload fehlgeschlagen",
|
"uploadFailed": "Upload fehlgeschlagen",
|
||||||
|
"hashEngineNotFound": "In der aktuellen Umgebung ist keine Engine zur Dateihash-Berechnung verfügbar",
|
||||||
"processing": {
|
"processing": {
|
||||||
"hash": "Hash wird berechnet...",
|
"hash": "Hash wird berechnet...",
|
||||||
"create": "Upload wird initialisiert...",
|
"create": "Upload wird initialisiert...",
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
"chunkUploadRetry": "Chunk {1} of file {0} failed to upload, we will try again later",
|
"chunkUploadRetry": "Chunk {1} of file {0} failed to upload, we will try again later",
|
||||||
"fileUploadFailed": "File {0} upload failed, please try again",
|
"fileUploadFailed": "File {0} upload failed, please try again",
|
||||||
"uploadFailed": "Upload Failed",
|
"uploadFailed": "Upload Failed",
|
||||||
|
"hashEngineNotFound": "No file hash engine is available in the current environment",
|
||||||
"processing": {
|
"processing": {
|
||||||
"hash": "Calculating Hash...",
|
"hash": "Calculating Hash...",
|
||||||
"create": "Initializing Upload...",
|
"create": "Initializing Upload...",
|
||||||
|
|||||||
@@ -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",
|
"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",
|
"fileUploadFailed": "Le téléversement du fichier {0} a échoué, veuillez réessayer",
|
||||||
"uploadFailed": "Échec du téléversement",
|
"uploadFailed": "Échec du téléversement",
|
||||||
|
"hashEngineNotFound": "Aucun moteur de calcul de hash de fichier n'est disponible dans l'environnement actuel",
|
||||||
"processing": {
|
"processing": {
|
||||||
"hash": "Calcul du hash...",
|
"hash": "Calcul du hash...",
|
||||||
"create": "Initialisation du téléversement...",
|
"create": "Initialisation du téléversement...",
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
"chunkUploadRetry": "ファイル {0} のチャンク {1} のアップロードに失敗しました。後でもう一度試します",
|
"chunkUploadRetry": "ファイル {0} のチャンク {1} のアップロードに失敗しました。後でもう一度試します",
|
||||||
"fileUploadFailed": "ファイル {0} のアップロードに失敗しました。もう一度お試しください",
|
"fileUploadFailed": "ファイル {0} のアップロードに失敗しました。もう一度お試しください",
|
||||||
"uploadFailed": "アップロード失敗",
|
"uploadFailed": "アップロード失敗",
|
||||||
|
"hashEngineNotFound": "現在の環境ではファイルハッシュ計算エンジンを利用できません",
|
||||||
"processing": {
|
"processing": {
|
||||||
"hash": "ハッシュを計算中...",
|
"hash": "ハッシュを計算中...",
|
||||||
"create": "アップロードを初期化中...",
|
"create": "アップロードを初期化中...",
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
"chunkUploadRetry": "파일 {0}의 청크 {1} 업로드에 실패했습니다. 잠시 후 다시 시도합니다",
|
"chunkUploadRetry": "파일 {0}의 청크 {1} 업로드에 실패했습니다. 잠시 후 다시 시도합니다",
|
||||||
"fileUploadFailed": "파일 {0} 업로드에 실패했습니다. 다시 시도해 주세요",
|
"fileUploadFailed": "파일 {0} 업로드에 실패했습니다. 다시 시도해 주세요",
|
||||||
"uploadFailed": "업로드 실패",
|
"uploadFailed": "업로드 실패",
|
||||||
|
"hashEngineNotFound": "현재 환경에서는 파일 해시 계산 엔진을 사용할 수 없습니다",
|
||||||
"processing": {
|
"processing": {
|
||||||
"hash": "해시 계산 중...",
|
"hash": "해시 계산 중...",
|
||||||
"create": "업로드 초기화 중...",
|
"create": "업로드 초기화 중...",
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
"chunkUploadRetry": "文件 {0} 的{1}分块上传失败, 我们将在稍后再次尝试上传",
|
"chunkUploadRetry": "文件 {0} 的{1}分块上传失败, 我们将在稍后再次尝试上传",
|
||||||
"fileUploadFailed": "文件{0}上传失败, 请重试",
|
"fileUploadFailed": "文件{0}上传失败, 请重试",
|
||||||
"uploadFailed": "上传失败",
|
"uploadFailed": "上传失败",
|
||||||
|
"hashEngineNotFound": "当前环境不支持文件哈希计算引擎",
|
||||||
"processing": {
|
"processing": {
|
||||||
"hash": "计算Hash...",
|
"hash": "计算Hash...",
|
||||||
"create": "初始化上传...",
|
"create": "初始化上传...",
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
"chunkUploadRetry": "檔案 {0} 的第 {1} 個分塊上傳失敗,稍後將再次嘗試",
|
"chunkUploadRetry": "檔案 {0} 的第 {1} 個分塊上傳失敗,稍後將再次嘗試",
|
||||||
"fileUploadFailed": "檔案 {0} 上傳失敗,請重試",
|
"fileUploadFailed": "檔案 {0} 上傳失敗,請重試",
|
||||||
"uploadFailed": "上傳失敗",
|
"uploadFailed": "上傳失敗",
|
||||||
|
"hashEngineNotFound": "目前環境不支援檔案雜湊計算引擎",
|
||||||
"processing": {
|
"processing": {
|
||||||
"hash": "計算雜湊中...",
|
"hash": "計算雜湊中...",
|
||||||
"create": "初始化上傳中...",
|
"create": "初始化上傳中...",
|
||||||
|
|||||||
@@ -35,4 +35,15 @@ export const calcNativeHash = async (buffer: BufferSource) => {
|
|||||||
.join('')
|
.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
|
export default calcFileHash
|
||||||
|
|||||||
Reference in New Issue
Block a user