Compare commits
7 Commits
koenvander
...
duykhang53
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e5fb25e22 | ||
|
|
7b9401e8f0 | ||
|
|
8878aa2f88 | ||
|
|
025162d345 | ||
|
|
02b0c1db60 | ||
|
|
732eb2d2a9 | ||
|
|
6226188df3 |
11
.travis.yml
11
.travis.yml
@@ -1,11 +1,13 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "6"
|
||||
- "10.15.0"
|
||||
|
||||
sudo: required
|
||||
services:
|
||||
- xvfb
|
||||
|
||||
before_install:
|
||||
- export CHROME_BIN=chromium-browser
|
||||
- export DISPLAY=:99.0
|
||||
- sh -e /etc/init.d/xvfb start
|
||||
- google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
|
||||
|
||||
script:
|
||||
- npm run pretest
|
||||
@@ -15,6 +17,7 @@ after_success:
|
||||
- ./node_modules/.bin/codecov
|
||||
|
||||
addons:
|
||||
chrome: stable
|
||||
firefox: "latest"
|
||||
apt:
|
||||
sources:
|
||||
|
||||
@@ -67,7 +67,6 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
|
||||
5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called.
|
||||
6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false.
|
||||
7. `parametersBeforeFiles` - States if additional parameters should be appended before or after the file. Defaults to false.
|
||||
8. `queueMaxSizeLimit` - States the maximum allowed size (in bytes) of all files in the queue that are going to be uploaded.
|
||||
|
||||
### Events
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ module.exports = function (config) {
|
||||
|
||||
if (process.env.TRAVIS) {
|
||||
configuration.browsers = ['Chrome_travis_ci'];
|
||||
configuration.singleRun = true;
|
||||
}
|
||||
|
||||
if (process.env.SAUCE) {
|
||||
|
||||
16759
package-lock.json
generated
Normal file
16759
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -84,8 +84,8 @@
|
||||
"jasmine-core": "2.5.2",
|
||||
"jasmine-data-provider": "2.2.0",
|
||||
"jasmine-spec-reporter": "3.2.0",
|
||||
"karma": "1.4.0",
|
||||
"karma-chrome-launcher": "^2.0.0",
|
||||
"karma": "^4.2.0",
|
||||
"karma-chrome-launcher": "^3.1.0",
|
||||
"karma-cli": "^1.0.1",
|
||||
"karma-coverage-istanbul-reporter": "^1.3.0",
|
||||
"karma-jasmine": "^1.0.2",
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { Directive, EventEmitter, ElementRef, HostListener, Input, Output } from '@angular/core';
|
||||
import { Directive, ElementRef, EventEmitter, HostListener, Input, Output } from '@angular/core';
|
||||
|
||||
import { FileUploader, FileUploaderOptions } from './file-uploader.class';
|
||||
import { FileUploader, FileUploaderOptions, FilterFunction } from './file-uploader.class';
|
||||
|
||||
@Directive({ selector: '[ng2FileDrop]' })
|
||||
export class FileDropDirective {
|
||||
@Input() public uploader: FileUploader;
|
||||
// tslint:disable-next-line:no-input-rename
|
||||
@Input('ng2FileFilter') public filter: FilterFunction['fn'];
|
||||
@Output() public fileOver: EventEmitter<any> = new EventEmitter();
|
||||
@Output() public onFileDrop: EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
@Output() public onFileDrop: EventEmitter<FileList> = new EventEmitter<FileList>();
|
||||
|
||||
protected element: ElementRef;
|
||||
|
||||
@@ -30,7 +32,10 @@ export class FileDropDirective {
|
||||
}
|
||||
|
||||
let options = this.getOptions();
|
||||
let filters = this.getFilters();
|
||||
let filters = typeof this.filter === 'function' ? [{
|
||||
name: 'ng2FileDropDirectiveFilter',
|
||||
fn: this.filter
|
||||
}, ...options.filters] : this.getFilters();
|
||||
this._preventAndStop(event);
|
||||
this.uploader.addToQueue(transfer.files, options, filters);
|
||||
this.fileOver.emit(false);
|
||||
@@ -61,11 +66,11 @@ export class FileDropDirective {
|
||||
this.fileOver.emit(false);
|
||||
}
|
||||
|
||||
protected _getTransfer(event: any): any {
|
||||
protected _getTransfer(event: any): DragEvent['dataTransfer'] {
|
||||
return event.dataTransfer ? event.dataTransfer : event.originalEvent.dataTransfer; // jQuery fix;
|
||||
}
|
||||
|
||||
protected _preventAndStop(event: any): any {
|
||||
protected _preventAndStop(event: Event): any {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { Directive, EventEmitter, ElementRef, Input, HostListener, Output } from '@angular/core';
|
||||
import { Directive, ElementRef, EventEmitter, HostListener, Input, Output } from '@angular/core';
|
||||
|
||||
import { FileUploader } from './file-uploader.class';
|
||||
import { FileUploader, FilterFunction } from './file-uploader.class';
|
||||
|
||||
@Directive({ selector: '[ng2FileSelect]' })
|
||||
export class FileSelectDirective {
|
||||
@Input() public uploader: FileUploader;
|
||||
// tslint:disable-next-line:no-input-rename
|
||||
@Input('ng2FileFilter') public filter: FilterFunction['fn'];
|
||||
@Output() public onFileSelected: EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
|
||||
protected element: ElementRef;
|
||||
@@ -29,7 +31,10 @@ export class FileSelectDirective {
|
||||
public onChange(): any {
|
||||
let files = this.element.nativeElement.files;
|
||||
let options = this.getOptions();
|
||||
let filters = this.getFilters();
|
||||
let filters = typeof this.filter === 'function' ? [{
|
||||
name: 'ng2FileSelectDirectiveFilter',
|
||||
fn: this.filter
|
||||
}, ...options.filters] : this.getFilters();
|
||||
|
||||
this.uploader.addToQueue(files, options, filters);
|
||||
this.onFileSelected.emit(files);
|
||||
|
||||
@@ -14,9 +14,9 @@ export interface Headers {
|
||||
|
||||
export type ParsedResponseHeaders = { [headerFieldName: string]: string };
|
||||
|
||||
export type FilterFunction = {
|
||||
name: string,
|
||||
fn: (item?: FileLikeObject, options?: FileUploaderOptions) => boolean
|
||||
export interface FilterFunction {
|
||||
name: string;
|
||||
fn(this: FileUploader, item?: FileLikeObject, options?: FileUploaderOptions, queueIndex?: number): boolean | Promise<boolean>;
|
||||
};
|
||||
|
||||
export interface FileUploaderOptions {
|
||||
@@ -30,7 +30,6 @@ export interface FileUploaderOptions {
|
||||
authToken?: string;
|
||||
maxFileSize?: number;
|
||||
queueLimit?: number;
|
||||
queueMaxSizeLimit? : number;
|
||||
removeAfterUpload?: boolean;
|
||||
url?: string;
|
||||
disableMultipart?: boolean;
|
||||
@@ -78,10 +77,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,26 +89,30 @@ 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 (const q of this.queue) {
|
||||
q.url = this.options.url;
|
||||
}
|
||||
}
|
||||
|
||||
public addToQueue(files: File[], options?: FileUploaderOptions, filters?: FilterFunction[] | string): void {
|
||||
public async addToQueue(files: FileList, options?: FileUploaderOptions, filters?: FilterFunction[] | string): Promise<void> {
|
||||
let list: File[] = [];
|
||||
for (let file of files) {
|
||||
list.push(file);
|
||||
// tslint:disable-next-line:prefer-for-of
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
list.push(files[i]);
|
||||
}
|
||||
|
||||
let arrayOfFilters = this._getFilters(filters);
|
||||
let count = this.queue.length;
|
||||
let addedFileItems: FileItem[] = [];
|
||||
list.map((some: File) => {
|
||||
let idx = 0;
|
||||
|
||||
for (const some of list) {
|
||||
if (!options) {
|
||||
options = this.options;
|
||||
}
|
||||
|
||||
let temp = new FileLikeObject(some);
|
||||
if (this._isValidFile(temp, arrayOfFilters, options)) {
|
||||
if (await this._isValidFile(temp, arrayOfFilters, options, idx++)) {
|
||||
let fileItem = new FileItem(this, some, options);
|
||||
addedFileItems.push(fileItem);
|
||||
this.queue.push(fileItem);
|
||||
@@ -122,7 +121,7 @@ export class FileUploader {
|
||||
let filter = arrayOfFilters[this._failFilterIndex];
|
||||
this._onWhenAddingFileFailed(temp, filter, options);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (this.queue.length !== count) {
|
||||
this._onAfterAddingAll(addedFileItems);
|
||||
this.progress = this._getTotalProgress();
|
||||
@@ -376,11 +375,11 @@ export class FileUploader {
|
||||
if (this.authToken) {
|
||||
xhr.setRequestHeader(this.authTokenHeader, this.authToken);
|
||||
}
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
||||
that.response.emit(xhr.responseText)
|
||||
}
|
||||
xhr.onreadystatechange = function (): void {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
that.response.emit(xhr.responseText);
|
||||
}
|
||||
};
|
||||
if (this.options.formatDataFunctionIsAsync) {
|
||||
sendable.then(
|
||||
(result: any) => xhr.send(JSON.stringify(result))
|
||||
@@ -425,23 +424,18 @@ 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);
|
||||
protected async _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions, queueIndex: number): Promise<boolean> {
|
||||
this._failFilterIndex = -1;
|
||||
|
||||
for (const filter of filters) {
|
||||
this._failFilterIndex++;
|
||||
|
||||
if (!(await Promise.resolve(filter.fn.call(this, file, options, queueIndex)))) {
|
||||
return false;
|
||||
}
|
||||
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) => {
|
||||
this._failFilterIndex++;
|
||||
return filter.fn.call(this, file, options);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
protected _isSuccessCode(status: number): boolean {
|
||||
|
||||
@@ -63,7 +63,6 @@ describe('Directive: FileSelectDirective', () => {
|
||||
expect(options.isHTML5).toBeTruthy();
|
||||
expect(options.removeAfterUpload).toBeFalsy();
|
||||
expect(options.disableMultipart).toBeFalsy();
|
||||
expect(options.queueMaxSizeLimit).toBeUndefined();
|
||||
});
|
||||
|
||||
it('can get filters', () => {
|
||||
|
||||
Reference in New Issue
Block a user