Chromium Code Reviews| 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 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 metadataPromise = metadataModel.get( | 
| 264 [fileEntry], | 264 [fileEntry], | 
| 265 ['mediaMimeType', 'contentMimeType', 'ifd', 'exifLittleEndian']); | 265 ['mediaMimeType', 'contentMimeType', 'ifd', 'exifLittleEndian']); | 
| 266 metadataPromise.then(function(metadataItems) { | 266 metadataPromise.then(function(metadataItems) { | 
| 267 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
 
 | |
| 268 metadataItem.modificationTime = new Date(); | |
| 269 var metadataEncoder = ImageEncoder.encodeMetadata( | |
| 270 metadataItem, canvas, /* quality for thumbnail*/ 0.8); | |
| 271 // Contrary to what one might think 1.0 is not a good default. Opening | |
| 272 // and saving an typical photo taken with consumer camera increases | |
| 273 // its file size by 50-100%. Experiments show that 0.9 is much better. | |
| 274 // It shrinks some photos a bit, keeps others about the same size, but | |
| 275 // does not visibly lower the quality. | |
| 276 var blob = ImageEncoder.getBlob(canvas, metadataEncoder, 0.9); | |
| 277 | |
| 267 fileEntry.createWriter(function(fileWriter) { | 278 fileEntry.createWriter(function(fileWriter) { | 
| 268 var writeContent = function() { | |
| 269 fileWriter.onwriteend = onSuccess.bind(null, fileEntry); | |
| 270 var metadataItem = metadataItems[0]; | |
| 271 metadataItem.modificationTime = new Date(); | |
| 272 var metadataEncoder = ImageEncoder.encodeMetadata( | |
| 273 metadataItem, canvas, /* quality for thumbnail*/ 0.8); | |
| 274 // Contrary to what one might think 1.0 is not a good default. Opening | |
| 275 // and saving an typical photo taken with consumer camera increases | |
| 276 // its file size by 50-100%. Experiments show that 0.9 is much better. | |
| 277 // It shrinks some photos a bit, keeps others about the same size, but | |
| 278 // does not visibly lower the quality. | |
| 279 fileWriter.write(ImageEncoder.getBlob(canvas, metadataEncoder, 0.9)); | |
| 280 }.bind(this); | |
| 281 fileWriter.onerror = function(error) { | 279 fileWriter.onerror = function(error) { | 
| 280 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
 
 | |
| 281 }; | |
| 282 | |
| 283 new Promise(function(fulfill) { | |
| 284 // Truncates the file if it overwrites. | |
| 285 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
 
 | |
| 286 fileWriter.onwriteend = fulfill; | |
| 287 fileWriter.truncate(0); | |
| 288 } else { | |
| 289 fulfill(); | |
| 290 } | |
| 291 }).then(function() { | |
| 292 return new Promise(function(fulfill) { | |
| 293 // Writes the data. | |
| 294 fileWriter.onwriteend = fulfill; | |
| 295 fileWriter.write(blob); | |
| 296 }); | |
| 297 }).then(onSuccess.bind(null, fileEntry)) | |
| 298 .catch(function(error) { | |
| 282 onError(error); | 299 onError(error); | 
| 283 // Disable all callbacks on the first error. | 300 // Disable all callbacks on the first error. | 
| 284 fileWriter.onerror = null; | 301 fileWriter.onerror = null; | 
| 285 fileWriter.onwriteend = null; | 302 fileWriter.onwriteend = null; | 
| 286 }; | 303 }); | 
| 287 if (newFile) { | |
| 288 writeContent(); | |
| 289 } else { | |
| 290 fileWriter.onwriteend = writeContent; | |
| 291 fileWriter.truncate(0); | |
| 292 } | |
| 293 }.bind(this), onError); | 304 }.bind(this), onError); | 
| 294 }.bind(this)); | 305 }.bind(this)); | 
| 295 }.bind(this); | 306 }.bind(this); | 
| 296 | 307 | 
| 297 var getFile = function(dir, newFile) { | 308 var getFile = function(dir, newFile) { | 
| 298 dir.getFile(name, {create: newFile, exclusive: newFile}, | 309 dir.getFile(name, {create: newFile, exclusive: newFile}, | 
| 299 function(fileEntry) { | 310 function(fileEntry) { | 
| 300 doSave(newFile, fileEntry); | 311 doSave(newFile, fileEntry); | 
| 301 }.bind(this), onError); | 312 }.bind(this), onError); | 
| 302 }.bind(this); | 313 }.bind(this); | 
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 return Promise.reject(str('GALLERY_FILE_EXISTS')); | 366 return Promise.reject(str('GALLERY_FILE_EXISTS')); | 
| 356 }, function() { | 367 }, function() { | 
| 357 return new Promise( | 368 return new Promise( | 
| 358 this.entry_.moveTo.bind(this.entry_, parentDirectory, newFileName)); | 369 this.entry_.moveTo.bind(this.entry_, parentDirectory, newFileName)); | 
| 359 }.bind(this)); | 370 }.bind(this)); | 
| 360 }.bind(this)); | 371 }.bind(this)); | 
| 361 }.bind(this)).then(function(entry) { | 372 }.bind(this)).then(function(entry) { | 
| 362 this.entry_ = entry; | 373 this.entry_ = entry; | 
| 363 }.bind(this)); | 374 }.bind(this)); | 
| 364 }; | 375 }; | 
| OLD | NEW |