Compare commits

..

1 Commits

Author SHA1 Message Date
Marc
dff713e550 allow to specify number of parallel uploads 2018-11-13 07:38:40 +01:00
2 changed files with 12 additions and 11 deletions

View File

@@ -100,7 +100,6 @@ export class FileType {
'tiff': 'image', 'tiff': 'image',
'cr2': 'image', 'cr2': 'image',
'dwg': 'image', 'dwg': 'image',
'dwf': 'image',
'cdr': 'image', 'cdr': 'image',
'ai': 'image', 'ai': 'image',
'indd': 'image', 'indd': 'image',

View File

@@ -39,12 +39,12 @@ export interface FileUploaderOptions {
parametersBeforeFiles?: boolean; parametersBeforeFiles?: boolean;
formatDataFunction?: Function; formatDataFunction?: Function;
formatDataFunctionIsAsync?: boolean; formatDataFunctionIsAsync?: boolean;
parallelUploads?: number;
} }
export class FileUploader { export class FileUploader {
public authToken: string; public authToken: string;
public isUploading: boolean = false;
public queue: FileItem[] = []; public queue: FileItem[] = [];
public progress: number = 0; public progress: number = 0;
public _nextIndex: number = 0; public _nextIndex: number = 0;
@@ -52,6 +52,10 @@ export class FileUploader {
public authTokenHeader: string; public authTokenHeader: string;
public response: EventEmitter<any>; public response: EventEmitter<any>;
public get isUploading(): boolean {
return this.queue.some((item: FileItem) => item.isUploading);
}
public options: FileUploaderOptions = { public options: FileUploaderOptions = {
autoUpload: false, autoUpload: false,
isHTML5: true, isHTML5: true,
@@ -89,8 +93,8 @@ export class FileUploader {
this.options.filters.unshift({ name: 'mimeType', fn: this._mimeTypeFilter }); this.options.filters.unshift({ name: 'mimeType', fn: this._mimeTypeFilter });
} }
for (let i = 0; i < this.queue.length; i++) { for (let item of this.queue) {
this.queue[ i ].url = this.options.url; item.url = this.options.url;
} }
} }
@@ -150,10 +154,6 @@ export class FileUploader {
let item = this.queue[ index ]; let item = this.queue[ index ];
let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport'; let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport';
item._prepareToUploading(); item._prepareToUploading();
if (this.isUploading) {
return;
}
this.isUploading = true;
(this as any)[ transport ](item); (this as any)[ transport ](item);
} }
@@ -172,7 +172,10 @@ export class FileUploader {
return; return;
} }
items.map((item: FileItem) => item._prepareToUploading()); 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 { public cancelAll(): void {
@@ -276,7 +279,6 @@ export class FileUploader {
item._onComplete(response, status, headers); item._onComplete(response, status, headers);
this.onCompleteItem(item, response, status, headers); this.onCompleteItem(item, response, status, headers);
let nextItem = this.getReadyItems()[ 0 ]; let nextItem = this.getReadyItems()[ 0 ];
this.isUploading = false;
if (nextItem) { if (nextItem) {
nextItem.upload(); nextItem.upload();
return; return;
@@ -373,7 +375,7 @@ export class FileUploader {
} }
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.readyState == XMLHttpRequest.DONE) {
that.response.emit(xhr.responseText) that.response.emit(xhr.responseText);
} }
} }
if (this.options.formatDataFunctionIsAsync) { if (this.options.formatDataFunctionIsAsync) {