2 * jQuery File Upload Validation Plugin
3 * https://github.com/blueimp/jQuery-File-Upload
5 * Copyright 2013, Sebastian Tschan
8 * Licensed under the MIT license:
9 * https://opensource.org/licenses/MIT
12 /* global define, require */
16 if (typeof define === 'function' && define.amd) {
17 // Register as an anonymous AMD module:
18 define(['jquery', './jquery.fileupload-process'], factory);
19 } else if (typeof exports === 'object') {
21 factory(require('jquery'), require('./jquery.fileupload-process'));
24 factory(window.jQuery);
29 // Append to the default processQueue:
30 $.blueimp.fileupload.prototype.options.processQueue.push({
32 // Always trigger this action,
33 // even if the previous action was rejected:
35 // Options taken from the global options map:
39 maxNumberOfFiles: '@',
40 disabled: '@disableValidation'
43 // The File Upload Validation plugin extends the fileupload widget
44 // with file validation functionality:
45 $.widget('blueimp.fileupload', $.blueimp.fileupload, {
48 // The regular expression for allowed file types, matches
49 // against either file type or file name:
50 acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
51 // The maximum allowed file size in bytes:
52 maxFileSize: 10000000, // 10 MB
53 // The minimum allowed file size in bytes:
54 minFileSize: undefined, // No minimal file size
55 // The limit of files to be uploaded:
59 // Function returning the current number of files,
60 // has to be overridden for maxNumberOfFiles validation:
61 getNumberOfFiles: $.noop,
63 // Error and info messages:
65 maxNumberOfFiles: 'Maximum number of files exceeded',
66 acceptFileTypes: 'File type not allowed',
67 maxFileSize: 'File is too large',
68 minFileSize: 'File is too small'
73 validate: function (data, options) {
74 if (options.disabled) {
77 // eslint-disable-next-line new-cap
78 var dfd = $.Deferred(),
79 settings = this.options,
80 file = data.files[data.index],
82 if (options.minFileSize || options.maxFileSize) {
86 $.type(options.maxNumberOfFiles) === 'number' &&
87 (settings.getNumberOfFiles() || 0) + data.files.length >
88 options.maxNumberOfFiles
90 file.error = settings.i18n('maxNumberOfFiles');
92 options.acceptFileTypes &&
94 options.acceptFileTypes.test(file.type) ||
95 options.acceptFileTypes.test(file.name)
98 file.error = settings.i18n('acceptFileTypes');
99 } else if (fileSize > options.maxFileSize) {
100 file.error = settings.i18n('maxFileSize');
102 $.type(fileSize) === 'number' &&
103 fileSize < options.minFileSize
105 file.error = settings.i18n('minFileSize');
109 if (file.error || data.files.error) {
110 data.files.error = true;
111 dfd.rejectWith(this, [data]);
113 dfd.resolveWith(this, [data]);
115 return dfd.promise();