Skip to content

Commit

Permalink
Added allowedContentTypes. If the Content-Type of a file is not allow…
Browse files Browse the repository at this point in the history
…ed, the file will rejected.
  • Loading branch information
retailify authored and jkuri committed Nov 9, 2017
1 parent 6b0c1e1 commit f13c1bd
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ lib/
*.ngsummary.json
*.ngfactory.ts
*.d.ts
yarn.lock
.idea
8 changes: 5 additions & 3 deletions src/components/app-home/app-home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class AppHomeComponent {
options: UploaderOptions;

constructor() {
this.options = { concurrency: 1 };
this.options = { concurrency: 1, allowedContentTypes: ['image/png', 'image/jpeg', 'image/gif'] };
this.files = [];
this.uploadInput = new EventEmitter<UploadInput>();
this.humanizeBytes = humanizeBytes;
Expand All @@ -24,7 +24,7 @@ export class AppHomeComponent {
if (output.type === 'allAddedToQueue') {
const event: UploadInput = {
type: 'uploadAll',
url: 'http://ngx-uploader.com/upload',
url: 'http://localhost:4900/upload',
method: 'POST',
data: { foo: 'bar' }
};
Expand All @@ -43,6 +43,8 @@ export class AppHomeComponent {
this.dragOver = false;
} else if (output.type === 'drop') {
this.dragOver = false;
} else if (output.type === 'rejected' && typeof output.file !== 'undefined') {
console.log(output.file.name + ' rejected');
}

this.files = this.files.filter(file => file.progress.status !== UploadStatus.Done);
Expand All @@ -51,7 +53,7 @@ export class AppHomeComponent {
startUpload(): void {
const event: UploadInput = {
type: 'uploadAll',
url: 'http://ngx-uploader.com/upload',
url: 'http://localhost:4900/upload',
method: 'POST',
data: { foo: 'bar' }
};
Expand Down
3 changes: 2 additions & 1 deletion src/ngx-uploader/classes/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Subscription } from 'rxjs/Subscription';

export interface UploaderOptions {
concurrency: number;
allowedContentTypes?: string[];
}

export interface BlobFile extends Blob {
Expand Down Expand Up @@ -45,7 +46,7 @@ export interface UploadFile {

export interface UploadOutput {
type: 'addedToQueue' | 'allAddedToQueue' | 'uploading' | 'done' | 'start' | 'cancelled' | 'dragOver'
| 'dragOut' | 'drop' | 'removed' | 'removedAll';
| 'dragOut' | 'drop' | 'removed' | 'removedAll' | 'rejected';
file?: UploadFile;
nativeFile?: File;
}
Expand Down
85 changes: 60 additions & 25 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,35 @@ export class NgUploaderService {
serviceEvents: EventEmitter<UploadOutput>;
uploadScheduler: Subject<{ file: UploadFile, event: UploadInput }>;
subs: { id: string, sub: Subscription }[];
contentTypes: string[];

constructor(concurrency: number = Number.POSITIVE_INFINITY) {
constructor(concurrency: number = Number.POSITIVE_INFINITY, contentTypes: string[] = ['*']) {
this.queue = [];
this.serviceEvents = new EventEmitter<any>();
this.uploadScheduler = new Subject();
this.subs = [];
this.contentTypes = contentTypes;

this.uploadScheduler
.mergeMap(upload => this.startUpload(upload), concurrency)
.subscribe(uploadOutput => this.serviceEvents.emit(uploadOutput));
}

handleFiles(incomingFiles: FileList): void {
this.queue.push(...[].map.call(incomingFiles, (file: File, i: number) => {
const uploadFile: UploadFile = {
fileIndex: i,
id: this.generateId(),
name: file.name,
size: file.size,
type: file.type,
form: new FormData(),
progress: {
status: UploadStatus.Queue,
data: {
percentage: 0,
speed: 0,
speedHuman: `${humanizeBytes(0)}/s`,
startTime: null,
endTime: null,
eta: null,
etaHuman: null
}
},
lastModifiedDate: file.lastModifiedDate,
sub: undefined,
nativeFile: file
};
let allowedIncomingFiles: File[] = [];

for (let i = 0; i < incomingFiles.length; i++) {
let checkFile = incomingFiles[i];
if (this.isContentTypeAllowed(checkFile.type)) {
allowedIncomingFiles.push(checkFile);
} else {
const rejectedFile: UploadFile = this.makeUploadFile(checkFile, i);
this.serviceEvents.emit({ type: 'rejected', file: rejectedFile });
}
}

this.queue.push(...[].map.call(allowedIncomingFiles, (file: File, i: number) => {
const uploadFile: UploadFile = this.makeUploadFile(file, i);
this.serviceEvents.emit({ type: 'addedToQueue', file: uploadFile });
return uploadFile;
}));
Expand Down Expand Up @@ -256,4 +247,48 @@ export class NgUploaderService {
generateId(): string {
return Math.random().toString(36).substring(7);
}

private allContentTypesAllowed(): boolean {
if (this.contentTypes.find((type: string) => type === '*') !== undefined) {
return true;
}
return false;
}

private isContentTypeAllowed(mimetype: string): boolean {
if (this.allContentTypesAllowed()) {
return true;
}
if (this.contentTypes.find((type: string) => type === mimetype ) != undefined) {
return true;
}

return false;
}

private makeUploadFile(file: File, index: number): UploadFile {
return {
fileIndex: index,
id: this.generateId(),
name: file.name,
size: file.size,
type: file.type,
form: new FormData(),
progress: {
status: UploadStatus.Queue,
data: {
percentage: 0,
speed: 0,
speedHuman: `${humanizeBytes(0)}/s`,
startTime: null,
endTime: null,
eta: null,
etaHuman: null
}
},
lastModifiedDate: file.lastModifiedDate,
sub: undefined,
nativeFile: file
};
}
}
3 changes: 2 additions & 1 deletion src/ngx-uploader/directives/ng-file-drop.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export class NgFileDropDirective implements OnInit, OnDestroy {
ngOnInit() {
this._sub = [];
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency);
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
this.upload = new NgUploaderService(concurrency, allowedContentTypes);

this.el = this.elementRef.nativeElement;

Expand Down
3 changes: 2 additions & 1 deletion src/ngx-uploader/directives/ng-file-select.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export class NgFileSelectDirective implements OnInit, OnDestroy {
ngOnInit() {
this._sub = [];
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency);
const allowedContentTypes = this.options && this.options.allowedContentTypes || ['*'];
this.upload = new NgUploaderService(concurrency, allowedContentTypes);

this.el = this.elementRef.nativeElement;
this.el.addEventListener('change', this.fileListener, false);
Expand Down

0 comments on commit f13c1bd

Please sign in to comment.