getDirectory.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /*
  22. * getDirectory
  23. *
  24. * IN:
  25. * args
  26. * 0 - local filesytem URI for the base directory to search
  27. * 1 - directory to be created/returned; may be absolute path or relative path
  28. * 2 - options object
  29. * OUT:
  30. * success - DirectoryEntry
  31. * fail - FileError code
  32. */
  33. var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
  34. requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
  35. module.exports = function (success, fail, args) {
  36. var uri = args[0] === "/" ? "" : args[0],
  37. dir = args[1],
  38. options = args[2],
  39. onSuccess = function (entry) {
  40. if (typeof(success) === 'function') {
  41. success(entry);
  42. }
  43. },
  44. onFail = function (error) {
  45. if (typeof(fail) === 'function') {
  46. if (error && error.code) {
  47. //set error codes expected by mobile-spec tests
  48. if (error.code === FileError.INVALID_MODIFICATION_ERR && options.exclusive) {
  49. fail(FileError.PATH_EXISTS_ERR);
  50. } else if ( error.code === FileError.NOT_FOUND_ERR && dir.indexOf(':') > 0) {
  51. fail(FileError.ENCODING_ERR);
  52. } else {
  53. fail(error.code);
  54. }
  55. } else {
  56. fail(error);
  57. }
  58. }
  59. };
  60. resolve(function (entry) {
  61. requestAnimationFrame(function () {
  62. entry.nativeEntry.getDirectory(dir, options, function (nativeEntry) {
  63. resolve(function (entry) {
  64. onSuccess(entry);
  65. }, onFail, [uri + "/" + dir]);
  66. }, onFail);
  67. });
  68. }, onFail, [uri]);
  69. };