更新版本至 0.0.37,优化文件上传功能,支持大文件分块上传,改用 SparkMD5 计算 Blob 哈希
This commit is contained in:
@@ -71,23 +71,83 @@ export class QueryResources {
|
||||
// Blob 类型时 hashContent 返回 Promise
|
||||
const hash = hashResult instanceof Promise ? await hashResult : hashResult;
|
||||
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();
|
||||
if (content instanceof Blob) {
|
||||
if (isBlob) {
|
||||
formData.append('file', content);
|
||||
} else {
|
||||
formData.append('file', new Blob([content], { type }));
|
||||
}
|
||||
return adapter({
|
||||
url: url.toString(),
|
||||
headers: { ...this.header(opts?.headers, false) },
|
||||
params: {
|
||||
hash: hash,
|
||||
},
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
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>> {
|
||||
const filepath = folderpath.endsWith('/') ? `${folderpath}keep.txt` : `${folderpath}/keep.txt`;
|
||||
return this.uploadFile(filepath, '文件夹占位,其他文件不存在,文件夹不存在,如果有其他文件夹,删除当前文件夹占位文件即可', opts);
|
||||
|
||||
Reference in New Issue
Block a user