|
93 | 93 | // widget (via file input selection, drag & drop or add API call).
|
94 | 94 | // See the basic file upload widget for more information:
|
95 | 95 | add: function (e, data) {
|
96 |
| - if (e.isDefaultPrevented()) { |
97 |
| - return false; |
98 |
| - } |
| 96 | + // console.log('add: XXXXXXX', e, data); |
99 | 97 | var $this = $(this),
|
100 | 98 | that = $this.data('blueimp-fileupload') || $this.data('fileupload'),
|
101 | 99 | options = that.options;
|
102 |
| - data.context = that |
103 |
| - ._renderUpload(data.files) |
104 |
| - .data('data', data) |
105 |
| - .addClass('processing'); |
106 |
| - options.filesContainer[options.prependFiles ? 'prepend' : 'append']( |
107 |
| - data.context |
108 |
| - ); |
109 |
| - that._forceReflow(data.context); |
110 |
| - that._transition(data.context); |
111 |
| - data |
112 |
| - .process(function () { |
113 |
| - return $this.fileupload('process', data); |
114 |
| - }) |
115 |
| - .always(function () { |
116 |
| - data.context |
117 |
| - .each(function (index) { |
118 |
| - $(this) |
119 |
| - .find('.size') |
120 |
| - .text(that._formatFileSize(data.files[index].size)); |
121 |
| - }) |
122 |
| - .removeClass('processing'); |
123 |
| - that._renderPreviews(data); |
124 |
| - }) |
125 |
| - .done(function () { |
126 |
| - data.context.find('.edit,.start').prop('disabled', false); |
127 |
| - if ( |
128 |
| - that._trigger('added', e, data) !== false && |
129 |
| - (options.autoUpload || data.autoUpload) && |
130 |
| - data.autoUpload !== false |
131 |
| - ) { |
132 |
| - data.submit(); |
133 |
| - } |
134 |
| - }) |
135 |
| - .fail(function () { |
136 |
| - if (data.files.error) { |
137 |
| - data.context.each(function (index) { |
138 |
| - var error = data.files[index].error; |
139 |
| - if (error) { |
140 |
| - $(this).find('.error').text(error); |
141 |
| - } |
| 100 | + |
| 101 | + const blobx = data.files[0]; |
| 102 | + const fileReader = new FileReader(); |
| 103 | + fileReader.onloadend = function (e) { |
| 104 | + const arr = new Uint8Array(e.target.result).subarray(0, 4); |
| 105 | + let header = ''; |
| 106 | + let type = ''; |
| 107 | + |
| 108 | + for (var i = 0; i < arr.length; i++) { |
| 109 | + header += arr[i].toString(16); |
| 110 | + } |
| 111 | + |
| 112 | + switch (header) { |
| 113 | + case '89504e47': |
| 114 | + type = 'image/png'; |
| 115 | + break; |
| 116 | + case '47494638': |
| 117 | + type = 'image/gif'; |
| 118 | + break; |
| 119 | + case 'ffd8ffe0': |
| 120 | + case 'ffd8ffe1': |
| 121 | + case 'ffd8ffe2': |
| 122 | + case 'ffd8ffe3': |
| 123 | + case 'ffd8ffe8': |
| 124 | + type = 'image/jpeg'; |
| 125 | + break; |
| 126 | + case '00018': |
| 127 | + type = 'image/heif'; |
| 128 | + break; |
| 129 | + default: |
| 130 | + type = 'unknown'; // Or you can use the blob.type as fallback |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + // console.log(header, type); |
| 135 | + |
| 136 | + if (type !== 'unknown') { |
| 137 | + if (type === 'image/heic' || type === 'image/heif') { |
| 138 | + const originalName = data.files[0].name; |
| 139 | + |
| 140 | + heic2any({ |
| 141 | + blob: data.files[0], |
| 142 | + toType: 'image/jpeg', |
| 143 | + quality: 0.5, // cuts the quality and size by half |
| 144 | + }).then((conversionResultBlob) => { |
| 145 | + // conversionResult is a BLOB |
| 146 | + // of the JPEG formatted image |
| 147 | + // with low quality |
| 148 | + data.files[0] = new File( |
| 149 | + [conversionResultBlob], |
| 150 | + originalName |
| 151 | + .replace('.heic', '.jpg') |
| 152 | + .replace('.heif', '.jpg'), |
| 153 | + { |
| 154 | + type: conversionResultBlob.type, |
| 155 | + } |
| 156 | + ); |
| 157 | + // console.log('Converted to jpeg:', data.files[0]); |
| 158 | + // data.submit(); |
| 159 | + |
| 160 | + addContinues(data); |
142 | 161 | });
|
| 162 | + } else if ( |
| 163 | + type === 'image/jpeg' || |
| 164 | + type === 'image/gif' || |
| 165 | + type === 'image/png' |
| 166 | + ) { |
| 167 | + addContinues(data); |
| 168 | + } else { |
| 169 | + console.log('Aborted'); |
| 170 | + data.abort(); |
143 | 171 | }
|
144 |
| - }); |
| 172 | + } |
| 173 | + }; |
| 174 | + |
| 175 | + fileReader.readAsArrayBuffer(blobx); |
| 176 | + |
| 177 | + function addContinues() { |
| 178 | + if (e.isDefaultPrevented()) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + data.context = that |
| 183 | + ._renderUpload(data.files) |
| 184 | + .data('data', data) |
| 185 | + .addClass('processing'); |
| 186 | + options.filesContainer[options.prependFiles ? 'prepend' : 'append']( |
| 187 | + data.context |
| 188 | + ); |
| 189 | + that._forceReflow(data.context); |
| 190 | + that._transition(data.context); |
| 191 | + data |
| 192 | + .process(function () { |
| 193 | + return $this.fileupload('process', data); |
| 194 | + }) |
| 195 | + .always(function () { |
| 196 | + data.context |
| 197 | + .each(function (index) { |
| 198 | + $(this) |
| 199 | + .find('.size') |
| 200 | + .text(that._formatFileSize(data.files[index].size)); |
| 201 | + }) |
| 202 | + .removeClass('processing'); |
| 203 | + that._renderPreviews(data); |
| 204 | + }) |
| 205 | + .done(function () { |
| 206 | + data.context.find('.edit,.start').prop('disabled', false); |
| 207 | + if ( |
| 208 | + that._trigger('added', e, data) !== false && |
| 209 | + (options.autoUpload || data.autoUpload) && |
| 210 | + data.autoUpload !== false |
| 211 | + ) { |
| 212 | + data.submit(); |
| 213 | + } |
| 214 | + }) |
| 215 | + .fail(function () { |
| 216 | + if (data.files.error) { |
| 217 | + data.context.each(function (index) { |
| 218 | + var error = data.files[index].error; |
| 219 | + if (error) { |
| 220 | + $(this).find('.error').text(error); |
| 221 | + } |
| 222 | + }); |
| 223 | + } |
| 224 | + }); |
| 225 | + } |
145 | 226 | },
|
146 | 227 | // Callback for the start of each file upload request:
|
147 | 228 | send: function (e, data) {
|
|
338 | 419 | that
|
339 | 420 | ._transition($(this).find('.fileupload-progress'))
|
340 | 421 | .done(function () {
|
341 |
| - $(this) |
342 |
| - .find('.progress') |
343 |
| - .attr('aria-valuenow', '0') |
344 |
| - .children() |
345 |
| - .first() |
346 |
| - .css('width', '0%'); |
347 |
| - $(this).find('.progress-extended').html(' '); |
| 422 | + // $(this) |
| 423 | + // .find('.progress') |
| 424 | + // .attr('aria-valuenow', '0') |
| 425 | + // .children() |
| 426 | + // .first() |
| 427 | + // .css('width', '0%'); |
| 428 | + // $(this).find('.progress-extended').html(' '); |
348 | 429 | deferred.resolve();
|
349 | 430 | });
|
350 | 431 | },
|
|
0 commit comments