| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Object representing an image item (a photo or a video). | 8 * Object representing an image item (a photo or a video). |
| 9 * | 9 * |
| 10 * @param {string} url Image url. | 10 * @param {FileEntry} entry Image entry. |
| 11 * @constructor | 11 * @constructor |
| 12 */ | 12 */ |
| 13 Gallery.Item = function(url) { | 13 Gallery.Item = function(entry) { |
| 14 this.url_ = url; | 14 this.entry_ = entry; |
| 15 this.original_ = true; | 15 this.original_ = true; |
| 16 }; | 16 }; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * @return {string} Image url. | 19 * @return {FileEntry} Image entry. |
| 20 */ | 20 */ |
| 21 Gallery.Item.prototype.getUrl = function() { return this.url_ }; | 21 Gallery.Item.prototype.getEntry = function() { return this.entry_ }; |
| 22 | |
| 23 /** | |
| 24 * @param {string} url New url. | |
| 25 */ | |
| 26 Gallery.Item.prototype.setUrl = function(url) { this.url_ = url }; | |
| 27 | 22 |
| 28 /** | 23 /** |
| 29 * @return {string} File name. | 24 * @return {string} File name. |
| 30 */ | 25 */ |
| 31 Gallery.Item.prototype.getFileName = function() { | 26 Gallery.Item.prototype.getFileName = function() { |
| 32 return ImageUtil.getFullNameFromUrl(this.url_); | 27 return this.entry_.name; |
| 33 }; | 28 }; |
| 34 | 29 |
| 35 /** | 30 /** |
| 36 * @return {boolean} True if this image has not been created in this session. | 31 * @return {boolean} True if this image has not been created in this session. |
| 37 */ | 32 */ |
| 38 Gallery.Item.prototype.isOriginal = function() { return this.original_ }; | 33 Gallery.Item.prototype.isOriginal = function() { return this.original_ }; |
| 39 | 34 |
| 40 // TODO: Localize? | 35 // TODO: Localize? |
| 41 /** | 36 /** |
| 42 * @type {string} Suffix for a edited copy file name. | 37 * @type {string} Suffix for a edited copy file name. |
| 43 */ | 38 */ |
| 44 Gallery.Item.COPY_SIGNATURE = ' - Edited'; | 39 Gallery.Item.COPY_SIGNATURE = ' - Edited'; |
| 45 | 40 |
| 46 /** | 41 /** |
| 47 * Regular expression to match '... - Edited'. | 42 * Regular expression to match '... - Edited'. |
| 48 * @type {RegExp} | 43 * @type {RegExp} |
| 49 */ | 44 */ |
| 50 Gallery.Item.REGEXP_COPY_0 = | 45 Gallery.Item.REGEXP_COPY_0 = |
| 51 new RegExp('^(.+)' + Gallery.Item.COPY_SIGNATURE + '$'); | 46 new RegExp('^(.+)' + Gallery.Item.COPY_SIGNATURE + '$'); |
| 52 | 47 |
| 53 /** | 48 /** |
| 54 * Regular expression to match '... - Edited (N)'. | 49 * Regular expression to match '... - Edited (N)'. |
| 55 * @type {RegExp} | 50 * @type {RegExp} |
| 56 */ | 51 */ |
| 57 Gallery.Item.REGEXP_COPY_N = | 52 Gallery.Item.REGEXP_COPY_N = |
| 58 new RegExp('^(.+)' + Gallery.Item.COPY_SIGNATURE + ' \\((\\d+)\\)$'); | 53 new RegExp('^(.+)' + Gallery.Item.COPY_SIGNATURE + ' \\((\\d+)\\)$'); |
| 59 | 54 |
| 60 /** | 55 /** |
| 61 * Create a name for an edited copy of the file. | 56 * Creates a name for an edited copy of the file. |
| 62 * | 57 * |
| 63 * @param {Entry} dirEntry Entry. | 58 * @param {Entry} dirEntry Entry. |
| 64 * @param {function} callback Callback. | 59 * @param {function} callback Callback. |
| 65 * @private | 60 * @private |
| 66 */ | 61 */ |
| 67 Gallery.Item.prototype.createCopyName_ = function(dirEntry, callback) { | 62 Gallery.Item.prototype.createCopyName_ = function(dirEntry, callback) { |
| 68 var name = this.getFileName(); | 63 var name = this.getFileName(); |
| 69 | 64 |
| 70 // If the item represents a file created during the current Gallery session | 65 // If the item represents a file created during the current Gallery session |
| 71 // we reuse it for subsequent saves instead of creating multiple copies. | 66 // we reuse it for subsequent saves instead of creating multiple copies. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 104 |
| 110 dirEntry.getFile(name + ext, {create: false, exclusive: false}, | 105 dirEntry.getFile(name + ext, {create: false, exclusive: false}, |
| 111 tryNext.bind(null, tries - 1), | 106 tryNext.bind(null, tries - 1), |
| 112 callback.bind(null, name + ext)); | 107 callback.bind(null, name + ext)); |
| 113 } | 108 } |
| 114 | 109 |
| 115 tryNext(10); | 110 tryNext(10); |
| 116 }; | 111 }; |
| 117 | 112 |
| 118 /** | 113 /** |
| 119 * Write the new item content to the file. | 114 * Writes the new item content to the file. |
| 120 * | 115 * |
| 121 * @param {Entry} overrideDir Directory to save to. If null, save to the same | 116 * @param {Entry} overrideDir Directory to save to. If null, save to the same |
| 122 * directory as the original. | 117 * directory as the original. |
| 123 * @param {boolean} overwrite True if overwrite, false if copy. | 118 * @param {boolean} overwrite True if overwrite, false if copy. |
| 124 * @param {HTMLCanvasElement} canvas Source canvas. | 119 * @param {HTMLCanvasElement} canvas Source canvas. |
| 125 * @param {ImageEncoder.MetadataEncoder} metadataEncoder MetadataEncoder. | 120 * @param {ImageEncoder.MetadataEncoder} metadataEncoder MetadataEncoder. |
| 126 * @param {function(boolean)=} opt_callback Callback accepting true for success. | 121 * @param {function(boolean)=} opt_callback Callback accepting true for success. |
| 127 */ | 122 */ |
| 128 Gallery.Item.prototype.saveToFile = function( | 123 Gallery.Item.prototype.saveToFile = function( |
| 129 overrideDir, overwrite, canvas, metadataEncoder, opt_callback) { | 124 overrideDir, overwrite, canvas, metadataEncoder, opt_callback) { |
| 130 ImageUtil.metrics.startInterval(ImageUtil.getMetricName('SaveTime')); | 125 ImageUtil.metrics.startInterval(ImageUtil.getMetricName('SaveTime')); |
| 131 | 126 |
| 132 var name = this.getFileName(); | 127 var name = this.getFileName(); |
| 133 | 128 |
| 134 var onSuccess = function(url) { | 129 var onSuccess = function(entry) { |
| 135 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 1, 2); | 130 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 1, 2); |
| 136 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('SaveTime')); | 131 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('SaveTime')); |
| 137 this.setUrl(url); | 132 this.entry_ = entry; |
| 138 if (opt_callback) opt_callback(true); | 133 if (opt_callback) opt_callback(true); |
| 139 }.bind(this); | 134 }.bind(this); |
| 140 | 135 |
| 141 function onError(error) { | 136 function onError(error) { |
| 142 console.error('Error saving from gallery', name, error); | 137 console.error('Error saving from gallery', name, error); |
| 143 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 0, 2); | 138 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 0, 2); |
| 144 if (opt_callback) opt_callback(false); | 139 if (opt_callback) opt_callback(false); |
| 145 } | 140 } |
| 146 | 141 |
| 147 function doSave(newFile, fileEntry) { | 142 function doSave(newFile, fileEntry) { |
| 148 fileEntry.createWriter(function(fileWriter) { | 143 fileEntry.createWriter(function(fileWriter) { |
| 149 function writeContent() { | 144 function writeContent() { |
| 150 fileWriter.onwriteend = onSuccess.bind(null, fileEntry.toURL()); | 145 fileWriter.onwriteend = onSuccess.bind(null, fileEntry); |
| 151 fileWriter.write(ImageEncoder.getBlob(canvas, metadataEncoder)); | 146 fileWriter.write(ImageEncoder.getBlob(canvas, metadataEncoder)); |
| 152 } | 147 } |
| 153 fileWriter.onerror = function(error) { | 148 fileWriter.onerror = function(error) { |
| 154 onError(error); | 149 onError(error); |
| 155 // Disable all callbacks on the first error. | 150 // Disable all callbacks on the first error. |
| 156 fileWriter.onerror = null; | 151 fileWriter.onerror = null; |
| 157 fileWriter.onwriteend = null; | 152 fileWriter.onwriteend = null; |
| 158 }; | 153 }; |
| 159 if (newFile) { | 154 if (newFile) { |
| 160 writeContent(); | 155 writeContent(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 184 this.original_ = false; | 179 this.original_ = false; |
| 185 name = copyName; | 180 name = copyName; |
| 186 checkExistence(dir); | 181 checkExistence(dir); |
| 187 }.bind(this)); | 182 }.bind(this)); |
| 188 } | 183 } |
| 189 }.bind(this); | 184 }.bind(this); |
| 190 | 185 |
| 191 if (overrideDir) { | 186 if (overrideDir) { |
| 192 saveToDir(overrideDir); | 187 saveToDir(overrideDir); |
| 193 } else { | 188 } else { |
| 194 webkitResolveLocalFileSystemURL(this.getUrl(), | 189 this.entry_.getParent(saveToDir, onError); |
| 195 function(entry) { entry.getParent(saveToDir, onError)}, | |
| 196 onError); | |
| 197 } | 190 } |
| 198 }; | 191 }; |
| 199 | 192 |
| 200 /** | 193 /** |
| 201 * Rename the file. | 194 * Renames the file. |
| 202 * | 195 * |
| 203 * @param {string} name New file name. | 196 * @param {string} displayName New display name (without the extension). |
| 204 * @param {function} onSuccess Success callback. | 197 * @param {function()} onSuccess Success callback. |
| 205 * @param {function} onExists Called if the file with the new name exists. | 198 * @param {function()} onExists Called if the file with the new name exists. |
| 206 */ | 199 */ |
| 207 Gallery.Item.prototype.rename = function(name, onSuccess, onExists) { | 200 Gallery.Item.prototype.rename = function(displayName, onSuccess, onExists) { |
| 208 var oldName = this.getFileName(); | 201 var fileName = this.entry_.name.replace( |
| 209 if (ImageUtil.getExtensionFromFullName(name) == | 202 ImageUtil.getDisplayNameFromName(this.entry_.name), displayName); |
| 210 ImageUtil.getExtensionFromFullName(oldName)) { | |
| 211 name = ImageUtil.getFileNameFromFullName(name); | |
| 212 } | |
| 213 var newName = ImageUtil.replaceFileNameInFullName(oldName, name); | |
| 214 if (oldName == newName) return; | |
| 215 | 203 |
| 216 function onError() { | 204 if (name === this.entry_.name) |
| 217 console.error('Rename error: "' + oldName + '" to "' + newName + '"'); | 205 return; |
| 218 } | |
| 219 | 206 |
| 220 var onRenamed = function(entry) { | 207 var onRenamed = function(entry) { |
| 221 this.setUrl(entry.toURL()); | 208 this.entry_ = entry; |
| 222 onSuccess(); | 209 onSuccess(); |
| 223 }.bind(this); | 210 }.bind(this); |
| 224 | 211 |
| 225 function moveIfDoesNotExist(entry, parentDir) { | 212 var onError = function() { |
| 226 parentDir.getFile(newName, {create: false, exclusive: false}, onExists, | 213 console.error('Rename error: "' + oldName + '" to "' + newName + '"'); |
| 227 function() { entry.moveTo(parentDir, newName, onRenamed, onError) }); | 214 }; |
| 228 } | |
| 229 | 215 |
| 230 webkitResolveLocalFileSystemURL(this.getUrl(), | 216 var moveIfDoesNotExist = function(parentDir) { |
| 231 function(entry) { | 217 parentDir.getFile( |
| 232 entry.getParent(moveIfDoesNotExist.bind(null, entry), onError); | 218 fileName, |
| 233 }, | 219 {create: false, exclusive: false}, |
| 234 onError); | 220 onExists, |
| 221 function() { |
| 222 this.entry_.moveTo(parentDir, fileName, onRenamed, onError); |
| 223 }.bind(this)); |
| 224 }.bind(this); |
| 225 |
| 226 this.entry_.getParent(moveIfDoesNotExist, onError); |
| 235 }; | 227 }; |
| OLD | NEW |