UNPKG

ngx-dropzone-next

Version:

A highly configurable dropzone component for Angular.

74 lines (71 loc) 3.02 kB
import * as i0 from '@angular/core'; import { Injectable } from '@angular/core'; /** * This service contains the filtering logic to be applied to * any dropped or selected file. If a file matches all criteria * like maximum size or accept type, it will be emitted in the * addedFiles array, otherwise in the rejectedFiles array. */ class NgxDropzoneService { parseFileList(files, accept, maxFileSize, multiple) { const addedFiles = []; const rejectedFiles = []; for (let i = 0; i < files.length; i++) { const file = files.item(i); if (!this.isAccepted(file, accept)) { this.rejectFile(rejectedFiles, file, 'type'); continue; } if (maxFileSize && file.size > maxFileSize) { this.rejectFile(rejectedFiles, file, 'size'); continue; } if (!multiple && addedFiles.length >= 1) { this.rejectFile(rejectedFiles, file, 'no_multiple'); continue; } addedFiles.push(file); } const result = { addedFiles, rejectedFiles, }; return result; } isAccepted(file, accept) { if (accept === '*') { return true; } const acceptFiletypes = accept .split(',') .map((it) => it.toLowerCase().trim()); const filetype = file.type.toLowerCase(); const filename = file.name.toLowerCase(); const matchedFileType = acceptFiletypes.find((acceptFiletype) => { // check for wildcard mimetype (e.g. image/*) if (acceptFiletype.endsWith('/*')) { return filetype.split('/')[0] === acceptFiletype.split('/')[0]; } // check for file extension (e.g. .csv) if (acceptFiletype.startsWith('.')) { return filename.endsWith(acceptFiletype); } // check for exact mimetype match (e.g. image/jpeg) return acceptFiletype == filetype; }); return !!matchedFileType; } rejectFile(rejectedFiles, file, reason) { const rejectedFile = file; rejectedFile.reason = reason; rejectedFiles.push(rejectedFile); } /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: NgxDropzoneService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } /** @nocollapse */ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: NgxDropzoneService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: NgxDropzoneService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }] }); export { NgxDropzoneService }; //# sourceMappingURL=ngx-dropzone-next-ngx-dropzone.service-DgjHzr4g.mjs.map