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