| 1 |
// Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Concrete implementations of the |
| 17 |
* goog.fs.DirectoryEntry, and goog.fs.FileEntry interfaces. |
| 18 |
*/ |
| 19 |
goog.provide('goog.fs.DirectoryEntryImpl'); |
| 20 |
goog.provide('goog.fs.EntryImpl'); |
| 21 |
goog.provide('goog.fs.FileEntryImpl'); |
| 22 |
|
| 23 |
goog.require('goog.array'); |
| 24 |
goog.require('goog.async.Deferred'); |
| 25 |
goog.require('goog.fs.DirectoryEntry'); |
| 26 |
goog.require('goog.fs.Entry'); |
| 27 |
goog.require('goog.fs.Error'); |
| 28 |
goog.require('goog.fs.FileEntry'); |
| 29 |
goog.require('goog.fs.FileWriter'); |
| 30 |
goog.require('goog.functions'); |
| 31 |
goog.require('goog.string'); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Base class for concrete implementations of goog.fs.Entry. |
| 37 |
* @param {!goog.fs.FileSystem} fs The wrapped filesystem. |
| 38 |
* @param {!Entry} entry The underlying Entry object. |
| 39 |
* @constructor |
| 40 |
* @implements {goog.fs.Entry} |
| 41 |
*/ |
| 42 |
goog.fs.EntryImpl = function(fs, entry) { |
| 43 |
/** |
| 44 |
* The wrapped filesystem. |
| 45 |
* |
| 46 |
* @type {!goog.fs.FileSystem} |
| 47 |
* @private |
| 48 |
*/ |
| 49 |
this.fs_ = fs; |
| 50 |
|
| 51 |
/** |
| 52 |
* The underlying Entry object. |
| 53 |
* |
| 54 |
* @type {!Entry} |
| 55 |
* @private |
| 56 |
*/ |
| 57 |
this.entry_ = entry; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
/** @override */ |
| 62 |
goog.fs.EntryImpl.prototype.isFile = function() { |
| 63 |
return this.entry_.isFile; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
/** @override */ |
| 68 |
goog.fs.EntryImpl.prototype.isDirectory = function() { |
| 69 |
return this.entry_.isDirectory; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
/** @override */ |
| 74 |
goog.fs.EntryImpl.prototype.getName = function() { |
| 75 |
return this.entry_.name; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
/** @override */ |
| 80 |
goog.fs.EntryImpl.prototype.getFullPath = function() { |
| 81 |
return this.entry_.fullPath; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
/** @override */ |
| 86 |
goog.fs.EntryImpl.prototype.getFileSystem = function() { |
| 87 |
return this.fs_; |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
/** @override */ |
| 92 |
goog.fs.EntryImpl.prototype.getLastModified = function() { |
| 93 |
return this.getMetadata().addCallback(function(metadata) { |
| 94 |
return metadata.modificationTime; |
| 95 |
}); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
/** @override */ |
| 100 |
goog.fs.EntryImpl.prototype.getMetadata = function() { |
| 101 |
var d = new goog.async.Deferred(); |
| 102 |
|
| 103 |
this.entry_.getMetadata(function(metadata) { |
| 104 |
d.callback(metadata); |
| 105 |
}, goog.bind(function(err) { |
| 106 |
var msg = 'retrieving metadata for ' + this.getFullPath(); |
| 107 |
d.errback(new goog.fs.Error(err, msg)); |
| 108 |
}, this)); |
| 109 |
return d; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
/** @override */ |
| 114 |
goog.fs.EntryImpl.prototype.moveTo = function(parent, opt_newName) { |
| 115 |
var d = new goog.async.Deferred(); |
| 116 |
this.entry_.moveTo( |
| 117 |
/** @type {!goog.fs.DirectoryEntryImpl} */ (parent).dir_, opt_newName, |
| 118 |
goog.bind(function(entry) { |
| 119 |
d.callback(this.wrapEntry(entry)); |
| 120 |
}, this), goog.bind(function(err) { |
| 121 |
var msg = 'moving ' + this.getFullPath() + ' into ' + |
| 122 |
parent.getFullPath() + |
| 123 |
(opt_newName ? ', renaming to ' + opt_newName : ''); |
| 124 |
d.errback(new goog.fs.Error(err, msg)); |
| 125 |
}, this)); |
| 126 |
return d; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
/** @override */ |
| 131 |
goog.fs.EntryImpl.prototype.copyTo = function(parent, opt_newName) { |
| 132 |
var d = new goog.async.Deferred(); |
| 133 |
this.entry_.copyTo( |
| 134 |
/** @type {!goog.fs.DirectoryEntryImpl} */ (parent).dir_, opt_newName, |
| 135 |
goog.bind(function(entry) { |
| 136 |
d.callback(this.wrapEntry(entry)); |
| 137 |
}, this), goog.bind(function(err) { |
| 138 |
var msg = 'copying ' + this.getFullPath() + ' into ' + |
| 139 |
parent.getFullPath() + |
| 140 |
(opt_newName ? ', renaming to ' + opt_newName : ''); |
| 141 |
d.errback(new goog.fs.Error(err, msg)); |
| 142 |
}, this)); |
| 143 |
return d; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
/** @override */ |
| 148 |
goog.fs.EntryImpl.prototype.wrapEntry = function(entry) { |
| 149 |
return entry.isFile ? |
| 150 |
new goog.fs.FileEntryImpl(this.fs_, /** @type {!FileEntry} */ (entry)) : |
| 151 |
new goog.fs.DirectoryEntryImpl( |
| 152 |
this.fs_, /** @type {!DirectoryEntry} */ (entry)); |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
/** @override */ |
| 157 |
goog.fs.EntryImpl.prototype.toUrl = function(opt_mimeType) { |
| 158 |
return this.entry_.toURL(opt_mimeType); |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
/** @override */ |
| 163 |
goog.fs.EntryImpl.prototype.toUri = goog.fs.EntryImpl.prototype.toUrl; |
| 164 |
|
| 165 |
|
| 166 |
/** @override */ |
| 167 |
goog.fs.EntryImpl.prototype.remove = function() { |
| 168 |
var d = new goog.async.Deferred(); |
| 169 |
this.entry_.remove( |
| 170 |
goog.bind(d.callback, d, true /* result */), goog.bind(function(err) { |
| 171 |
var msg = 'removing ' + this.getFullPath(); |
| 172 |
d.errback(new goog.fs.Error(err, msg)); |
| 173 |
}, this)); |
| 174 |
return d; |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
/** @override */ |
| 179 |
goog.fs.EntryImpl.prototype.getParent = function() { |
| 180 |
var d = new goog.async.Deferred(); |
| 181 |
this.entry_.getParent(goog.bind(function(parent) { |
| 182 |
d.callback(new goog.fs.DirectoryEntryImpl(this.fs_, parent)); |
| 183 |
}, this), goog.bind(function(err) { |
| 184 |
var msg = 'getting parent of ' + this.getFullPath(); |
| 185 |
d.errback(new goog.fs.Error(err, msg)); |
| 186 |
}, this)); |
| 187 |
return d; |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* A directory in a local FileSystem. |
| 194 |
* |
| 195 |
* This should not be instantiated directly. Instead, it should be accessed via |
| 196 |
* {@link goog.fs.FileSystem#getRoot} or |
| 197 |
* {@link goog.fs.DirectoryEntry#getDirectoryEntry}. |
| 198 |
* |
| 199 |
* @param {!goog.fs.FileSystem} fs The wrapped filesystem. |
| 200 |
* @param {!DirectoryEntry} dir The underlying DirectoryEntry object. |
| 201 |
* @constructor |
| 202 |
* @extends {goog.fs.EntryImpl} |
| 203 |
* @implements {goog.fs.DirectoryEntry} |
| 204 |
* @final |
| 205 |
*/ |
| 206 |
goog.fs.DirectoryEntryImpl = function(fs, dir) { |
| 207 |
goog.fs.DirectoryEntryImpl.base(this, 'constructor', fs, dir); |
| 208 |
|
| 209 |
/** |
| 210 |
* The underlying DirectoryEntry object. |
| 211 |
* |
| 212 |
* @type {!DirectoryEntry} |
| 213 |
* @private |
| 214 |
*/ |
| 215 |
this.dir_ = dir; |
| 216 |
}; |
| 217 |
goog.inherits(goog.fs.DirectoryEntryImpl, goog.fs.EntryImpl); |
| 218 |
|
| 219 |
|
| 220 |
/** @override */ |
| 221 |
goog.fs.DirectoryEntryImpl.prototype.getFile = function(path, opt_behavior) { |
| 222 |
var d = new goog.async.Deferred(); |
| 223 |
this.dir_.getFile( |
| 224 |
path, this.getOptions_(opt_behavior), goog.bind(function(entry) { |
| 225 |
d.callback(new goog.fs.FileEntryImpl(this.fs_, entry)); |
| 226 |
}, this), goog.bind(function(err) { |
| 227 |
var msg = 'loading file ' + path + ' from ' + this.getFullPath(); |
| 228 |
d.errback(new goog.fs.Error(err, msg)); |
| 229 |
}, this)); |
| 230 |
return d; |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
/** @override */ |
| 235 |
goog.fs.DirectoryEntryImpl.prototype.getDirectory = function( |
| 236 |
path, opt_behavior) { |
| 237 |
var d = new goog.async.Deferred(); |
| 238 |
this.dir_.getDirectory( |
| 239 |
path, this.getOptions_(opt_behavior), goog.bind(function(entry) { |
| 240 |
d.callback(new goog.fs.DirectoryEntryImpl(this.fs_, entry)); |
| 241 |
}, this), goog.bind(function(err) { |
| 242 |
var msg = 'loading directory ' + path + ' from ' + this.getFullPath(); |
| 243 |
d.errback(new goog.fs.Error(err, msg)); |
| 244 |
}, this)); |
| 245 |
return d; |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
/** @override */ |
| 250 |
goog.fs.DirectoryEntryImpl.prototype.createPath = function(path) { |
| 251 |
// If the path begins at the root, reinvoke createPath on the root directory. |
| 252 |
if (goog.string.startsWith(path, '/')) { |
| 253 |
var root = this.getFileSystem().getRoot(); |
| 254 |
if (this.getFullPath() != root.getFullPath()) { |
| 255 |
return root.createPath(path); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Filter out any empty path components caused by '//' or a leading slash. |
| 260 |
var parts = goog.array.filter(path.split('/'), goog.functions.identity); |
| 261 |
|
| 262 |
/** |
| 263 |
* @param {goog.fs.DirectoryEntryImpl} dir |
| 264 |
* @return {!goog.async.Deferred} |
| 265 |
*/ |
| 266 |
function getNextDirectory(dir) { |
| 267 |
if (!parts.length) { |
| 268 |
return goog.async.Deferred.succeed(dir); |
| 269 |
} |
| 270 |
|
| 271 |
var def; |
| 272 |
var nextDir = parts.shift(); |
| 273 |
|
| 274 |
if (nextDir == '..') { |
| 275 |
def = dir.getParent(); |
| 276 |
} else if (nextDir == '.') { |
| 277 |
def = goog.async.Deferred.succeed(dir); |
| 278 |
} else { |
| 279 |
def = dir.getDirectory(nextDir, goog.fs.DirectoryEntry.Behavior.CREATE); |
| 280 |
} |
| 281 |
return def.addCallback(getNextDirectory); |
| 282 |
} |
| 283 |
|
| 284 |
return getNextDirectory(this); |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
/** @override */ |
| 289 |
goog.fs.DirectoryEntryImpl.prototype.listDirectory = function() { |
| 290 |
var d = new goog.async.Deferred(); |
| 291 |
var reader = this.dir_.createReader(); |
| 292 |
var results = []; |
| 293 |
|
| 294 |
var errorCallback = goog.bind(function(err) { |
| 295 |
var msg = 'listing directory ' + this.getFullPath(); |
| 296 |
d.errback(new goog.fs.Error(err, msg)); |
| 297 |
}, this); |
| 298 |
|
| 299 |
var successCallback = goog.bind(function(entries) { |
| 300 |
if (entries.length) { |
| 301 |
for (var i = 0, entry; entry = entries[i]; i++) { |
| 302 |
results.push(this.wrapEntry(entry)); |
| 303 |
} |
| 304 |
reader.readEntries(successCallback, errorCallback); |
| 305 |
} else { |
| 306 |
d.callback(results); |
| 307 |
} |
| 308 |
}, this); |
| 309 |
|
| 310 |
reader.readEntries(successCallback, errorCallback); |
| 311 |
return d; |
| 312 |
}; |
| 313 |
|
| 314 |
|
| 315 |
/** @override */ |
| 316 |
goog.fs.DirectoryEntryImpl.prototype.removeRecursively = function() { |
| 317 |
var d = new goog.async.Deferred(); |
| 318 |
this.dir_.removeRecursively( |
| 319 |
goog.bind(d.callback, d, true /* result */), goog.bind(function(err) { |
| 320 |
var msg = 'removing ' + this.getFullPath() + ' recursively'; |
| 321 |
d.errback(new goog.fs.Error(err, msg)); |
| 322 |
}, this)); |
| 323 |
return d; |
| 324 |
}; |
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* Converts a value in the Behavior enum into an options object expected by the |
| 329 |
* File API. |
| 330 |
* |
| 331 |
* @param {goog.fs.DirectoryEntry.Behavior=} opt_behavior The behavior for |
| 332 |
* existing files. |
| 333 |
* @return {!Object<boolean>} The options object expected by the File API. |
| 334 |
* @private |
| 335 |
*/ |
| 336 |
goog.fs.DirectoryEntryImpl.prototype.getOptions_ = function(opt_behavior) { |
| 337 |
if (opt_behavior == goog.fs.DirectoryEntry.Behavior.CREATE) { |
| 338 |
return {'create': true}; |
| 339 |
} else if (opt_behavior == goog.fs.DirectoryEntry.Behavior.CREATE_EXCLUSIVE) { |
| 340 |
return {'create': true, 'exclusive': true}; |
| 341 |
} else { |
| 342 |
return {}; |
| 343 |
} |
| 344 |
}; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
/** |
| 349 |
* A file in a local filesystem. |
| 350 |
* |
| 351 |
* This should not be instantiated directly. Instead, it should be accessed via |
| 352 |
* {@link goog.fs.DirectoryEntry#getFile}. |
| 353 |
* |
| 354 |
* @param {!goog.fs.FileSystem} fs The wrapped filesystem. |
| 355 |
* @param {!FileEntry} file The underlying FileEntry object. |
| 356 |
* @constructor |
| 357 |
* @extends {goog.fs.EntryImpl} |
| 358 |
* @implements {goog.fs.FileEntry} |
| 359 |
* @final |
| 360 |
*/ |
| 361 |
goog.fs.FileEntryImpl = function(fs, file) { |
| 362 |
goog.fs.FileEntryImpl.base(this, 'constructor', fs, file); |
| 363 |
|
| 364 |
/** |
| 365 |
* The underlying FileEntry object. |
| 366 |
* |
| 367 |
* @type {!FileEntry} |
| 368 |
* @private |
| 369 |
*/ |
| 370 |
this.file_ = file; |
| 371 |
}; |
| 372 |
goog.inherits(goog.fs.FileEntryImpl, goog.fs.EntryImpl); |
| 373 |
|
| 374 |
|
| 375 |
/** @override */ |
| 376 |
goog.fs.FileEntryImpl.prototype.createWriter = function() { |
| 377 |
var d = new goog.async.Deferred(); |
| 378 |
this.file_.createWriter(function(w) { |
| 379 |
d.callback(new goog.fs.FileWriter(w)); |
| 380 |
}, goog.bind(function(err) { |
| 381 |
var msg = 'creating writer for ' + this.getFullPath(); |
| 382 |
d.errback(new goog.fs.Error(err, msg)); |
| 383 |
}, this)); |
| 384 |
return d; |
| 385 |
}; |
| 386 |
|
| 387 |
|
| 388 |
/** @override */ |
| 389 |
goog.fs.FileEntryImpl.prototype.file = function() { |
| 390 |
var d = new goog.async.Deferred(); |
| 391 |
this.file_.file(function(f) { d.callback(f); }, goog.bind(function(err) { |
| 392 |
var msg = 'getting file for ' + this.getFullPath(); |
| 393 |
d.errback(new goog.fs.Error(err, msg)); |
| 394 |
}, this)); |
| 395 |
return d; |
| 396 |
}; |