| 24 |
lars |
1 |
/*
|
|
|
2 |
* jQuery File Upload Validation Plugin
|
|
|
3 |
* https://github.com/blueimp/jQuery-File-Upload
|
|
|
4 |
*
|
|
|
5 |
* Copyright 2013, Sebastian Tschan
|
|
|
6 |
* https://blueimp.net
|
|
|
7 |
*
|
|
|
8 |
* Licensed under the MIT license:
|
|
|
9 |
* https://opensource.org/licenses/MIT
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/* global define, require */
|
|
|
13 |
|
|
|
14 |
(function (factory) {
|
|
|
15 |
'use strict';
|
|
|
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') {
|
|
|
20 |
// Node/CommonJS:
|
|
|
21 |
factory(require('jquery'), require('./jquery.fileupload-process'));
|
|
|
22 |
} else {
|
|
|
23 |
// Browser globals:
|
|
|
24 |
factory(window.jQuery);
|
|
|
25 |
}
|
|
|
26 |
})(function ($) {
|
|
|
27 |
'use strict';
|
|
|
28 |
|
|
|
29 |
// Append to the default processQueue:
|
|
|
30 |
$.blueimp.fileupload.prototype.options.processQueue.push({
|
|
|
31 |
action: 'validate',
|
|
|
32 |
// Always trigger this action,
|
|
|
33 |
// even if the previous action was rejected:
|
|
|
34 |
always: true,
|
|
|
35 |
// Options taken from the global options map:
|
|
|
36 |
acceptFileTypes: '@',
|
|
|
37 |
maxFileSize: '@',
|
|
|
38 |
minFileSize: '@',
|
|
|
39 |
maxNumberOfFiles: '@',
|
|
|
40 |
disabled: '@disableValidation'
|
|
|
41 |
});
|
|
|
42 |
|
|
|
43 |
// The File Upload Validation plugin extends the fileupload widget
|
|
|
44 |
// with file validation functionality:
|
|
|
45 |
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
|
|
|
46 |
options: {
|
|
|
47 |
/*
|
|
|
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:
|
|
|
56 |
maxNumberOfFiles: 10,
|
|
|
57 |
*/
|
|
|
58 |
|
|
|
59 |
// Function returning the current number of files,
|
|
|
60 |
// has to be overridden for maxNumberOfFiles validation:
|
|
|
61 |
getNumberOfFiles: $.noop,
|
|
|
62 |
|
|
|
63 |
// Error and info messages:
|
|
|
64 |
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'
|
|
|
69 |
}
|
|
|
70 |
},
|
|
|
71 |
|
|
|
72 |
processActions: {
|
|
|
73 |
validate: function (data, options) {
|
|
|
74 |
if (options.disabled) {
|
|
|
75 |
return data;
|
|
|
76 |
}
|
|
|
77 |
// eslint-disable-next-line new-cap
|
|
|
78 |
var dfd = $.Deferred(),
|
|
|
79 |
settings = this.options,
|
|
|
80 |
file = data.files[data.index],
|
|
|
81 |
fileSize;
|
|
|
82 |
if (options.minFileSize || options.maxFileSize) {
|
|
|
83 |
fileSize = file.size;
|
|
|
84 |
}
|
|
|
85 |
if (
|
|
|
86 |
$.type(options.maxNumberOfFiles) === 'number' &&
|
|
|
87 |
(settings.getNumberOfFiles() || 0) + data.files.length >
|
|
|
88 |
options.maxNumberOfFiles
|
|
|
89 |
) {
|
|
|
90 |
file.error = settings.i18n('maxNumberOfFiles');
|
|
|
91 |
} else if (
|
|
|
92 |
options.acceptFileTypes &&
|
|
|
93 |
!(
|
|
|
94 |
options.acceptFileTypes.test(file.type) ||
|
|
|
95 |
options.acceptFileTypes.test(file.name)
|
|
|
96 |
)
|
|
|
97 |
) {
|
|
|
98 |
file.error = settings.i18n('acceptFileTypes');
|
|
|
99 |
} else if (fileSize > options.maxFileSize) {
|
|
|
100 |
file.error = settings.i18n('maxFileSize');
|
|
|
101 |
} else if (
|
|
|
102 |
$.type(fileSize) === 'number' &&
|
|
|
103 |
fileSize < options.minFileSize
|
|
|
104 |
) {
|
|
|
105 |
file.error = settings.i18n('minFileSize');
|
|
|
106 |
} else {
|
|
|
107 |
delete file.error;
|
|
|
108 |
}
|
|
|
109 |
if (file.error || data.files.error) {
|
|
|
110 |
data.files.error = true;
|
|
|
111 |
dfd.rejectWith(this, [data]);
|
|
|
112 |
} else {
|
|
|
113 |
dfd.resolveWith(this, [data]);
|
|
|
114 |
}
|
|
|
115 |
return dfd.promise();
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
});
|
|
|
119 |
});
|