|
|
|
|
@@ -30,7 +30,6 @@ export interface FileUploaderOptions {
|
|
|
|
|
authToken?: string;
|
|
|
|
|
maxFileSize?: number;
|
|
|
|
|
queueLimit?: number;
|
|
|
|
|
queueMaxSizeLimit? : number;
|
|
|
|
|
removeAfterUpload?: boolean;
|
|
|
|
|
url?: string;
|
|
|
|
|
disableMultipart?: boolean;
|
|
|
|
|
@@ -40,12 +39,12 @@ export interface FileUploaderOptions {
|
|
|
|
|
parametersBeforeFiles?: boolean;
|
|
|
|
|
formatDataFunction?: Function;
|
|
|
|
|
formatDataFunctionIsAsync?: boolean;
|
|
|
|
|
parallelUploads?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class FileUploader {
|
|
|
|
|
|
|
|
|
|
public authToken: string;
|
|
|
|
|
public isUploading: boolean = false;
|
|
|
|
|
public queue: FileItem[] = [];
|
|
|
|
|
public progress: number = 0;
|
|
|
|
|
public _nextIndex: number = 0;
|
|
|
|
|
@@ -53,6 +52,10 @@ export class FileUploader {
|
|
|
|
|
public authTokenHeader: string;
|
|
|
|
|
public response: EventEmitter<any>;
|
|
|
|
|
|
|
|
|
|
public get isUploading(): boolean {
|
|
|
|
|
return this.queue.some((item: FileItem) => item.isUploading);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public options: FileUploaderOptions = {
|
|
|
|
|
autoUpload: false,
|
|
|
|
|
isHTML5: true,
|
|
|
|
|
@@ -78,10 +81,6 @@ export class FileUploader {
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
@@ -94,8 +93,8 @@ export class FileUploader {
|
|
|
|
|
this.options.filters.unshift({ name: 'mimeType', fn: this._mimeTypeFilter });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.queue.length; i++) {
|
|
|
|
|
this.queue[ i ].url = this.options.url;
|
|
|
|
|
for (let item of this.queue) {
|
|
|
|
|
item.url = this.options.url;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -155,10 +154,6 @@ export class FileUploader {
|
|
|
|
|
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 ](item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -177,7 +172,10 @@ export class FileUploader {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
items.map((item: FileItem) => item._prepareToUploading());
|
|
|
|
|
items[ 0 ].upload();
|
|
|
|
|
let numberUploadsToStart = Math.min(items.length, this.options.parallelUploads || 1);
|
|
|
|
|
for (let i = 0; i < numberUploadsToStart; ++i) {
|
|
|
|
|
items[ i ].upload();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public cancelAll(): void {
|
|
|
|
|
@@ -281,7 +279,6 @@ export class FileUploader {
|
|
|
|
|
item._onComplete(response, status, headers);
|
|
|
|
|
this.onCompleteItem(item, response, status, headers);
|
|
|
|
|
let nextItem = this.getReadyItems()[ 0 ];
|
|
|
|
|
this.isUploading = false;
|
|
|
|
|
if (nextItem) {
|
|
|
|
|
nextItem.upload();
|
|
|
|
|
return;
|
|
|
|
|
@@ -378,7 +375,7 @@ export class FileUploader {
|
|
|
|
|
}
|
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
|
if (xhr.readyState == XMLHttpRequest.DONE) {
|
|
|
|
|
that.response.emit(xhr.responseText)
|
|
|
|
|
that.response.emit(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (this.options.formatDataFunctionIsAsync) {
|
|
|
|
|
@@ -425,17 +422,6 @@ 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) => {
|
|
|
|
|
|