FileEntry.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 utils = require('cordova/utils'),
  22. exec = require('cordova/exec'),
  23. Entry = require('./Entry'),
  24. FileWriter = require('./FileWriter'),
  25. File = require('./File'),
  26. FileError = require('./FileError');
  27. /**
  28. * An interface representing a file on the file system.
  29. *
  30. * {boolean} isFile always true (readonly)
  31. * {boolean} isDirectory always false (readonly)
  32. * {DOMString} name of the file, excluding the path leading to it (readonly)
  33. * {DOMString} fullPath the absolute full path to the file (readonly)
  34. * {FileSystem} filesystem on which the file resides (readonly)
  35. */
  36. var FileEntry = function(name, fullPath, fileSystem, nativeURL) {
  37. // remove trailing slash if it is present
  38. if (fullPath && /\/$/.test(fullPath)) {
  39. fullPath = fullPath.substring(0, fullPath.length - 1);
  40. }
  41. if (nativeURL && /\/$/.test(nativeURL)) {
  42. nativeURL = nativeURL.substring(0, nativeURL.length - 1);
  43. }
  44. FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath, fileSystem, nativeURL]);
  45. };
  46. utils.extend(FileEntry, Entry);
  47. /**
  48. * Creates a new FileWriter associated with the file that this FileEntry represents.
  49. *
  50. * @param {Function} successCallback is called with the new FileWriter
  51. * @param {Function} errorCallback is called with a FileError
  52. */
  53. FileEntry.prototype.createWriter = function(successCallback, errorCallback) {
  54. this.file(function(filePointer) {
  55. var writer = new FileWriter(filePointer);
  56. if (writer.localURL === null || writer.localURL === "") {
  57. if (errorCallback) {
  58. errorCallback(new FileError(FileError.INVALID_STATE_ERR));
  59. }
  60. } else {
  61. if (successCallback) {
  62. successCallback(writer);
  63. }
  64. }
  65. }, errorCallback);
  66. };
  67. /**
  68. * Returns a File that represents the current state of the file that this FileEntry represents.
  69. *
  70. * @param {Function} successCallback is called with the new File object
  71. * @param {Function} errorCallback is called with a FileError
  72. */
  73. FileEntry.prototype.file = function(successCallback, errorCallback) {
  74. var localURL = this.toInternalURL();
  75. var win = successCallback && function(f) {
  76. var file = new File(f.name, localURL, f.type, f.lastModifiedDate, f.size);
  77. successCallback(file);
  78. };
  79. var fail = errorCallback && function(code) {
  80. errorCallback(new FileError(code));
  81. };
  82. exec(win, fail, "File", "getFileMetadata", [localURL]);
  83. };
  84. module.exports = FileEntry;