|
1 | | -/* |
2 | | - * FilePondPluginFileValidateSize 2.1.1 |
3 | | - * Licensed under MIT, https://opensource.org/licenses/MIT |
4 | | - * Please visit https://pqina.nl/filepond for details. |
| 1 | +/*! |
| 2 | + * FilePondPluginFileValidateSize 2.1.2 |
| 3 | + * Licensed under MIT, https://opensource.org/licenses/MIT/ |
| 4 | + * Please visit https://pqina.nl/filepond/ for details. |
5 | 5 | */ |
6 | 6 |
|
7 | 7 | /* eslint-disable */ |
| 8 | + |
8 | 9 | (function(global, factory) { |
9 | 10 | typeof exports === 'object' && typeof module !== 'undefined' |
10 | 11 | ? (module.exports = factory()) |
11 | 12 | : typeof define === 'function' && define.amd |
12 | | - ? define(factory) |
13 | | - : (global.FilePondPluginFileValidateSize = factory()); |
| 13 | + ? define(factory) |
| 14 | + : ((global = global || self), |
| 15 | + (global.FilePondPluginFileValidateSize = factory())); |
14 | 16 | })(this, function() { |
15 | 17 | 'use strict'; |
16 | 18 |
|
17 | | - var plugin$1 = function(_ref) { |
| 19 | + var plugin = function plugin(_ref) { |
18 | 20 | var addFilter = _ref.addFilter, |
19 | 21 | utils = _ref.utils; |
20 | | - |
21 | 22 | // get quick reference to Type utils |
22 | 23 | var Type = utils.Type, |
23 | 24 | replaceInString = utils.replaceInString, |
24 | | - toNaturalFileSize = utils.toNaturalFileSize; |
25 | | - |
26 | | - // filtering if an item is allowed in hopper |
| 25 | + toNaturalFileSize = utils.toNaturalFileSize; // filtering if an item is allowed in hopper |
27 | 26 |
|
28 | 27 | addFilter('ALLOW_HOPPER_ITEM', function(file, _ref2) { |
29 | 28 | var query = _ref2.query; |
|
33 | 32 | } |
34 | 33 |
|
35 | 34 | var sizeMax = query('GET_MAX_FILE_SIZE'); |
| 35 | + |
36 | 36 | if (sizeMax !== null && file.size >= sizeMax) { |
37 | 37 | return false; |
38 | 38 | } |
39 | 39 |
|
40 | 40 | var sizeMin = query('GET_MIN_FILE_SIZE'); |
| 41 | + |
41 | 42 | if (sizeMin !== null && file.size <= sizeMin) { |
42 | 43 | return false; |
43 | 44 | } |
44 | 45 |
|
45 | 46 | return true; |
46 | | - }); |
47 | | - |
48 | | - // called for each file that is loaded |
| 47 | + }); // called for each file that is loaded |
49 | 48 | // right before it is set to the item state |
50 | 49 | // should return a promise |
| 50 | + |
51 | 51 | addFilter('LOAD_FILE', function(file, _ref3) { |
52 | 52 | var query = _ref3.query; |
53 | 53 | return new Promise(function(resolve, reject) { |
54 | 54 | // if not allowed, all fine, exit |
55 | 55 | if (!query('GET_ALLOW_FILE_SIZE_VALIDATION')) { |
56 | 56 | resolve(file); |
57 | 57 | return; |
58 | | - } |
| 58 | + } // reject or resolve based on file size |
59 | 59 |
|
60 | | - // reject or resolve based on file size |
61 | 60 | var sizeMax = query('GET_MAX_FILE_SIZE'); |
| 61 | + |
62 | 62 | if (sizeMax !== null && file.size >= sizeMax) { |
63 | 63 | reject({ |
64 | 64 | status: { |
|
69 | 69 | } |
70 | 70 | }); |
71 | 71 | return; |
72 | | - } |
| 72 | + } // reject or resolve based on file size |
73 | 73 |
|
74 | | - // reject or resolve based on file size |
75 | 74 | var sizeMin = query('GET_MIN_FILE_SIZE'); |
| 75 | + |
76 | 76 | if (sizeMin !== null && file.size <= sizeMin) { |
77 | 77 | reject({ |
78 | 78 | status: { |
|
83 | 83 | } |
84 | 84 | }); |
85 | 85 | return; |
86 | | - } |
| 86 | + } // returns the current option value |
87 | 87 |
|
88 | | - // returns the current option value |
89 | 88 | var totalSizeMax = query('GET_MAX_TOTAL_FILE_SIZE'); |
| 89 | + |
90 | 90 | if (totalSizeMax !== null) { |
91 | 91 | // get the current total file size |
92 | 92 | var currentTotalSize = query('GET_ACTIVE_ITEMS').reduce(function( |
|
95 | 95 | ) { |
96 | 96 | return total + item.fileSize; |
97 | 97 | }, |
98 | | - 0); |
| 98 | + 0); // get the size of the new file |
99 | 99 |
|
100 | | - // get the size of the new file |
101 | 100 | if (currentTotalSize > totalSizeMax) { |
102 | 101 | reject({ |
103 | 102 | status: { |
|
109 | 108 | }); |
110 | 109 | return; |
111 | 110 | } |
112 | | - } |
| 111 | + } // file is fine, let's pass it back |
113 | 112 |
|
114 | | - // file is fine, let's pass it back |
115 | 113 | resolve(file); |
116 | 114 | }); |
117 | 115 | }); |
118 | | - |
119 | 116 | return { |
120 | 117 | options: { |
121 | 118 | // Enable or disable file type validation |
122 | 119 | allowFileSizeValidation: [true, Type.BOOLEAN], |
123 | | - |
124 | 120 | // Max individual file size in bytes |
125 | 121 | maxFileSize: [null, Type.INT], |
126 | | - |
127 | 122 | // Min individual file size in bytes |
128 | 123 | minFileSize: [null, Type.INT], |
129 | | - |
130 | 124 | // Max total file size in bytes |
131 | 125 | maxTotalFileSize: [null, Type.INT], |
132 | | - |
133 | 126 | // error labels |
134 | 127 | labelMinFileSizeExceeded: ['File is too small', Type.STRING], |
135 | 128 | labelMinFileSize: ['Minimum file size is {filesize}', Type.STRING], |
136 | | - |
137 | 129 | labelMaxFileSizeExceeded: ['File is too large', Type.STRING], |
138 | 130 | labelMaxFileSize: ['Maximum file size is {filesize}', Type.STRING], |
139 | | - |
140 | 131 | labelMaxTotalFileSizeExceeded: [ |
141 | 132 | 'Maximum total size exceeded', |
142 | 133 | Type.STRING |
|
147 | 138 | ] |
148 | 139 | } |
149 | 140 | }; |
150 | | - }; |
| 141 | + }; // fire pluginloaded event if running in browser, this allows registering the plugin when using async script tags |
151 | 142 |
|
152 | 143 | var isBrowser = |
153 | 144 | typeof window !== 'undefined' && typeof window.document !== 'undefined'; |
154 | 145 |
|
155 | 146 | if (isBrowser) { |
156 | 147 | document.dispatchEvent( |
157 | | - new CustomEvent('FilePond:pluginloaded', { detail: plugin$1 }) |
| 148 | + new CustomEvent('FilePond:pluginloaded', { |
| 149 | + detail: plugin |
| 150 | + }) |
158 | 151 | ); |
159 | 152 | } |
160 | 153 |
|
161 | | - return plugin$1; |
| 154 | + return plugin; |
162 | 155 | }); |
0 commit comments