|
|
|
|
@@ -30,6 +30,7 @@ export interface FileUploaderOptions {
|
|
|
|
|
authToken?: string;
|
|
|
|
|
maxFileSize?: number;
|
|
|
|
|
queueLimit?: number;
|
|
|
|
|
queueMaxSizeLimit? : number;
|
|
|
|
|
removeAfterUpload?: boolean;
|
|
|
|
|
url?: string;
|
|
|
|
|
disableMultipart?: boolean;
|
|
|
|
|
@@ -39,7 +40,6 @@ export interface FileUploaderOptions {
|
|
|
|
|
parametersBeforeFiles?: boolean;
|
|
|
|
|
formatDataFunction?: Function;
|
|
|
|
|
formatDataFunctionIsAsync?: boolean;
|
|
|
|
|
uploadFilesInSingleRequest?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class FileUploader {
|
|
|
|
|
@@ -60,24 +60,28 @@ export class FileUploader {
|
|
|
|
|
removeAfterUpload: false,
|
|
|
|
|
disableMultipart: false,
|
|
|
|
|
formatDataFunction: (item: FileItem) => item._file,
|
|
|
|
|
formatDataFunctionIsAsync: false,
|
|
|
|
|
uploadFilesInSingleRequest : false
|
|
|
|
|
formatDataFunctionIsAsync: false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
protected _failFilterIndex: number;
|
|
|
|
|
|
|
|
|
|
public constructor(options: FileUploaderOptions) {
|
|
|
|
|
this.setOptions(options);
|
|
|
|
|
this.response = new EventEmitter<any>();
|
|
|
|
|
this.response = new EventEmitter<any>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setOptions(options: FileUploaderOptions): void {
|
|
|
|
|
this.options = Object.assign(this.options, options);
|
|
|
|
|
|
|
|
|
|
this.authToken = this.options.authToken;
|
|
|
|
|
this.authTokenHeader = this.options.authTokenHeader || 'Authorization';
|
|
|
|
|
this.autoUpload = this.options.autoUpload;
|
|
|
|
|
this.options.filters.unshift({ name: 'queueLimit', fn: this._queueLimitFilter });
|
|
|
|
|
|
|
|
|
|
if (this.options.queueMaxSizeLimit) {
|
|
|
|
|
this.options.filters.unshift({ name: 'queueMaxSizeLimit', fn: this._queueMaxSizeLimitFilter });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.options.maxFileSize) {
|
|
|
|
|
this.options.filters.unshift({ name: 'fileSize', fn: this._fileSizeFilter });
|
|
|
|
|
}
|
|
|
|
|
@@ -146,24 +150,16 @@ export class FileUploader {
|
|
|
|
|
this.progress = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uploadItem(value: FileItem): void {
|
|
|
|
|
this.uploadItems(new Array<FileItem>(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uploadItems(values: FileItem[]): void {
|
|
|
|
|
values.forEach(element => {
|
|
|
|
|
let index = this.getIndexOfItem(element);
|
|
|
|
|
let item = this.queue[ index ];
|
|
|
|
|
item._prepareToUploading();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
public uploadItem(value: FileItem): void {
|
|
|
|
|
let index = this.getIndexOfItem(value);
|
|
|
|
|
let item = this.queue[ index ];
|
|
|
|
|
let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport';
|
|
|
|
|
|
|
|
|
|
item._prepareToUploading();
|
|
|
|
|
if (this.isUploading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.isUploading = true;
|
|
|
|
|
(this as any)[ transport ](values);
|
|
|
|
|
this.isUploading = true;
|
|
|
|
|
(this as any)[ transport ](item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public cancelItem(value: FileItem): void {
|
|
|
|
|
@@ -175,20 +171,13 @@ export class FileUploader {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uploadAll(): void {
|
|
|
|
|
public uploadAll(): void {
|
|
|
|
|
let items = this.getNotUploadedItems().filter((item: FileItem) => !item.isUploading);
|
|
|
|
|
if (!items.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items.map((item: FileItem) => item._prepareToUploading());
|
|
|
|
|
|
|
|
|
|
if (this.options.uploadFilesInSingleRequest){
|
|
|
|
|
this.uploadItems(items);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
items[ 0 ].upload();
|
|
|
|
|
}
|
|
|
|
|
items[ 0 ].upload();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public cancelAll(): void {
|
|
|
|
|
@@ -291,27 +280,12 @@ export class FileUploader {
|
|
|
|
|
public _onCompleteItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
|
|
|
|
|
item._onComplete(response, status, headers);
|
|
|
|
|
this.onCompleteItem(item, response, status, headers);
|
|
|
|
|
|
|
|
|
|
if (!this.options.uploadFilesInSingleRequest)
|
|
|
|
|
{
|
|
|
|
|
let nextItem = this.getReadyItems()[ 0 ];
|
|
|
|
|
this.isUploading = false;
|
|
|
|
|
if (nextItem) {
|
|
|
|
|
nextItem.upload();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.onCompleteAll();
|
|
|
|
|
this.progress = this._getTotalProgress();
|
|
|
|
|
this._render();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public _onCompleteAllItems(items: FileItem[], response: string, status: number, headers: ParsedResponseHeaders): void {
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
item._onComplete(response, status, headers);
|
|
|
|
|
this.onCompleteItem(item, response, status, headers);
|
|
|
|
|
});
|
|
|
|
|
let nextItem = this.getReadyItems()[ 0 ];
|
|
|
|
|
this.isUploading = false;
|
|
|
|
|
if (nextItem) {
|
|
|
|
|
nextItem.upload();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.onCompleteAll();
|
|
|
|
|
this.progress = this._getTotalProgress();
|
|
|
|
|
this._render();
|
|
|
|
|
@@ -326,36 +300,22 @@ export class FileUploader {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected _xhrTransport(items: FileItem[]): any {
|
|
|
|
|
protected _xhrTransport(item: FileItem): any {
|
|
|
|
|
let that = this;
|
|
|
|
|
let firstItem = items[0];
|
|
|
|
|
let xhr = firstItem._xhr = new XMLHttpRequest();
|
|
|
|
|
let xhr = item._xhr = new XMLHttpRequest();
|
|
|
|
|
let sendable: any;
|
|
|
|
|
this._onBeforeUploadItem(item);
|
|
|
|
|
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
this._onBeforeUploadItem(item);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
if (typeof item._file.size !== 'number') {
|
|
|
|
|
throw new TypeError('The file specified is no longer valid');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (typeof item._file.size !== 'number') {
|
|
|
|
|
throw new TypeError('The file specified is no longer valid');
|
|
|
|
|
}
|
|
|
|
|
if (!this.options.disableMultipart) {
|
|
|
|
|
sendable = new FormData();
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
this._onBuildItemForm(item, sendable);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const appendFiles = () => {
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
sendable.append(item.alias, item._file, item.file.name)
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
this._onBuildItemForm(item, sendable);
|
|
|
|
|
|
|
|
|
|
const appendFile = () => sendable.append(item.alias, item._file, item.file.name);
|
|
|
|
|
if (!this.options.parametersBeforeFiles) {
|
|
|
|
|
appendFiles();
|
|
|
|
|
appendFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For AWS, Additional Parameters must come BEFORE Files
|
|
|
|
|
@@ -363,55 +323,53 @@ export class FileUploader {
|
|
|
|
|
Object.keys(this.options.additionalParameter).forEach((key: string) => {
|
|
|
|
|
let paramVal = this.options.additionalParameter[ key ];
|
|
|
|
|
// Allow an additional parameter to include the filename
|
|
|
|
|
if (!this.options.uploadFilesInSingleRequest && typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0) {
|
|
|
|
|
paramVal = paramVal.replace('{{file_name}}', firstItem.file.name);
|
|
|
|
|
if (typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0) {
|
|
|
|
|
paramVal = paramVal.replace('{{file_name}}', item.file.name);
|
|
|
|
|
}
|
|
|
|
|
sendable.append(key, paramVal);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.options.parametersBeforeFiles) {
|
|
|
|
|
appendFiles();
|
|
|
|
|
appendFile();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
sendable = this.options.formatDataFunction(firstItem);
|
|
|
|
|
sendable = this.options.formatDataFunction(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xhr.upload.onprogress = (event: any) => {
|
|
|
|
|
let progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
|
|
|
|
|
this._onProgressItem(item, progress);
|
|
|
|
|
};
|
|
|
|
|
xhr.onload = () => {
|
|
|
|
|
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
|
|
|
|
let response = this._transformResponse(xhr.response, headers);
|
|
|
|
|
let gist = this._isSuccessCode(xhr.status) ? 'Success' : 'Error';
|
|
|
|
|
let method = '_on' + gist + 'Item';
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
(this as any)[ method ](item, response, xhr.status, headers);
|
|
|
|
|
});
|
|
|
|
|
this._onCompleteAllItems(items, response, xhr.status, headers);
|
|
|
|
|
(this as any)[ method ](item, response, xhr.status, headers);
|
|
|
|
|
this._onCompleteItem(item, response, xhr.status, headers);
|
|
|
|
|
};
|
|
|
|
|
xhr.onerror = () => {
|
|
|
|
|
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
|
|
|
|
let response = this._transformResponse(xhr.response, headers);
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
this._onErrorItem(firstItem, response, xhr.status, headers);
|
|
|
|
|
});
|
|
|
|
|
this._onCompleteAllItems(items, response, xhr.status, headers);
|
|
|
|
|
this._onErrorItem(item, response, xhr.status, headers);
|
|
|
|
|
this._onCompleteItem(item, response, xhr.status, headers);
|
|
|
|
|
};
|
|
|
|
|
xhr.onabort = () => {
|
|
|
|
|
let headers = this._parseHeaders(xhr.getAllResponseHeaders());
|
|
|
|
|
let response = this._transformResponse(xhr.response, headers);
|
|
|
|
|
items.forEach(item => {
|
|
|
|
|
this._onCancelItem(firstItem, response, xhr.status, headers);
|
|
|
|
|
});
|
|
|
|
|
this._onCompleteAllItems(items, response, xhr.status, headers);
|
|
|
|
|
this._onCancelItem(item, response, xhr.status, headers);
|
|
|
|
|
this._onCompleteItem(item, response, xhr.status, headers);
|
|
|
|
|
};
|
|
|
|
|
xhr.open(firstItem.method, firstItem.url, true);
|
|
|
|
|
xhr.withCredentials = firstItem.withCredentials;
|
|
|
|
|
xhr.open(item.method, item.url, true);
|
|
|
|
|
xhr.withCredentials = item.withCredentials;
|
|
|
|
|
if (this.options.headers) {
|
|
|
|
|
for (let header of this.options.headers) {
|
|
|
|
|
xhr.setRequestHeader(header.name, header.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (firstItem.headers.length) {
|
|
|
|
|
for (let header of firstItem.headers) {
|
|
|
|
|
if (item.headers.length) {
|
|
|
|
|
for (let header of item.headers) {
|
|
|
|
|
xhr.setRequestHeader(header.name, header.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -467,6 +425,17 @@ export class FileUploader {
|
|
|
|
|
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected _queueMaxSizeLimitFilter(item: FileLikeObject): boolean {
|
|
|
|
|
let queueFileSize = 0;
|
|
|
|
|
let queueNotProcessedItems = this.queue.filter(queuedItem => !(queuedItem.isSuccess || queuedItem.isCancel || queuedItem.isError));
|
|
|
|
|
if(queueNotProcessedItems.length>0)
|
|
|
|
|
{
|
|
|
|
|
// total size of all queued items that are going to be uploaded
|
|
|
|
|
queueFileSize = queueNotProcessedItems.map(queuedItem => queuedItem.file.size).reduce((fileSizeA,fileSizeB)=> fileSizeA + fileSizeB);
|
|
|
|
|
}
|
|
|
|
|
return this.options.queueMaxSizeLimit === undefined || (queueFileSize + item.size) < this.options.queueMaxSizeLimit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {
|
|
|
|
|
this._failFilterIndex = -1;
|
|
|
|
|
return !filters.length ? true : filters.every((filter: FilterFunction) => {
|
|
|
|
|
|