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