Chromium Code Reviews| Index: ui/file_manager/gallery/js/gallery_item.js |
| diff --git a/ui/file_manager/gallery/js/gallery_item.js b/ui/file_manager/gallery/js/gallery_item.js |
| index 54c6ef1515e5b87b4a7ed340f5f84076dc17c1c4..16b64516e534d702217cd7826265511928de7dd6 100644 |
| --- a/ui/file_manager/gallery/js/gallery_item.js |
| +++ b/ui/file_manager/gallery/js/gallery_item.js |
| @@ -264,32 +264,43 @@ Gallery.Item.prototype.saveToFile = function( |
| [fileEntry], |
| ['mediaMimeType', 'contentMimeType', 'ifd', 'exifLittleEndian']); |
| metadataPromise.then(function(metadataItems) { |
| + var metadataItem = metadataItems[0]; |
|
fukino
2015/03/09 07:12:35
It's preferred to defer declaring variables until
yawano
2015/03/09 08:40:32
Changed to put this block in promise chain. In the
|
| + metadataItem.modificationTime = new Date(); |
| + var metadataEncoder = ImageEncoder.encodeMetadata( |
| + metadataItem, canvas, /* quality for thumbnail*/ 0.8); |
| + // Contrary to what one might think 1.0 is not a good default. Opening |
| + // and saving an typical photo taken with consumer camera increases |
| + // its file size by 50-100%. Experiments show that 0.9 is much better. |
| + // It shrinks some photos a bit, keeps others about the same size, but |
| + // does not visibly lower the quality. |
| + var blob = ImageEncoder.getBlob(canvas, metadataEncoder, 0.9); |
| + |
| fileEntry.createWriter(function(fileWriter) { |
| - var writeContent = function() { |
| - fileWriter.onwriteend = onSuccess.bind(null, fileEntry); |
| - var metadataItem = metadataItems[0]; |
| - metadataItem.modificationTime = new Date(); |
| - var metadataEncoder = ImageEncoder.encodeMetadata( |
| - metadataItem, canvas, /* quality for thumbnail*/ 0.8); |
| - // Contrary to what one might think 1.0 is not a good default. Opening |
| - // and saving an typical photo taken with consumer camera increases |
| - // its file size by 50-100%. Experiments show that 0.9 is much better. |
| - // It shrinks some photos a bit, keeps others about the same size, but |
| - // does not visibly lower the quality. |
| - fileWriter.write(ImageEncoder.getBlob(canvas, metadataEncoder, 0.9)); |
| - }.bind(this); |
| fileWriter.onerror = function(error) { |
| + throw error; |
|
fukino
2015/03/09 07:12:35
Who handles this exception?
yawano
2015/03/09 08:40:32
Done. After checking behavior, this exception is n
|
| + }; |
| + |
| + new Promise(function(fulfill) { |
| + // Truncates the file if it overwrites. |
| + if (!newFile) { |
|
fukino
2015/03/09 07:12:34
optional nit: In if-else statement, inverted condi
yawano
2015/03/09 08:40:32
This promise is a promise to truncate the file to
|
| + fileWriter.onwriteend = fulfill; |
| + fileWriter.truncate(0); |
| + } else { |
| + fulfill(); |
| + } |
| + }).then(function() { |
| + return new Promise(function(fulfill) { |
| + // Writes the data. |
| + fileWriter.onwriteend = fulfill; |
| + fileWriter.write(blob); |
| + }); |
| + }).then(onSuccess.bind(null, fileEntry)) |
| + .catch(function(error) { |
| onError(error); |
| // Disable all callbacks on the first error. |
| fileWriter.onerror = null; |
| fileWriter.onwriteend = null; |
| - }; |
| - if (newFile) { |
| - writeContent(); |
| - } else { |
| - fileWriter.onwriteend = writeContent; |
| - fileWriter.truncate(0); |
| - } |
| + }); |
| }.bind(this), onError); |
| }.bind(this)); |
| }.bind(this); |