createEntryFromNative.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * createEntryFromNative
  23. *
  24. * IN
  25. * native - webkit Entry
  26. * OUT
  27. * returns Cordova entry
  28. */
  29. var info = require('cordova-plugin-file.bb10FileSystemInfo'),
  30. fileSystems = require('cordova-plugin-file.fileSystems');
  31. module.exports = function (native) {
  32. var entry = {
  33. nativeEntry: native,
  34. isDirectory: !!native.isDirectory,
  35. isFile: !!native.isFile,
  36. name: native.name,
  37. fullPath: native.fullPath,
  38. filesystemName: native.filesystem.name,
  39. nativeURL: native.toURL()
  40. },
  41. persistentPath = info.persistentPath.substring(7),
  42. temporaryPath = info.temporaryPath.substring(7);
  43. //fix bb10 webkit incorrect nativeURL
  44. if (native.filesystem.name === 'root') {
  45. entry.nativeURL = 'file:///' + FileSystem.encodeURIPath(native.fullPath);
  46. } else if (entry.nativeURL.indexOf('filesystem:local:///persistent/') === 0) {
  47. entry.nativeURL = info.persistentPath + FileSystem.encodeURIPath(native.fullPath);
  48. } else if (entry.nativeURL.indexOf('filesystem:local:///temporary') === 0) {
  49. entry.nativeURL = info.temporaryPath + FileSystem.encodeURIPath(native.fullPath);
  50. }
  51. //translate file system name from bb10 webkit
  52. if (entry.filesystemName === 'local__0:Persistent' || entry.fullPath.indexOf(persistentPath) !== -1) {
  53. entry.filesystemName = 'persistent';
  54. } else if (entry.filesystemName === 'local__0:Temporary' || entry.fullPath.indexOf(temporaryPath) !== -1) {
  55. entry.filesystemName = 'temporary';
  56. }
  57. //add file system property (will be called sync)
  58. fileSystems.getFs(entry.filesystemName, function (fs) {
  59. entry.filesystem = fs;
  60. });
  61. //set root on fullPath for persistent / temporary locations
  62. entry.fullPath = entry.fullPath.replace(persistentPath, "");
  63. entry.fullPath = entry.fullPath.replace(temporaryPath, "");
  64. //set trailing slash on directory
  65. if (entry.isDirectory && entry.fullPath.substring(entry.fullPath.length - 1) !== '/') {
  66. entry.fullPath += '/';
  67. }
  68. if (entry.isDirectory && entry.nativeURL.substring(entry.nativeURL.length - 1) !== '/') {
  69. entry.nativeURL += '/';
  70. }
  71. return entry;
  72. };