Skip to content

Commit

Permalink
Typefy files arrays (bleenco#211)
Browse files Browse the repository at this point in the history
* Add filter by size, through maxSize option

* Add filenameHeader option and handle no-multipart uploads

* Fix setRequestHeaders before opened xhr

* Fix array filter call

* Add brackets to if's, fix files iteration

* Set proper types on xhr payload

* Small style fixes

* Typefy Files arrays
  • Loading branch information
andrevmatos authored and jkuri committed Feb 16, 2017
1 parent e7b9fa1 commit c9d15fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/directives/ng-file-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class NgFileDropDirective implements OnChanges, OnInit {
@Output() onUploadRejected: EventEmitter<UploadRejected> = new EventEmitter<UploadRejected>();
@Output() beforeUpload: EventEmitter<UploadedFile> = new EventEmitter<UploadedFile>();

files: any[] = [];
files: File[] = [];

constructor(
@Inject(ElementRef) public el: ElementRef,
Expand All @@ -38,7 +38,7 @@ export class NgFileDropDirective implements OnChanges, OnInit {
this.uploader._emitter.subscribe((data: any) => {
this.onUpload.emit(data);
if (data.done && this.files && this.files.length) {
this.files = [].filter.call(this.files, (x: any) => x.name !== data.originalName);
this.files = [].filter.call(this.files, (f: File) => f.name !== data.originalName);
}
});

Expand Down Expand Up @@ -81,8 +81,8 @@ export class NgFileDropDirective implements OnChanges, OnInit {
e.stopPropagation();
e.preventDefault();
this.onFileOver.emit(false);
this.files = Array.from(e.dataTransfer.files);
if (this.files.length) {
this.files = Array.from<File>(e.dataTransfer.files);
if (this.files && this.files.length) {
this.uploader.addFilesToQueue(this.files);
}
}, false);
Expand All @@ -100,19 +100,19 @@ export class NgFileDropDirective implements OnChanges, OnInit {

@HostListener('change') onChange(): void {
this.files = this.el.nativeElement.files;
if (!this.files) {
if (!this.files || !this.files.length) {
return;
}

if (this.options.filterExtensions && this.options.allowedExtensions && this.files && this.files.length) {
this.files = [].filter.call(this.files, (f: any) => {
this.files = [].filter.call(this.files, (f: File) => {
let allowedExtensions = this.options.allowedExtensions || [];
if (allowedExtensions.indexOf(f.type) !== -1) {
return true;
}

let ext: string = f.name.split('.').pop();
if (allowedExtensions.indexOf(ext) !== -1 ) {
let ext = f.name.split('.').pop();
if (ext && allowedExtensions.indexOf(ext) !== -1 ) {
return true;
}

Expand All @@ -123,7 +123,7 @@ export class NgFileDropDirective implements OnChanges, OnInit {
}

if (this.options.maxSize > 0) {
this.files = [].filter.call(this.files, (f: any) => {
this.files = [].filter.call(this.files, (f: File) => {
if (f.size <= this.options.maxSize) {
return true;
}
Expand All @@ -133,7 +133,7 @@ export class NgFileDropDirective implements OnChanges, OnInit {
});
}

if (this.files.length) {
if (this.files && this.files.length) {
this.uploader.addFilesToQueue(this.files);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/directives/ng-file-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class NgFileSelectDirective implements OnChanges {
@Output() onUploadRejected: EventEmitter<UploadRejected> = new EventEmitter<UploadRejected>();
@Output() beforeUpload: EventEmitter<UploadedFile> = new EventEmitter<UploadedFile>();

files: any[] = [];
files: File[] = [];

constructor(
@Inject(ElementRef) public el: ElementRef,
Expand All @@ -42,7 +42,7 @@ export class NgFileSelectDirective implements OnChanges {
this.uploader._emitter.subscribe((data: any) => {
this.onUpload.emit(data);
if (data.done && this.files && this.files.length) {
this.files = [].filter.call(this.files, (x: any) => x.name !== data.originalName);
this.files = [].filter.call(this.files, (f: File) => f.name !== data.originalName);
}
});

Expand All @@ -65,19 +65,19 @@ export class NgFileSelectDirective implements OnChanges {

@HostListener('change') onChange(): void {
this.files = this.el.nativeElement.files;
if (!this.files) {
if (!this.files || !this.files.length) {
return;
}

if (this.options.filterExtensions && this.options.allowedExtensions && this.files && this.files.length) {
this.files = [].filter.call(this.files, (f: any) => {
this.files = [].filter.call(this.files, (f: File) => {
let allowedExtensions = this.options.allowedExtensions || [];
if (allowedExtensions.indexOf(f.type) !== -1) {
return true;
}

let ext: string = f.name.split('.').pop();
if (allowedExtensions.indexOf(ext) !== -1 ) {
let ext = f.name.split('.').pop();
if (ext && allowedExtensions.indexOf(ext) !== -1 ) {
return true;
}

Expand All @@ -88,7 +88,7 @@ export class NgFileSelectDirective implements OnChanges {
}

if (this.options.maxSize > 0) {
this.files = [].filter.call(this.files, (f: any) => {
this.files = [].filter.call(this.files, (f: File) => {
if (f.size <= this.options.maxSize) {
return true;
}
Expand All @@ -98,7 +98,7 @@ export class NgFileSelectDirective implements OnChanges {
});
}

if (this.files.length) {
if (this.files && this.files.length) {
this.uploader.addFilesToQueue(this.files);
}
}
Expand Down

0 comments on commit c9d15fb

Please sign in to comment.