import BaseFoundation, { DefaultAdapter } from '../base/foundation';
export interface XhrError extends Error {
status: XMLHttpRequest['status'];
method: string;
url: string;
}
export type FileItemStatus = 'success' | 'uploadFail' | 'validateFail' | 'validating' | 'uploading' | 'wait';
export interface BaseFileItem {
showReplace?: boolean;
showRetry?: boolean;
response?: any;
event?: Event;
status: FileItemStatus;
name: string;
size: string;
uid: string;
url?: string;
fileInstance?: File;
percent?: number;
_sizeInvalid?: boolean;
preview?: boolean;
validateMessage?: any;
shouldUpload?: boolean;
[key: string]: any;
}
export interface CustomFile extends File {
uid?: string;
_sizeInvalid?: boolean;
status?: string;
}
export interface BeforeUploadObjectResult {
shouldUpload?: boolean;
status?: string;
autoRemove?: boolean;
validateMessage?: unknown;
fileInstance?: CustomFile;
}
export interface AfterUploadResult {
autoRemove?: boolean;
status?: string;
validateMessage?: unknown;
name?: string;
url?: string;
}
export interface UploadAdapter
, S = Record> extends DefaultAdapter {
notifyFileSelect: (files: Array) => void;
notifyError: (error: XhrError, fileInstance: File, fileList: Array, xhr: XMLHttpRequest) => void;
notifySuccess: (body: any, fileInstance: File, newFileList: Array) => void;
notifyProgress: (percent: number, fileInstance: File, newFileList: Array) => void;
notifyRemove: (file: File, newFileList: Array, fileItem: BaseFileItem) => void;
notifySizeError: (file: File, fileList: Array) => void;
notifyExceed: (files: Array) => void;
updateFileList: (newFileList: Array, callback?: () => void) => void;
notifyBeforeUpload: ({ file, fileList }: {
file: BaseFileItem;
fileList: Array;
}) => boolean | BeforeUploadObjectResult | Promise;
notifyAfterUpload: ({ response, file, fileList }: {
response: any;
file: BaseFileItem;
fileList: Array;
}) => AfterUploadResult;
resetInput: () => void;
resetReplaceInput: () => void;
updateDragAreaStatus: (dragAreaStatus: string) => void;
notifyBeforeRemove: (file: BaseFileItem, fileList: Array) => boolean | Promise;
notifyBeforeClear: (fileList: Array) => boolean | Promise;
notifyChange: ({ currentFile, fileList }: {
currentFile: BaseFileItem | null;
fileList: Array;
}) => void;
updateLocalUrls: (urls: Array) => void;
notifyClear: () => void;
notifyPreviewClick: (file: any) => void;
notifyDrop: (e: any, files: Array, fileList: Array) => void;
notifyAcceptInvalid: (invalidFiles: Array) => void;
registerPastingHandler: (cb?: (params?: any) => void) => void;
unRegisterPastingHandler: () => void;
isMac: () => boolean;
notifyPastingError: (error: Error | PermissionStatus) => void;
}
declare class UploadFoundation, S = Record> extends BaseFoundation, P, S> {
destroyState: boolean;
constructor(adapter: UploadAdapter);
init(): void;
destroy(): void;
getError({ action, xhr, message, fileName }: {
action: string;
xhr: XMLHttpRequest;
message?: string;
fileName: string;
}): XhrError;
getBody(xhr: XMLHttpRequest): any;
checkFileSize(file: File): boolean;
/**
* 1. 选择文件
* 2. transform转换. 添加uid
* 3. 检查文件个数是否超出
* 若超出,不添加到list中,触发onExceed,中止流程
* 若未超出,执行以下流程
* 4. 检查文件尺寸,添加尺寸是否合法的标识
* 5. 检查uploadTrigger是否为'auto',若是执行步骤6-8
* 6. 遍历文件列表触发上传
* - 对尺寸不合适的不需要触发上传
* 7. beforeUpload
* - 对beforeUpload中设为不合法的不需要触发上传
* 8. TODO: check
* 9. afterUpload
*
* 1. Select file
* 2. transform, add uid
* 3. Check whether the number of files exceeds
* If it exceeds, it is not added to the list, trigger onExceed, and abort the process
* If it is not exceeded, execute the following process
* 4. check the file size, add the size is legal logo
* 5. Check whether the uploadTrigger is'auto ', if so, perform steps 6-8
* 6. Traversing the file list triggers upload
* - No need to trigger uploads for inappropriate sizes
* 7. beforeUpload
* - no need to trigger upload if beforeUpload is not set to be valid
* 8. TODO: check
* 9. afterUpload
*/
handleChange(currentFileList: FileList | Array): void;
handleReplaceChange(currentFileList: FileList | Array): void;
buildFileItem(fileInstance: CustomFile, uploadTrigger: string): BaseFileItem;
replaceFileList(files: Array): void;
addFilesToList(files: Array): void;
insertFileToList(files: Array, index: number): void;
manualUpload(): void;
startUpload(fileList: Array): void;
upload(file: BaseFileItem): void;
handleBeforeUploadResultInObject(buResult: Partial, file: BaseFileItem): void;
post(file: BaseFileItem): void;
handleProgress({ e, fileInstance }: {
e?: ProgressEvent;
fileInstance: File;
}): void;
handleOnLoad({ e, xhr, fileInstance }: {
e?: ProgressEvent;
xhr: XMLHttpRequest;
fileInstance: File;
}): void;
handleSuccess({ e, fileInstance, isCustomRequest, xhr, response }: {
e?: ProgressEvent;
fileInstance: CustomFile;
isCustomRequest?: boolean;
xhr?: XMLHttpRequest;
response?: any;
}): void;
_getFileIndex(file: CustomFile | BaseFileItem, fileList: Array): number;
handleRemove(file: BaseFileItem): void;
handleError({ e, xhr, fileInstance }: {
e?: ProgressEvent;
xhr: XMLHttpRequest;
fileInstance: CustomFile;
}): void;
handleClear(): void;
_createURL(fileInstance: CustomFile): string;
releaseMemory(): void;
_releaseBlob(url: string): void;
isImage(file: CustomFile): boolean;
isMultiple(): boolean;
_dragEnterTarget: EventTarget;
handleDragEnter(e: any): void;
handleDirectoryDrop(e: any): Promise;
handleDrop(e: any): void;
handleDragOver(e: any): void;
handleDragLeave(e: any): void;
checkFileFormat(accept: string, file: File): boolean;
retry(fileItem: BaseFileItem): void;
handlePreviewClick(fileItem: BaseFileItem): void;
readFileFromClipboard(clipboardItems: any): void;
handlePasting(e: any): void;
bindPastingHandler(): void;
unbindPastingHandler(): void;
}
export default UploadFoundation;