base.d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { region } from '../config';
  2. import Logger, { LogLevel } from '../logger';
  3. import * as utils from '../utils';
  4. export declare const DEFAULT_CHUNK_SIZE = 4;
  5. /** 上传文件的资源信息配置 */
  6. export interface Extra {
  7. /** 文件原文件名 */
  8. fname: string;
  9. /** 用来放置自定义变量 */
  10. customVars?: {
  11. [key: string]: string;
  12. };
  13. /** 自定义元信息 */
  14. metadata?: {
  15. [key: string]: string;
  16. };
  17. /** 文件类型设置 */
  18. mimeType?: string;
  19. }
  20. /** 上传任务的配置信息 */
  21. export interface Config {
  22. /** 是否开启 cdn 加速 */
  23. useCdnDomain: boolean;
  24. /** 是否对分片进行 md5校验 */
  25. checkByMD5: boolean;
  26. /** 强制直传 */
  27. forceDirect: boolean;
  28. /** 上传失败后重试次数 */
  29. retryCount: number;
  30. /** 自定义上传域名 */
  31. uphost: string;
  32. /** 自定义分片上传并发请求量 */
  33. concurrentRequestLimit: number;
  34. /** 分片大小,单位为 MB */
  35. chunkSize: number;
  36. /** 上传域名协议 */
  37. upprotocol: 'http:' | 'https:';
  38. /** 上传区域 */
  39. region?: typeof region[keyof typeof region];
  40. /** 是否禁止统计日志上报 */
  41. disableStatisticsReport: boolean;
  42. /** 设置调试日志输出模式,默认 `OFF`,不输出任何日志 */
  43. debugLogLevel?: LogLevel;
  44. }
  45. export interface UploadOptions {
  46. file: File;
  47. key: string | null | undefined;
  48. token: string;
  49. putExtra?: Partial<Extra>;
  50. config?: Partial<Config>;
  51. }
  52. export interface UploadInfo {
  53. id: string;
  54. url: string;
  55. }
  56. /** 传递给外部的上传进度信息 */
  57. export interface UploadProgress {
  58. total: ProgressCompose;
  59. uploadInfo?: UploadInfo;
  60. chunks?: ProgressCompose[];
  61. }
  62. export interface UploadHandler {
  63. onData: (data: UploadProgress) => void;
  64. onError: (err: utils.CustomError) => void;
  65. onComplete: (res: any) => void;
  66. }
  67. export interface Progress {
  68. loaded: number;
  69. total: number;
  70. }
  71. export interface ProgressCompose {
  72. loaded: number;
  73. size: number;
  74. percent: number;
  75. }
  76. export declare type XHRHandler = (xhr: XMLHttpRequest) => void;
  77. export default abstract class Base {
  78. protected logger: Logger;
  79. protected config: Config;
  80. protected putExtra: Extra;
  81. protected xhrList: XMLHttpRequest[];
  82. protected file: File;
  83. protected key: string | null | undefined;
  84. protected aborted: boolean;
  85. protected retryCount: number;
  86. protected token: string;
  87. protected uploadUrl: string;
  88. protected bucket: string;
  89. protected uploadAt: number;
  90. protected progress: UploadProgress;
  91. protected onData: (data: UploadProgress) => void;
  92. protected onError: (err: utils.CustomError) => void;
  93. protected onComplete: (res: any) => void;
  94. protected abstract run(): utils.Response<any>;
  95. constructor(options: UploadOptions, handlers: UploadHandler, logger: Logger);
  96. private handleError;
  97. /**
  98. * @returns Promise 返回结果与上传最终状态无关,状态信息请通过 [Subscriber] 获取。
  99. * @description 上传文件,状态信息请通过 [Subscriber] 获取。
  100. */
  101. putFile(): Promise<void>;
  102. private clear;
  103. stop(): void;
  104. addXhr(xhr: XMLHttpRequest): void;
  105. private sendLog;
  106. getProgressInfoItem(loaded: number, size: number): {
  107. loaded: number;
  108. size: number;
  109. percent: number;
  110. };
  111. }