Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: chrome/browser/resources/file_manager/foreground/js/photo/gallery_item.js

Issue 109973002: Migrate from URLs to Entries in the Files App's gallery. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments + rebased. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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 * Rename 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)
hirono 2013/12/10 03:36:53 === is preferred.
mtomasz 2013/12/10 05:07:01 Done.
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(fileName, {create: false, exclusive: false}, onExists,
hirono 2013/12/10 03:36:53 Please place each argument per line.
mtomasz 2013/12/10 05:07:01 Done.
232 entry.getParent(moveIfDoesNotExist.bind(null, entry), onError); 218 function() {
233 }, 219 this.entry_.moveTo(parentDir, fileName, onRenamed, onError);
234 onError); 220 }.bind(this));
221 }.bind(this);
222
223 this.entry_.getParent(moveIfDoesNotExist, onError);
235 }; 224 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698