/** * @file src/bos/super_upload.ts * @desc BOS 分片上传封装 * @author lurunze */ import type { BceResponse } from '../types/common'; import type { UploadData, StorageClass as StorageClassType, PartInfo, ListPartsResponse, InitiateMultipartUploadResponse, UploadPartResponse } from './types'; /** 进度回调参数 */ export interface ProgressCallbackParams { /** 当前上传速度 */ speed: string; /** 当前上传进度 */ progress: number; /** 当前上传进度-百分比 */ percent: string; /** 已上传字节数 */ uploadedBytes: number; /** 文件总字节数 */ totalBytes: number; } /** 进度回调函数 */ export type ProgressCallback = (params: ProgressCallbackParams) => void; /** 状态变化回调参数 */ export interface StateChangeCallbackParams { /** 消息 */ message: string; /** 数据 */ data: any; } /** 状态变化回调函数 */ export type StateChangeCallback = (state: string, params: StateChangeCallbackParams) => void; /** 超级上传选项 */ export interface SuperUploadOptions { /** 存储桶名称 */ bucketName: string; /** 上传后对象名称 */ objectName: string; /** 上传数据, 类型为string时表示文件路径 */ data: UploadData; /** 文件大小 */ ContentLength: number; /** MimeType */ ContentType?: string; /** 默认分片大小, 单位为bytes */ chunkSize?: number; /** 分片并发数 */ partConcurrency?: number; /** 存储类型 */ StorageClass?: StorageClassType; /** 任务创建时间 */ createTime?: string; /** 上传ID, 如果存在则表示任务已经初始化 */ uploadId?: string; /** 数据类型 */ dataType?: string; /** 上传进度回调函数 */ onProgress?: ProgressCallback; /** 状态变化回调函数 */ onStateChange?: StateChangeCallback; } /** BOS 客户端接口(最小必需方法) */ interface BosClientLike { initiateMultipartUpload(bucket: string, key: string, options?: any): Promise>; uploadPartFromFile(bucket: string, key: string, uploadId: string, partNumber: number, partSize: number, filename: string, start: number, options?: any): Promise>; uploadPartFromDataUrl(bucket: string, key: string, uploadId: string, partNumber: number, partSize: number, dataUrl: string, options?: any): Promise>; uploadPartFromBlob(bucket: string, key: string, uploadId: string, partNumber: number, partSize: number, blob: Blob, options?: any): Promise>; listParts(bucket: string, key: string, uploadId: string, options?: any): Promise>; completeMultipartUpload(bucket: string, key: string, uploadId: string, parts: PartInfo[]): Promise; } /** * 超级上传类 - 提供大文件分片上传功能 * * @example * ```typescript * const superUpload = new SuperUpload(bosClient, { * bucketName: 'my-bucket', * objectName: 'large-file.zip', * data: fileBuffer, * ContentLength: fileBuffer.length, * ContentType: 'application/zip', * onProgress: (progress) => { * console.log(`Upload progress: ${progress.percent}`); * } * }); * * await superUpload.start(); * ``` */ export declare class SuperUpload { /** BOS 客户端 */ client: BosClientLike; /** 当前状态 */ state: string; /** 存储桶名称 */ bucketName: string; /** 对象名称 */ objectName: string; /** 上传数据 */ data: UploadData; /** 上传ID */ uploadId: string; /** 内容类型 */ ContentType?: string; /** 内容长度 */ ContentLength: number; /** 存储类型 */ StorageClass: StorageClassType; /** 分片并发数 */ partConcurrency: number; /** 分片大小 */ chunkSize: number; /** 创建时间 */ createTime: string; /** 进度回调 */ onProgress: ProgressCallback | null; /** 状态变化回调 */ onStateChange: StateChangeCallback | null; /** 数据类型 */ private __dataType; /** 已上传字节数 */ private __uploadedBytes; /** 已上传分片 */ private __uploadedParts; /** 上传速度集合 */ private __speeds; /** 异常任务集合 */ private __exceptionParts; /** 重试次数 */ private __retryTimes; /** 任务队列 */ private __queue; /** * 构造函数 * @param client BOS 客户端实例 * @param options 上传选项 */ constructor(client: BosClientLike, options: SuperUploadOptions); /** * 初始化任务 * @param options 上传选项 */ private __init; /** * 开始上传任务 */ start(): Promise; /** * 暂停上传 */ pause(): void; /** * 恢复上传 */ resume(): void; /** * 取消上传 */ cancel(): void; /** * 检查是否已完成 */ isCompleted(): boolean; /** * 检查是否正在运行 */ isRunning(): boolean; /** * 检查是否已暂停 */ isPaused(): boolean; /** * 检查是否已取消 */ isCancelled(): boolean; /** * 检查是否失败 */ isFailed(): boolean; /** * 获取已上传的分片列表 */ private __listExistedParts; /** * 生成分片任务 */ private __getMicroTasks; /** * 动态计算分片大小 */ private __calculatePartSize; /** * 上传分片任务函数 */ private __uploadPart; /** * 完成上传 */ private __complete; /** * 发送进度事件 */ private __emitProgress; } export default SuperUpload; //# sourceMappingURL=super_upload.d.ts.map