new-ruoyi-geek/ruoyi-geek-app/utils/ChunkUpload.ts

721 lines
25 KiB
TypeScript
Raw Permalink Normal View History

2025-11-17 15:20:25 +00:00
import modal from '@/plugins/modal'
import { initChunkUpload, uploadChunk, completeChunkUpload } from '@/api/system/chunkUpload'
import { UploadOptions, PartETag, File, UploadData, ProgressInfo } from '@/types/upload'
import TaskQueue from '@/utils/TaskQueue'
// 声明微信小程序全局对象
declare const wx: any;
/**
*
*
*/
export class ChunkUpload {
/**
*
*/
private chunkSize: number;
/**
*
*/
private concurrentLimit: number;
/**
*
*/
private static readonly PROGRESS_UPDATE_INTERVAL = 10;
/**
* -
* 15MB2
*/
constructor() {
this.chunkSize = 15 * 1024 * 1024; // 默认分片大小15MB
this.concurrentLimit = 2; // 并发上传的分片数量
}
/**
*
* @param params
*
* @param params.file path和size属性
* @param params.onSuccess { success: true }
* @param params.onError
* @param params.options { chunkSize?: number; concurrentLimit?: number }
* @param params.options.chunkSize 15MB (15 * 1024 * 1024)
* @param params.options.concurrentLimit 2
*
* @returns Promise<boolean> Promiseresolve(true)resolve(false)
*/
async upload(params: UploadOptions): Promise<boolean> {
const {
file,
onSuccess,
onError,
options = {} as { chunkSize?: number; concurrentLimit?: number }
} = params
try {
// 1.检验文件的参数
this._validateParams(file);
//2.获取文件信息
const { actualFilePath, actualFileSize, actualFileName } = this.getFileInfo(file);
modal.loading("准备上传...")
// 3.初始化分片数据
const chunkSize = options.chunkSize || this.chunkSize;
const chunkCount = Math.ceil(actualFileSize / chunkSize);
const concurrentLimit = options.concurrentLimit || this.concurrentLimit;
let partETags: PartETag[] = [];
//4.初始化分片上传
const initResult = await initChunkUpload(actualFileName, actualFileSize)
if (initResult.code !== 200) throw new Error("初始化上传失败")
const { uploadId, filePath: serverFilePath } = initResult.data
//5.将文件移动到应用 沙盒 目录
// #ifdef APP-PLUS
const localFilePath = await this.copyFileToSandbox(actualFilePath)
// #endif
//6.开始上传分片
modal.closeLoading()
modal.loading("上传中...")
const progressInfo: ProgressInfo = {
completedChunks: 0,
uploadProgress: 0,
chunkCount
}
// 7.并发上传数据
const uploadData = {
uploadId,
saveFilePath: serverFilePath,
fileSize: actualFileSize,
chunkCount,
filePath: actualFilePath
};
// #ifdef APP-PLUS
partETags = await this.uploadChunksWithTaskQueue(
uploadData,
chunkSize,
concurrentLimit,
localFilePath,
progressInfo
)
// #endif
// #ifdef MP-WEIXIN
partETags = await this._uploadChunks(uploadData, concurrentLimit, progressInfo);
// #endif
//8.合并分片
modal.closeLoading();
modal.loading("正在合并分片...")
//完成分片上传
await completeChunkUpload(
uploadId, serverFilePath, actualFileSize, actualFileName, partETags
)
// 9.将临时文件删除,防止占用空间
// #ifdef APP-PLUS
await this.deleteLocalFile(localFilePath)
// #endif
modal.closeLoading()
// 10.执行成功回调
onSuccess?.({ success: true })
return true
} catch (error) {
modal.closeLoading()
const errorMessage = error instanceof Error ? error.message : `上传失败`
onError?.(errorMessage)
return false
}
}
/**
*
*
* @param file -
* @throws {Error}
* @throws {Error}
*/
_validateParams(file: File) {
if (!file.path) throw new Error("文件路径不存在");
if (!file.size) throw new Error("文件大小不存在");
}
/**
*
*
* @param file path和size属性
* @returns
* @returns actualFilePath
* @returns actualFileSize
* @returns actualFileName
*/
getFileInfo(file: File): { actualFilePath: string; actualFileSize: number; actualFileName: string } {
const actualFilePath = file.path;
const actualFileSize = file.size;
let actualFileName: string;
// #ifdef APP-PLUS
actualFileName = this.getFileName(file.path);
// #endif
// #ifdef MP-WEIXIN
actualFileName = `weixin_${Date.now()}.${this.getFileExtension(file.path)}`;
// #endif
return {
actualFilePath,
actualFileSize,
actualFileName
};
}
/**
*
* @param filePath
* @returns string
*/
getFileName(filePath: string): string {
if (!filePath) return ""
const slashIndex = filePath.lastIndexOf("/");
if (slashIndex === -1) return filePath;
return filePath.substring(slashIndex + 1);
};
/**
*
* @param filePath
* @returns string
*/
getFileExtension(filePath: string): string {
if (!filePath) return ""
const dotIndex = filePath.lastIndexOf(".");
if (dotIndex === -1) return ""
return filePath.substring(dotIndex + 1).toLowerCase();
};
/**
*
* @param srcFilePath
* @returns Promise<string>
*/
copyFileToSandbox(srcFilePath: string): Promise<string> {
return new Promise((resolve, reject) => {
const newName = `file_${Date.now()}.${this.getFileExtension(srcFilePath)}`;
plus.io.requestFileSystem(
plus.io.PRIVATE_DOC,
(dstEntry) => {
plus.io.resolveLocalFileSystemURL(
srcFilePath,
(srcEntry) => {
srcEntry.copyTo(
dstEntry.root,
newName,
(entry) => {
if (entry.fullPath) {
resolve(entry.fullPath);
} else {
reject(new Error('File path is undefined'));
}
},
(e) => reject(e)
);
},
(e) => reject(e)
);
},
(e) => reject(e)
);
});
};
/**
* end位置
* @param start
* @param chunkSize
* @param fileSize
* @param index
* @param totalChunks
* @returns number
*/
getSliceEnd(start: number, chunkSize: number, fileSize: number, index: number, totalChunks: number) {
return index < totalChunks - 1 ? start + chunkSize - 1 : fileSize
}
/**
* 使TaskQueue并发上传分片APP端
*
* @param uploadData uploadIdsaveFilePathfileSize
* @param chunkSize
* @param concurrentLimit
* @param localFilePath APP端沙盒中的本地文件路径
* @param progressInfo completedChunksuploadProgresschunkCount等属性
*
* @returns Promise<PartETag[]> ETag信息数组
*
* @throws {Error}
*
*/
async uploadChunksWithTaskQueue(
uploadData: UploadData,
chunkSize: number,
concurrentLimit: number,
localFilePath: string,
progressInfo: ProgressInfo
): Promise<PartETag[]> {
const { chunkCount, fileSize, uploadId, saveFilePath } = uploadData;
const taskQueue = new TaskQueue(concurrentLimit);
const partETags: PartETag[] = [];
// 创建所有分片上传任务
const uploadPromises: Promise<PartETag>[] = [];
for (let i = 0; i < chunkCount; i++) {
const task = {
index: i + 1,
start: i * chunkSize,
end: this.getSliceEnd(i * chunkSize, chunkSize, fileSize, i, chunkCount),
};
const promise = taskQueue.add(async () => {
const chunk = await this.readAppFileChunk(localFilePath, task.start, task.end - task.start);
const response = await this.uploadAppChunk(uploadId, saveFilePath, task.index, chunk) as any;
if (!response.data || !response.data.etag) throw new Error('分片上传失败');
// 更新进度
this.updateUploadProgress(progressInfo);
return {
partNumber: task.index,
ETag: response.data.etag,
};
});
uploadPromises.push(promise);
}
// 等待所有任务完成
try {
const results = await Promise.all(uploadPromises);
// 收集所有 partETags
results.forEach(partETag => {
if (partETag) partETags.push(partETag);
});
// 按 partNumber 排序确保顺序正确
partETags.sort((a, b) => a.partNumber - b.partNumber);
return partETags;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : '分片上传失败';
throw new Error(`分片上传失败: ${errorMessage}`);
}
}
/**
* APP端分片上传单个分片
* @param uploadId ID
* @param filePath
* @param chunkIndex
* @param chunk ArrayBuffer或字符串
* @returns Promise<any>
*/
async uploadAppChunk(uploadId: string, filePath: string, chunkIndex: number, chunk: ArrayBuffer | string) {
try {
const response = await this.startUploadAppChunk(uploadId, filePath, chunkIndex, chunk)
return response
} catch (error) {
throw new Error('分片上传失败')
}
}
/**
* APP端分片上传
* @param uploadId ID
* @param filePath
* @param chunkIndex
* @param chunk ArrayBuffer或字符串
* @returns Promise Promise
*/
startUploadAppChunk(uploadId: string, filePath: string, chunkIndex: number, chunk: ArrayBuffer | string) {
return new Promise(async (resolve, reject) => {
try {
// 1. 准备临时文件信息
const tempFileName = `temp_chunk/chunk_${uploadId}_${chunkIndex}.bin`
const tempDirPath = plus.io.PRIVATE_DOC
// 2. 创建并写入临时文件
const tempFilePath = await this.createAndWriteTempFile(
tempDirPath,
tempFileName,
chunk
)
//设置文件的全路径
let formattedPath = tempFilePath
if (tempFilePath && !tempFilePath.startsWith("file://")) {
formattedPath = `file://${tempFilePath}`
}
// 3. 上传文件
const result = await uploadChunk(uploadId, filePath, chunkIndex, formattedPath)
// 4. 删除临时文件
await this.deleteTempFile(tempDirPath, tempFileName)
resolve(result)
} catch (error) {
reject(error)
}
})
}
/**
*
* @param filePath
* @returns Promise<boolean>
*/
deleteLocalFile(filePath: string): Promise<boolean> {
return new Promise((resolve) => {
if (!filePath) {
resolve(false);
return;
}
plus.io.resolveLocalFileSystemURL(
filePath,
(entry) => {
entry.remove(
() => { resolve(true); },
() => { resolve(false); }
);
},
() => { resolve(false); }
);
});
};
/**
*
* @param dirPath plus.io.PRIVATE_DOC等
* @param fileName
* @param data ArrayBuffer或字符串
* @returns Promise<string>
*/
createAndWriteTempFile(dirPath: number, fileName: String, data: ArrayBuffer | string): Promise<string> {
return new Promise((resolve, reject) => {
plus.io.requestFileSystem(
dirPath,
(dirEntry: any) => {
dirEntry.root.getFile(
fileName,
{ create: true, exclusive: false },
(fileEntry: any) => {
fileEntry.createWriter(
(writer: any) => {
const filePath = fileEntry.fullPath
writer.onwrite = function () { resolve(filePath) }
writer.onerror = function (e: any) { reject(e) }
try {
if (data) writer.writeAsBinary(data)
} catch (e) { reject(e) }
},
(err: any) => reject(err)
)
},
(err: any) => reject(err)
)
},
(err) => { reject(err) }
)
})
}
/**
*
* @param dirPath plus.io.PRIVATE_DOC等
* @param fileName
* @returns Promise<boolean>
*/
deleteTempFile(dirPath: number, fileName: string): Promise<boolean> {
return new Promise((resolve, reject) => {
plus.io.requestFileSystem(
dirPath,
(dirEntry) => {
if (!dirEntry || !dirEntry.root) {
reject(new Error('Directory entry or root is undefined'));
return;
}
dirEntry.root.getFile(
fileName,
{ create: false },
(fileEntry) => {
fileEntry.remove(
() => { resolve(true); },
() => { resolve(true); }
);
},
() => resolve(true)
);
},
() => resolve(true)
);
});
}
/**
* APP端文件分片的数据
* @param filePath
* @param start
* @param length
* @returns Promise<string> Base64编码的分片数据
*/
readAppFileChunk(filePath: string, start: number, length: number): Promise<string> {
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(
filePath,
(entry: any) => {
entry.file(
(file: any) => {
const reader = new plus.io.FileReader();
try {
const slice = file.slice(start, start + length);
reader.readAsDataURL(slice);
} catch (sliceError) {
reject(sliceError);
}
reader.onloadend = (e: any) => {
if (e.target.readyState == 2) {
try {
const base64 = e.target.result.split(",")[1];
resolve(base64);
} catch (err) {
reject(err);
}
}
};
reader.onerror = (err) => { reject(err); };
},
(error: any) => { reject(error); }
);
},
(error) => { reject(error); }
);
});
};
/**
* 使TaskQueue并发上传分片
*
* @param uploadData
* @param concurrentLimit
* @param progressInfo
*
* @returns Promise<PartETag[]> ETag信息数组partNumber排序
*
* @throws {Error}
*/
async _uploadChunks(uploadData: UploadData, concurrentLimit: number, progressInfo: ProgressInfo) {
try {
const { uploadId, saveFilePath, fileSize, chunkCount, filePath } = uploadData;
const fileManager = uni.getFileSystemManager();
const partETags: PartETag[] = [];
const taskQueue = new TaskQueue(concurrentLimit);
// 创建所有分片上传任务
const uploadTasks = [];
for (let i = 0; i < chunkCount; i++) {
const task = taskQueue.add(async () => {
return await this._uploadSingleChunk(
fileManager,
uploadId,
saveFilePath,
filePath,
i,
fileSize,
progressInfo
);
});
uploadTasks.push(task);
}
// 等待所有任务完成
const results = await Promise.all(uploadTasks);
// 收集所有 partETags
results.forEach(partETag => {
if (partETag) partETags.push(partETag);
});
// 按 partNumber 排序确保顺序正确
partETags.sort((a, b) => a.partNumber - b.partNumber);
return partETags;
} catch (e) {
const errorMessage = e instanceof Error ? e.message : '上传分片失败';
throw new Error(errorMessage);
}
}
/**
*
* @param fileManager
* @param uploadId ID
* @param saveFilePath
* @param filePath
* @param chunkIndex 0
* @param fileSize
* @param progressInfo
* @returns Promise<PartETag> ETag信息
*/
private async _uploadSingleChunk(
fileManager: UniApp.FileSystemManager,
uploadId: string,
saveFilePath: string,
filePath: string,
chunkIndex: number,
fileSize: number,
progressInfo: ProgressInfo
): Promise<PartETag> {
const start = chunkIndex * this.chunkSize;
const end = Math.min(start + this.chunkSize, fileSize);
const tempChunkPath = `${wx.env.USER_DATA_PATH}/chunk_${chunkIndex}_${Date.now()}.tmp`;
try {
// 1. 处理分片数据
await this._processChunk(fileManager, filePath, tempChunkPath, start, end - start);
// 2. 上传分片
const partNumber = chunkIndex + 1;
const response = await uploadChunk(uploadId, saveFilePath, partNumber, tempChunkPath);
if (!response.data?.etag) {
throw new Error(`分片 ${partNumber} 上传失败,无效响应`);
}
// 3. 更新进度
this.updateUploadProgress(progressInfo);
return {
partNumber,
ETag: response.data.etag,
};
} finally {
// 4. 清理临时文件(无论成功失败都要清理)
this._cleanupTempFile(fileManager, tempChunkPath);
}
}
/**
*
*
* @param fileManager - uni-app文件系统管理器实例
* @param filePath -
* @param tempChunkPath -
* @param start -
* @param length -
* @returns Promise<void> - Promise
* @throws {Error}
*/
async _processChunk(fileManager: UniApp.FileSystemManager, filePath: string, tempChunkPath: string, start: number, length: number) {
const readRes = await new Promise<ArrayBuffer | string>((resolve, reject) => {
fileManager.readFile({
filePath: filePath,
position: start,
length: length,
success: (res: any) => {
resolve(res.data as ArrayBuffer | string)
},
fail: (err) => {
reject(err)
},
});
});
// 写入临时文件
await new Promise((resolve, reject) => {
fileManager.writeFile({
filePath: tempChunkPath,
data: readRes,
success: () => {
resolve(true)
},
fail: (err) => {
reject(err)
},
});
});
}
/**
*
*
* @param fileManager - uni-app文件系统管理器实例
* @param tempChunkPath -
* @throws {Error}
*/
_cleanupTempFile(fileManager: UniApp.FileSystemManager, tempChunkPath: string) {
try {
fileManager.unlinkSync(tempChunkPath);
} catch (e) {
const errorMessage = e instanceof Error ? e.message : '未知错误';
throw new Error(`删除临时文件失败: ${tempChunkPath}, 错误: ${errorMessage}`);
}
}
/**
*
*
* @param progressInfo completedChunksuploadProgresschunkCount等属性
*/
private updateUploadProgress(progressInfo: ProgressInfo): void {
// 增加已完成分片数
progressInfo.completedChunks++;
// 计算当前进度百分比
const percent = Math.floor((progressInfo.completedChunks / progressInfo.chunkCount) * 100);
// 计算显示进度按间隔更新避免过于频繁的UI更新
const displayPercent = Math.floor(percent / ChunkUpload.PROGRESS_UPDATE_INTERVAL) * ChunkUpload.PROGRESS_UPDATE_INTERVAL;
// 当显示进度发生变化或上传完成时更新UI
if (displayPercent !== progressInfo.uploadProgress || progressInfo.completedChunks === progressInfo.chunkCount) {
modal.closeLoading();
const displayPercentForUI = progressInfo.completedChunks === progressInfo.chunkCount ? 100 : displayPercent;
modal.loading(`上传中 ${displayPercentForUI}% (请勿离开此页面)`);
progressInfo.uploadProgress = displayPercent;
}
}
}
export const chunkUpload = new ChunkUpload();