更新版本至 0.0.37,优化文件上传功能,支持大文件分块上传,改用 SparkMD5 计算 Blob 哈希
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/api",
|
"name": "@kevisual/api",
|
||||||
"version": "0.0.36",
|
"version": "0.0.37",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "mod.ts",
|
"main": "mod.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -36,11 +36,13 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@kevisual/js-filter": "^0.0.5",
|
"@kevisual/js-filter": "^0.0.5",
|
||||||
"@kevisual/load": "^0.0.6",
|
"@kevisual/load": "^0.0.6",
|
||||||
|
"@types/spark-md5": "^3.0.5",
|
||||||
"es-toolkit": "^1.44.0",
|
"es-toolkit": "^1.44.0",
|
||||||
"eventemitter3": "^5.0.4",
|
"eventemitter3": "^5.0.4",
|
||||||
"fuse.js": "^7.1.0",
|
"fuse.js": "^7.1.0",
|
||||||
"nanoid": "^5.1.6",
|
"nanoid": "^5.1.6",
|
||||||
"path-browserify-esm": "^1.0.6"
|
"path-browserify-esm": "^1.0.6",
|
||||||
|
"spark-md5": "^3.0.2"
|
||||||
},
|
},
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./mod.ts",
|
".": "./mod.ts",
|
||||||
|
|||||||
16
pnpm-lock.yaml
generated
16
pnpm-lock.yaml
generated
@@ -14,6 +14,9 @@ importers:
|
|||||||
'@kevisual/load':
|
'@kevisual/load':
|
||||||
specifier: ^0.0.6
|
specifier: ^0.0.6
|
||||||
version: 0.0.6
|
version: 0.0.6
|
||||||
|
'@types/spark-md5':
|
||||||
|
specifier: ^3.0.5
|
||||||
|
version: 3.0.5
|
||||||
es-toolkit:
|
es-toolkit:
|
||||||
specifier: ^1.44.0
|
specifier: ^1.44.0
|
||||||
version: 1.44.0
|
version: 1.44.0
|
||||||
@@ -29,6 +32,9 @@ importers:
|
|||||||
path-browserify-esm:
|
path-browserify-esm:
|
||||||
specifier: ^1.0.6
|
specifier: ^1.0.6
|
||||||
version: 1.0.6
|
version: 1.0.6
|
||||||
|
spark-md5:
|
||||||
|
specifier: ^3.0.2
|
||||||
|
version: 3.0.2
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@kevisual/cache':
|
'@kevisual/cache':
|
||||||
specifier: ^0.0.5
|
specifier: ^0.0.5
|
||||||
@@ -350,6 +356,9 @@ packages:
|
|||||||
'@types/resolve@1.20.2':
|
'@types/resolve@1.20.2':
|
||||||
resolution: {integrity: sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==}
|
resolution: {integrity: sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==}
|
||||||
|
|
||||||
|
'@types/spark-md5@3.0.5':
|
||||||
|
resolution: {integrity: sha512-lWf05dnD42DLVKQJZrDHtWFidcLrHuip01CtnC2/S6AMhX4t9ZlEUj4iuRlAnts0PQk7KESOqKxeGE/b6sIPGg==}
|
||||||
|
|
||||||
abort-controller@3.0.0:
|
abort-controller@3.0.0:
|
||||||
resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==}
|
resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==}
|
||||||
engines: {node: '>=6.5'}
|
engines: {node: '>=6.5'}
|
||||||
@@ -650,6 +659,9 @@ packages:
|
|||||||
resolution: {integrity: sha512-th5B4L2U+eGLq1TVh7zNRGBapioSORUeymIydxgFpwww9d2qyKvtuPU2jJuHvYAwwqi2Y596QBL3eEqcPEYL8Q==}
|
resolution: {integrity: sha512-th5B4L2U+eGLq1TVh7zNRGBapioSORUeymIydxgFpwww9d2qyKvtuPU2jJuHvYAwwqi2Y596QBL3eEqcPEYL8Q==}
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
|
|
||||||
|
spark-md5@3.0.2:
|
||||||
|
resolution: {integrity: sha512-wcFzz9cDfbuqe0FZzfi2or1sgyIrsDwmPwfZC4hiNidPdPINjeUwNfv5kldczoEAcjl9Y1L3SM7Uz2PUEQzxQw==}
|
||||||
|
|
||||||
supports-preserve-symlinks-flag@1.0.0:
|
supports-preserve-symlinks-flag@1.0.0:
|
||||||
resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==}
|
resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==}
|
||||||
engines: {node: '>= 0.4'}
|
engines: {node: '>= 0.4'}
|
||||||
@@ -915,6 +927,8 @@ snapshots:
|
|||||||
|
|
||||||
'@types/resolve@1.20.2': {}
|
'@types/resolve@1.20.2': {}
|
||||||
|
|
||||||
|
'@types/spark-md5@3.0.5': {}
|
||||||
|
|
||||||
abort-controller@3.0.0:
|
abort-controller@3.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
event-target-shim: 5.0.1
|
event-target-shim: 5.0.1
|
||||||
@@ -1201,6 +1215,8 @@ snapshots:
|
|||||||
'@types/node-forge': 1.3.11
|
'@types/node-forge': 1.3.11
|
||||||
node-forge: 1.3.1
|
node-forge: 1.3.1
|
||||||
|
|
||||||
|
spark-md5@3.0.2: {}
|
||||||
|
|
||||||
supports-preserve-symlinks-flag@1.0.0: {}
|
supports-preserve-symlinks-flag@1.0.0: {}
|
||||||
|
|
||||||
to-regex-range@5.0.1:
|
to-regex-range@5.0.1:
|
||||||
|
|||||||
@@ -71,23 +71,83 @@ export class QueryResources {
|
|||||||
// Blob 类型时 hashContent 返回 Promise
|
// Blob 类型时 hashContent 返回 Promise
|
||||||
const hash = hashResult instanceof Promise ? await hashResult : hashResult;
|
const hash = hashResult instanceof Promise ? await hashResult : hashResult;
|
||||||
url.searchParams.set('hash', hash);
|
url.searchParams.set('hash', hash);
|
||||||
|
|
||||||
|
// 判断是否需要分块上传(文件大于20MB)
|
||||||
|
const isBlob = content instanceof Blob;
|
||||||
|
const fileSize = isBlob ? content.size : new Blob([content]).size;
|
||||||
|
const CHUNK_THRESHOLD = 20 * 1024 * 1024; // 20MB
|
||||||
|
|
||||||
|
if (fileSize > CHUNK_THRESHOLD && isBlob) {
|
||||||
|
// 使用分块上传
|
||||||
|
return this.uploadChunkedFile(filepath, content, hash, opts);
|
||||||
|
}
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
if (content instanceof Blob) {
|
if (isBlob) {
|
||||||
formData.append('file', content);
|
formData.append('file', content);
|
||||||
} else {
|
} else {
|
||||||
formData.append('file', new Blob([content], { type }));
|
formData.append('file', new Blob([content], { type }));
|
||||||
}
|
}
|
||||||
return adapter({
|
return adapter({
|
||||||
url: url.toString(),
|
url: url.toString(),
|
||||||
headers: { ...this.header(opts?.headers, false) },
|
|
||||||
params: {
|
|
||||||
hash: hash,
|
|
||||||
},
|
|
||||||
isPostFile: true,
|
isPostFile: true,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData,
|
body: formData,
|
||||||
|
timeout: 5 * 60 * 1000, // 5分钟超时
|
||||||
|
...opts,
|
||||||
|
headers: { ...opts?.headers, ...this.header(opts?.headers, false) },
|
||||||
|
params: {
|
||||||
|
hash: hash,
|
||||||
|
...opts?.params,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
async uploadChunkedFile(filepath: string, file: Blob, hash: string, opts?: DataOpts): Promise<Result<any>> {
|
||||||
|
const pathname = `${this.prefix}${filepath}`;
|
||||||
|
const filename = path.basename(pathname);
|
||||||
|
const url = new URL(pathname, window.location.origin);
|
||||||
|
url.searchParams.set('hash', hash);
|
||||||
|
url.searchParams.set('chunked', '1');
|
||||||
|
console.log(`url,`, url, hash);
|
||||||
|
// 预留 eventSource 支持(暂不处理)
|
||||||
|
// const createEventSource = opts?.createEventSource;
|
||||||
|
|
||||||
|
const chunkSize = 5 * 1024 * 1024; // 5MB
|
||||||
|
const totalChunks = Math.ceil(file.size / chunkSize);
|
||||||
|
|
||||||
|
for (let currentChunk = 0; currentChunk < totalChunks; currentChunk++) {
|
||||||
|
const start = currentChunk * chunkSize;
|
||||||
|
const end = Math.min(start + chunkSize, file.size);
|
||||||
|
const chunk = file.slice(start, end);
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', chunk, filename);
|
||||||
|
formData.append('chunkIndex', currentChunk.toString());
|
||||||
|
formData.append('totalChunks', totalChunks.toString());
|
||||||
|
console.log(`Uploading chunk ${currentChunk + 1}/${totalChunks}`, url.toString());
|
||||||
|
try {
|
||||||
|
const res = await adapter({
|
||||||
|
url: url.toString(),
|
||||||
|
isPostFile: true,
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
timeout: 5 * 60 * 1000, // 5分钟超时
|
||||||
|
...opts,
|
||||||
|
headers: { ...opts?.headers, ...this.header(opts?.headers, false) },
|
||||||
|
params: {
|
||||||
|
hash: hash,
|
||||||
|
...opts?.params,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(`Chunk ${currentChunk + 1}/${totalChunks} uploaded`, res);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error uploading chunk ${currentChunk + 1}/${totalChunks}`, error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { code: 200, message: '上传成功' };
|
||||||
|
}
|
||||||
async createFolder(folderpath: string, opts?: DataOpts): Promise<Result<any>> {
|
async createFolder(folderpath: string, opts?: DataOpts): Promise<Result<any>> {
|
||||||
const filepath = folderpath.endsWith('/') ? `${folderpath}keep.txt` : `${folderpath}/keep.txt`;
|
const filepath = folderpath.endsWith('/') ? `${folderpath}keep.txt` : `${folderpath}/keep.txt`;
|
||||||
return this.uploadFile(filepath, '文件夹占位,其他文件不存在,文件夹不存在,如果有其他文件夹,删除当前文件夹占位文件即可', opts);
|
return this.uploadFile(filepath, '文件夹占位,其他文件不存在,文件夹不存在,如果有其他文件夹,删除当前文件夹占位文件即可', opts);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import MD5 from 'crypto-js/md5';
|
import MD5 from 'crypto-js/md5';
|
||||||
|
import SparkMD5 from 'spark-md5';
|
||||||
|
|
||||||
export const hashContent = (str: string | Blob | Buffer): Promise<string> | string => {
|
export const hashContent = (str: string | Blob | Buffer): Promise<string> | string => {
|
||||||
if (typeof str === 'string') {
|
if (typeof str === 'string') {
|
||||||
@@ -12,57 +13,20 @@ export const hashContent = (str: string | Blob | Buffer): Promise<string> | stri
|
|||||||
return '';
|
return '';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 直接计算整个 Blob 的 MD5
|
||||||
export const hashBlob = (blob: Blob): Promise<string> => {
|
export const hashBlob = (blob: Blob): Promise<string> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const reader = new FileReader();
|
try {
|
||||||
reader.onload = async () => {
|
const spark = new SparkMD5.ArrayBuffer();
|
||||||
try {
|
spark.append(await blob.arrayBuffer());
|
||||||
const content = reader.result;
|
resolve(spark.end());
|
||||||
if (typeof content === 'string') {
|
} catch (error) {
|
||||||
resolve(MD5(content).toString());
|
console.error('hashBlob error', error);
|
||||||
} else if (content) {
|
|
||||||
const contentString = new TextDecoder().decode(content);
|
|
||||||
resolve(MD5(contentString).toString());
|
|
||||||
} else {
|
|
||||||
reject(new Error('Empty content'));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('hashBlob error', error);
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
reader.onerror = (error) => reject(error);
|
|
||||||
reader.readAsArrayBuffer(blob);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
export const hashFile = (file: File): Promise<string> => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const reader = new FileReader();
|
|
||||||
|
|
||||||
reader.onload = async (event) => {
|
|
||||||
try {
|
|
||||||
const content = event.target?.result;
|
|
||||||
if (content instanceof ArrayBuffer) {
|
|
||||||
const contentString = new TextDecoder().decode(content);
|
|
||||||
const hashHex = MD5(contentString).toString();
|
|
||||||
resolve(hashHex);
|
|
||||||
} else if (typeof content === 'string') {
|
|
||||||
const hashHex = MD5(content).toString();
|
|
||||||
resolve(hashHex);
|
|
||||||
} else {
|
|
||||||
throw new Error('Invalid content type');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('hashFile error', error);
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
reader.onerror = (error) => {
|
|
||||||
reject(error);
|
reject(error);
|
||||||
};
|
}
|
||||||
|
|
||||||
// 读取文件为 ArrayBuffer
|
|
||||||
reader.readAsArrayBuffer(file);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const hashFile = (file: File): Promise<string> => {
|
||||||
|
return hashBlob(file);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user