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
3 // found in the LICENSE file.
8 * Object representing an image item (a photo or a video).
10 * @param {FileEntry} entry Image entry.
13 Gallery.Item = function(entry) {
15 this.original_ = true;
19 * @return {FileEntry} Image entry.
21 Gallery.Item.prototype.getEntry = function() { return this.entry_ };
24 * @return {string} File name.
26 Gallery.Item.prototype.getFileName = function() {
27 return this.entry_.name;
31 * @return {boolean} True if this image has not been created in this session.
33 Gallery.Item.prototype.isOriginal = function() { return this.original_ };
37 * @type {string} Suffix for a edited copy file name.
39 Gallery.Item.COPY_SIGNATURE = ' - Edited';
42 * Regular expression to match '... - Edited'.
45 Gallery.Item.REGEXP_COPY_0 =
46 new RegExp('^(.+)' + Gallery.Item.COPY_SIGNATURE + '$');
49 * Regular expression to match '... - Edited (N)'.
52 Gallery.Item.REGEXP_COPY_N =
53 new RegExp('^(.+)' + Gallery.Item.COPY_SIGNATURE + ' \\((\\d+)\\)$');
56 * Creates a name for an edited copy of the file.
58 * @param {Entry} dirEntry Entry.
59 * @param {function} callback Callback.
62 Gallery.Item.prototype.createCopyName_ = function(dirEntry, callback) {
63 var name = this.getFileName();
65 // If the item represents a file created during the current Gallery session
66 // we reuse it for subsequent saves instead of creating multiple copies.
67 if (!this.original_) {
73 var index = name.lastIndexOf('.');
75 ext = name.substr(index);
76 name = name.substr(0, index);
79 if (!ext.match(/jpe?g/i)) {
80 // Chrome can natively encode only two formats: JPEG and PNG.
81 // All non-JPEG images are saved in PNG, hence forcing the file extension.
85 function tryNext(tries) {
86 // All the names are used. Let's overwrite the last one.
88 setTimeout(callback, 0, name + ext);
92 // If the file name contains the copy signature add/advance the sequential
94 var matchN = Gallery.Item.REGEXP_COPY_N.exec(name);
95 var match0 = Gallery.Item.REGEXP_COPY_0.exec(name);
96 if (matchN && matchN[1] && matchN[2]) {
97 var copyNumber = parseInt(matchN[2], 10) + 1;
98 name = matchN[1] + Gallery.Item.COPY_SIGNATURE + ' (' + copyNumber + ')';
99 } else if (match0 && match0[1]) {
100 name = match0[1] + Gallery.Item.COPY_SIGNATURE + ' (1)';
102 name += Gallery.Item.COPY_SIGNATURE;
105 dirEntry.getFile(name + ext, {create: false, exclusive: false},
106 tryNext.bind(null, tries - 1),
107 callback.bind(null, name + ext));
114 * Writes the new item content to the file.
116 * @param {Entry} overrideDir Directory to save to. If null, save to the same
117 * directory as the original.
118 * @param {boolean} overwrite True if overwrite, false if copy.
119 * @param {HTMLCanvasElement} canvas Source canvas.
120 * @param {ImageEncoder.MetadataEncoder} metadataEncoder MetadataEncoder.
121 * @param {function(boolean)=} opt_callback Callback accepting true for success.
123 Gallery.Item.prototype.saveToFile = function(
124 overrideDir, overwrite, canvas, metadataEncoder, opt_callback) {
125 ImageUtil.metrics.startInterval(ImageUtil.getMetricName('SaveTime'));
127 var name = this.getFileName();
129 var onSuccess = function(entry) {
130 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 1, 2);
131 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('SaveTime'));
133 if (opt_callback) opt_callback(true);
136 function onError(error) {
137 console.error('Error saving from gallery', name, error);
138 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 0, 2);
139 if (opt_callback) opt_callback(false);
142 function doSave(newFile, fileEntry) {
143 fileEntry.createWriter(function(fileWriter) {
144 function writeContent() {
145 fileWriter.onwriteend = onSuccess.bind(null, fileEntry);
146 fileWriter.write(ImageEncoder.getBlob(canvas, metadataEncoder));
148 fileWriter.onerror = function(error) {
150 // Disable all callbacks on the first error.
151 fileWriter.onerror = null;
152 fileWriter.onwriteend = null;
157 fileWriter.onwriteend = writeContent;
158 fileWriter.truncate(0);
163 function getFile(dir, newFile) {
164 dir.getFile(name, {create: newFile, exclusive: newFile},
165 doSave.bind(null, newFile), onError);
168 function checkExistence(dir) {
169 dir.getFile(name, {create: false, exclusive: false},
170 getFile.bind(null, dir, false /* existing file */),
171 getFile.bind(null, dir, true /* create new file */));
174 var saveToDir = function(dir) {
178 this.createCopyName_(dir, function(copyName) {
179 this.original_ = false;
187 saveToDir(overrideDir);
189 this.entry_.getParent(saveToDir, onError);
196 * @param {string} displayName New display name (without the extension).
197 * @param {function()} onSuccess Success callback.
198 * @param {function()} onExists Called if the file with the new name exists.
200 Gallery.Item.prototype.rename = function(displayName, onSuccess, onExists) {
201 var newFileName = this.entry_.name.replace(
202 ImageUtil.getDisplayNameFromName(this.entry_.name), displayName);
204 if (newFileName === this.entry_.name)
207 var onRenamed = function(entry) {
212 var onError = function() {
214 'Rename error: "' + this.entry_.name + '" to "' + newFileName + '"');
217 var moveIfDoesNotExist = function(parentDir) {
220 {create: false, exclusive: false},
223 this.entry_.moveTo(parentDir, newFileName, onRenamed, onError);
227 this.entry_.getParent(moveIfDoesNotExist, onError);