130 lines
4.1 KiB
TypeScript
130 lines
4.1 KiB
TypeScript
import { QiniuError } from '../errors';
|
|
import Logger, { LogLevel } from '../logger';
|
|
import { region } from '../config';
|
|
import * as utils from '../utils';
|
|
import { Host, HostPool } from './hosts';
|
|
export declare const DEFAULT_CHUNK_SIZE = 4;
|
|
export declare const FREEZE_CODE_LIST: number[];
|
|
export declare const RETRY_CODE_LIST: number[];
|
|
/** 上传文件的资源信息配置 */
|
|
export interface Extra {
|
|
/** 文件原文件名 */
|
|
fname: string;
|
|
/** 用来放置自定义变量 */
|
|
customVars?: {
|
|
[key: string]: string;
|
|
};
|
|
/** 自定义元信息 */
|
|
metadata?: {
|
|
[key: string]: string;
|
|
};
|
|
/** 文件类型设置 */
|
|
mimeType?: string;
|
|
}
|
|
export interface InternalConfig {
|
|
/** 是否开启 cdn 加速 */
|
|
useCdnDomain: boolean;
|
|
/** 是否开启服务端校验 */
|
|
checkByServer: boolean;
|
|
/** 是否对分片进行 md5校验 */
|
|
checkByMD5: boolean;
|
|
/** 强制直传 */
|
|
forceDirect: boolean;
|
|
/** 上传失败后重试次数 */
|
|
retryCount: number;
|
|
/** 自定义上传域名 */
|
|
uphost: string[];
|
|
/** 自定义分片上传并发请求量 */
|
|
concurrentRequestLimit: number;
|
|
/** 分片大小,单位为 MB */
|
|
chunkSize: number;
|
|
/** 上传域名协议 */
|
|
upprotocol: 'https' | 'http';
|
|
/** 上传区域 */
|
|
region?: typeof region[keyof typeof region];
|
|
/** 是否禁止统计日志上报 */
|
|
disableStatisticsReport: boolean;
|
|
/** 设置调试日志输出模式,默认 `OFF`,不输出任何日志 */
|
|
debugLogLevel?: LogLevel;
|
|
}
|
|
/** 上传任务的配置信息 */
|
|
export interface Config extends Partial<Omit<InternalConfig, 'upprotocol' | 'uphost'>> {
|
|
/** 上传域名协议 */
|
|
upprotocol?: InternalConfig['upprotocol'] | 'https:' | 'http:';
|
|
/** 自定义上传域名 */
|
|
uphost?: InternalConfig['uphost'] | string;
|
|
}
|
|
export interface UploadOptions {
|
|
file: File;
|
|
key: string | null | undefined;
|
|
token: string;
|
|
config: InternalConfig;
|
|
putExtra?: Partial<Extra>;
|
|
}
|
|
export interface UploadInfo {
|
|
id: string;
|
|
url: string;
|
|
}
|
|
/** 传递给外部的上传进度信息 */
|
|
export interface UploadProgress {
|
|
total: ProgressCompose;
|
|
uploadInfo?: UploadInfo;
|
|
chunks?: ProgressCompose[];
|
|
}
|
|
export interface UploadHandlers {
|
|
onData: (data: UploadProgress) => void;
|
|
onError: (err: QiniuError) => void;
|
|
onComplete: (res: any) => void;
|
|
}
|
|
export interface Progress {
|
|
total: number;
|
|
loaded: number;
|
|
}
|
|
export interface ProgressCompose {
|
|
size: number;
|
|
loaded: number;
|
|
percent: number;
|
|
fromCache?: boolean;
|
|
}
|
|
export declare type XHRHandler = (xhr: XMLHttpRequest) => void;
|
|
export default abstract class Base {
|
|
protected hostPool: HostPool;
|
|
protected logger: Logger;
|
|
protected config: InternalConfig;
|
|
protected putExtra: Extra;
|
|
protected aborted: boolean;
|
|
protected retryCount: number;
|
|
protected uploadHost?: Host;
|
|
protected xhrList: XMLHttpRequest[];
|
|
protected file: File;
|
|
protected key: string | null | undefined;
|
|
protected token: string;
|
|
protected assessKey: string;
|
|
protected bucketName: string;
|
|
protected uploadAt: number;
|
|
protected progress: UploadProgress;
|
|
protected onData: (data: UploadProgress) => void;
|
|
protected onError: (err: QiniuError) => void;
|
|
protected onComplete: (res: any) => void;
|
|
/**
|
|
* @returns utils.Response<any>
|
|
* @description 子类通过该方法实现具体的任务处理
|
|
*/
|
|
protected abstract run(): utils.Response<any>;
|
|
constructor(options: UploadOptions, handlers: UploadHandlers, hostPool: HostPool, logger: Logger);
|
|
protected checkAndUpdateUploadHost(): Promise<void>;
|
|
protected checkAndUnfreezeHost(): void;
|
|
private checkAndFreezeHost;
|
|
private handleError;
|
|
/**
|
|
* @returns Promise 返回结果与上传最终状态无关,状态信息请通过 [Subscriber] 获取。
|
|
* @description 上传文件,状态信息请通过 [Subscriber] 获取。
|
|
*/
|
|
putFile(): Promise<void>;
|
|
private clear;
|
|
stop(): void;
|
|
addXhr(xhr: XMLHttpRequest): void;
|
|
private sendLog;
|
|
getProgressInfoItem(loaded: number, size: number, fromCache?: boolean): ProgressCompose;
|
|
}
|