| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Object representing an image item (a photo). | 6 * Object representing an image item (a photo). |
| 7 * | 7 * |
| 8 * @param {!FileEntry} entry Image entry. | 8 * @param {!FileEntry} entry Image entry. |
| 9 * @param {!EntryLocation} locationInfo Entry location information. | 9 * @param {!EntryLocation} locationInfo Entry location information. |
| 10 * @param {!MetadataItem} metadataItem | 10 * @param {!MetadataItem} metadataItem |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 }.bind(this); | 253 }.bind(this); |
| 254 | 254 |
| 255 var onError = function(error) { | 255 var onError = function(error) { |
| 256 console.error('Error saving from gallery', name, error); | 256 console.error('Error saving from gallery', name, error); |
| 257 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 0, 2); | 257 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 0, 2); |
| 258 if (callback) | 258 if (callback) |
| 259 callback(false); | 259 callback(false); |
| 260 }; | 260 }; |
| 261 | 261 |
| 262 var doSave = function(newFile, fileEntry) { | 262 var doSave = function(newFile, fileEntry) { |
| 263 var metadataPromise = metadataModel.get( | 263 var blob; |
| 264 var fileWriter; |
| 265 |
| 266 metadataModel.get( |
| 264 [fileEntry], | 267 [fileEntry], |
| 265 ['mediaMimeType', 'contentMimeType', 'ifd', 'exifLittleEndian']); | 268 ['mediaMimeType', 'contentMimeType', 'ifd', 'exifLittleEndian'] |
| 266 metadataPromise.then(function(metadataItems) { | 269 ).then(function(metadataItems) { |
| 267 fileEntry.createWriter(function(fileWriter) { | 270 // Create the blob of new image. |
| 268 var writeContent = function() { | 271 var metadataItem = metadataItems[0]; |
| 269 fileWriter.onwriteend = onSuccess.bind(null, fileEntry); | 272 metadataItem.modificationTime = new Date(); |
| 270 var metadataItem = metadataItems[0]; | 273 var metadataEncoder = ImageEncoder.encodeMetadata( |
| 271 metadataItem.modificationTime = new Date(); | 274 metadataItem, canvas, /* quality for thumbnail*/ 0.8); |
| 272 var metadataEncoder = ImageEncoder.encodeMetadata( | 275 // Contrary to what one might think 1.0 is not a good default. Opening |
| 273 metadataItem, canvas, /* quality for thumbnail*/ 0.8); | 276 // and saving an typical photo taken with consumer camera increases |
| 274 // Contrary to what one might think 1.0 is not a good default. Opening | 277 // its file size by 50-100%. Experiments show that 0.9 is much better. |
| 275 // and saving an typical photo taken with consumer camera increases | 278 // It shrinks some photos a bit, keeps others about the same size, but |
| 276 // its file size by 50-100%. Experiments show that 0.9 is much better. | 279 // does not visibly lower the quality. |
| 277 // It shrinks some photos a bit, keeps others about the same size, but | 280 blob = ImageEncoder.getBlob(canvas, metadataEncoder, 0.9); |
| 278 // does not visibly lower the quality. | 281 }).then(function() { |
| 279 fileWriter.write(ImageEncoder.getBlob(canvas, metadataEncoder, 0.9)); | 282 // Create writer. |
| 280 }.bind(this); | 283 return new Promise(function(fullfill, reject) { |
| 281 fileWriter.onerror = function(error) { | 284 fileEntry.createWriter(fullfill, reject); |
| 282 onError(error); | 285 }); |
| 283 // Disable all callbacks on the first error. | 286 }).then(function(writer) { |
| 284 fileWriter.onerror = null; | 287 fileWriter = writer; |
| 285 fileWriter.onwriteend = null; | 288 |
| 286 }; | 289 // Truncates the file to 0 byte if it overwrites. |
| 287 if (newFile) { | 290 return new Promise(function(fulfill, reject) { |
| 288 writeContent(); | 291 if (!newFile) { |
| 292 fileWriter.onerror = reject; |
| 293 fileWriter.onwriteend = fulfill; |
| 294 fileWriter.truncate(0); |
| 289 } else { | 295 } else { |
| 290 fileWriter.onwriteend = writeContent; | 296 fulfill(null); |
| 291 fileWriter.truncate(0); | |
| 292 } | 297 } |
| 293 }.bind(this), onError); | 298 }); |
| 294 }.bind(this)); | 299 }).then(function() { |
| 300 // Writes the blob of new image. |
| 301 return new Promise(function(fulfill, reject) { |
| 302 fileWriter.onerror = reject; |
| 303 fileWriter.onwriteend = fulfill; |
| 304 fileWriter.write(blob); |
| 305 }); |
| 306 }).then(onSuccess.bind(null, fileEntry)) |
| 307 .catch(function(error) { |
| 308 onError(error); |
| 309 // Disable all callbacks on the first error. |
| 310 fileWriter.onerror = null; |
| 311 fileWriter.onwriteend = null; |
| 312 }); |
| 295 }.bind(this); | 313 }.bind(this); |
| 296 | 314 |
| 297 var getFile = function(dir, newFile) { | 315 var getFile = function(dir, newFile) { |
| 298 dir.getFile(name, {create: newFile, exclusive: newFile}, | 316 dir.getFile(name, {create: newFile, exclusive: newFile}, |
| 299 function(fileEntry) { | 317 function(fileEntry) { |
| 300 doSave(newFile, fileEntry); | 318 doSave(newFile, fileEntry); |
| 301 }.bind(this), onError); | 319 }.bind(this), onError); |
| 302 }.bind(this); | 320 }.bind(this); |
| 303 | 321 |
| 304 var checkExistence = function(dir) { | 322 var checkExistence = function(dir) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 return Promise.reject(str('GALLERY_FILE_EXISTS')); | 373 return Promise.reject(str('GALLERY_FILE_EXISTS')); |
| 356 }, function() { | 374 }, function() { |
| 357 return new Promise( | 375 return new Promise( |
| 358 this.entry_.moveTo.bind(this.entry_, parentDirectory, newFileName)); | 376 this.entry_.moveTo.bind(this.entry_, parentDirectory, newFileName)); |
| 359 }.bind(this)); | 377 }.bind(this)); |
| 360 }.bind(this)); | 378 }.bind(this)); |
| 361 }.bind(this)).then(function(entry) { | 379 }.bind(this)).then(function(entry) { |
| 362 this.entry_ = entry; | 380 this.entry_ = entry; |
| 363 }.bind(this)); | 381 }.bind(this)); |
| 364 }; | 382 }; |
| OLD | NEW |