No Description

DirectoryEntry.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var argscheck = require('cordova/argscheck');
  22. var utils = require('cordova/utils');
  23. var exec = require('cordova/exec');
  24. var Entry = require('./Entry');
  25. var FileError = require('./FileError');
  26. var DirectoryReader = require('./DirectoryReader');
  27. /**
  28. * An interface representing a directory on the file system.
  29. *
  30. * {boolean} isFile always false (readonly)
  31. * {boolean} isDirectory always true (readonly)
  32. * {DOMString} name of the directory, excluding the path leading to it (readonly)
  33. * {DOMString} fullPath the absolute full path to the directory (readonly)
  34. * {FileSystem} filesystem on which the directory resides (readonly)
  35. */
  36. var DirectoryEntry = function (name, fullPath, fileSystem, nativeURL) {
  37. // add trailing slash if it is missing
  38. if ((fullPath) && !/\/$/.test(fullPath)) {
  39. fullPath += '/';
  40. }
  41. // add trailing slash if it is missing
  42. if (nativeURL && !/\/$/.test(nativeURL)) {
  43. nativeURL += '/';
  44. }
  45. DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath, fileSystem, nativeURL);
  46. };
  47. utils.extend(DirectoryEntry, Entry);
  48. /**
  49. * Creates a new DirectoryReader to read entries from this directory
  50. */
  51. DirectoryEntry.prototype.createReader = function () {
  52. return new DirectoryReader(this.toInternalURL());
  53. };
  54. /**
  55. * Creates or looks up a directory
  56. *
  57. * @param {DOMString} path either a relative or absolute path from this directory in which to look up or create a directory
  58. * @param {Flags} options to create or exclusively create the directory
  59. * @param {Function} successCallback is called with the new entry
  60. * @param {Function} errorCallback is called with a FileError
  61. */
  62. DirectoryEntry.prototype.getDirectory = function (path, options, successCallback, errorCallback) {
  63. argscheck.checkArgs('sOFF', 'DirectoryEntry.getDirectory', arguments);
  64. var fs = this.filesystem;
  65. var win = successCallback && function (result) {
  66. var entry = new DirectoryEntry(result.name, result.fullPath, fs, result.nativeURL);
  67. successCallback(entry);
  68. };
  69. var fail = errorCallback && function (code) {
  70. errorCallback(new FileError(code));
  71. };
  72. exec(win, fail, 'File', 'getDirectory', [this.toInternalURL(), path, options]);
  73. };
  74. /**
  75. * Deletes a directory and all of it's contents
  76. *
  77. * @param {Function} successCallback is called with no parameters
  78. * @param {Function} errorCallback is called with a FileError
  79. */
  80. DirectoryEntry.prototype.removeRecursively = function (successCallback, errorCallback) {
  81. argscheck.checkArgs('FF', 'DirectoryEntry.removeRecursively', arguments);
  82. var fail = errorCallback && function (code) {
  83. errorCallback(new FileError(code));
  84. };
  85. exec(successCallback, fail, 'File', 'removeRecursively', [this.toInternalURL()]);
  86. };
  87. /**
  88. * Creates or looks up a file
  89. *
  90. * @param {DOMString} path either a relative or absolute path from this directory in which to look up or create a file
  91. * @param {Flags} options to create or exclusively create the file
  92. * @param {Function} successCallback is called with the new entry
  93. * @param {Function} errorCallback is called with a FileError
  94. */
  95. DirectoryEntry.prototype.getFile = function (path, options, successCallback, errorCallback) {
  96. argscheck.checkArgs('sOFF', 'DirectoryEntry.getFile', arguments);
  97. var fs = this.filesystem;
  98. var win = successCallback && function (result) {
  99. var FileEntry = require('./FileEntry');
  100. var entry = new FileEntry(result.name, result.fullPath, fs, result.nativeURL);
  101. successCallback(entry);
  102. };
  103. var fail = errorCallback && function (code) {
  104. errorCallback(new FileError(code));
  105. };
  106. exec(win, fail, 'File', 'getFile', [this.toInternalURL(), path, options]);
  107. };
  108. module.exports = DirectoryEntry;