You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.1 KiB

  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. * copyTo
  23. *
  24. * IN:
  25. * args
  26. * 0 - URL of entry to copy
  27. * 1 - URL of the directory into which to copy/move the entry
  28. * 2 - the new name of the entry, defaults to the current name
  29. * move - if true, delete the entry which was copied
  30. * OUT:
  31. * success - entry for the copied file or directory
  32. * fail - FileError
  33. */
  34. var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
  35. requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame');
  36. module.exports = function (success, fail, args, move) {
  37. var uri = args[0],
  38. destination = args[1],
  39. fileName = args[2],
  40. copiedEntry;
  41. function onSuccess() {
  42. resolve(
  43. function (entry) {
  44. if (typeof(success) === 'function') {
  45. success(entry);
  46. }
  47. },
  48. onFail,
  49. [destination + copiedEntry.name]
  50. );
  51. }
  52. function onFail(error) {
  53. if (typeof(fail) === 'function') {
  54. if (error && error.code) {
  55. //set error codes expected by mobile spec
  56. if (uri === destination) {
  57. fail(FileError.INVALID_MODIFICATION_ERR);
  58. } else if (error.code === FileError.SECURITY_ERR) {
  59. fail(FileError.INVALID_MODIFICATION_ERR);
  60. } else {
  61. fail(error.code);
  62. }
  63. } else {
  64. fail(error);
  65. }
  66. }
  67. }
  68. function writeFile(fileEntry, blob, entry) {
  69. copiedEntry = fileEntry;
  70. fileEntry.createWriter(function (fileWriter) {
  71. fileWriter.onwriteend = function () {
  72. if (move) {
  73. entry.nativeEntry.remove(onSuccess, function () {
  74. console.error("Move operation failed. Files may exist at both source and destination");
  75. });
  76. } else {
  77. onSuccess();
  78. }
  79. };
  80. fileWriter.onerror = onFail;
  81. fileWriter.write(blob);
  82. }, onFail);
  83. }
  84. function copyFile(entry) {
  85. if (entry.nativeEntry.file) {
  86. entry.nativeEntry.file(function (file) {
  87. var reader = new FileReader()._realReader;
  88. reader.onloadend = function (e) {
  89. var contents = new Uint8Array(this.result),
  90. blob = new Blob([contents]);
  91. resolve(function (destEntry) {
  92. requestAnimationFrame(function () {
  93. destEntry.nativeEntry.getFile(fileName, {create: true}, function (fileEntry) {
  94. writeFile(fileEntry, blob, entry);
  95. }, onFail);
  96. });
  97. }, onFail, [destination]);
  98. };
  99. reader.onerror = onFail;
  100. reader.readAsArrayBuffer(file);
  101. }, onFail);
  102. } else {
  103. onFail(FileError.INVALID_MODIFICATION_ERR);
  104. }
  105. }
  106. function copyDirectory(entry) {
  107. resolve(function (destEntry) {
  108. if (entry.filesystemName !== destEntry.filesystemName) {
  109. console.error("Copying directories between filesystems is not supported on BB10");
  110. onFail(FileError.INVALID_MODIFICATION_ERR);
  111. } else {
  112. entry.nativeEntry.copyTo(destEntry.nativeEntry, fileName, function () {
  113. resolve(function (copiedDir) {
  114. copiedEntry = copiedDir;
  115. if (move) {
  116. entry.nativeEntry.removeRecursively(onSuccess, onFail);
  117. } else {
  118. onSuccess();
  119. }
  120. }, onFail, [destination + fileName]);
  121. }, onFail);
  122. }
  123. }, onFail, [destination]);
  124. }
  125. if (destination + fileName === uri) {
  126. onFail(FileError.INVALID_MODIFICATION_ERR);
  127. } else if (fileName.indexOf(':') > -1) {
  128. onFail(FileError.ENCODING_ERR);
  129. } else {
  130. resolve(function (entry) {
  131. if (entry.isDirectory) {
  132. copyDirectory(entry);
  133. } else {
  134. copyFile(entry);
  135. }
  136. }, onFail, [uri]);
  137. }
  138. };