| 172 | |
| 173 | |
| 174 | function _convertFile(file, fn, useBinaryString){ |
| 175 | var blob = file.blob, filename = file.file; |
| 176 | |
| 177 | if( filename ){ |
| 178 | if( !blob.toDataURL ){ |
| 179 | // The Blob is not an image. |
| 180 | api.readAsBinaryString(blob, function (evt){ |
| 181 | if( evt.type == 'load' ){ |
| 182 | fn(file, evt.result); |
| 183 | } |
| 184 | }); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | var |
| 189 | mime = { 'image/jpeg': '.jpe?g', 'image/png': '.png' } |
| 190 | , type = mime[file.type] ? file.type : 'image/png' |
| 191 | , ext = mime[type] || '.png' |
| 192 | , quality = blob.quality || 1 |
| 193 | ; |
| 194 | |
| 195 | if( !filename.match(new RegExp(ext+'$', 'i')) ){ |
| 196 | // Does not change the current extension, but add a new one. |
| 197 | filename += ext.replace('?', ''); |
| 198 | } |
| 199 | |
| 200 | file.file = filename; |
| 201 | file.type = type; |
| 202 | |
| 203 | if( !useBinaryString && blob.toBlob ){ |
| 204 | blob.toBlob(function (blob){ |
| 205 | fn(file, blob); |
| 206 | }, type, quality); |
| 207 | } |
| 208 | else { |
| 209 | fn(file, api.toBinaryString(blob.toDataURL(type, quality))); |
| 210 | } |
| 211 | } |
| 212 | else { |
| 213 | fn(file, blob); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | |
| 218 | // @export |