openEventsPlatformApp/plugins/cordova-plugin-file/www/blackberry10/write.js
plugins/cordova-plugin-file/www/blackberry10/write.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 * write
24 *
25 * IN:
26 * args
27 * 0 - URL of file to write
28 * 1 - data to write
29 * 2 - offset
30 * 3 - isBinary
31 * OUT:
32 * success - bytes written
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) {
40 var uri = args[0],
41 data = args[1],
42 offset = args[2],
43 //isBinary = args[3],
44 onSuccess = function (data) {
45 if (typeof success === 'function') {
46 success(data.loaded);
47 }
48 },
49 onFail = function (error) {
50 if (typeof fail === 'function') {
51 if (error && error.code) {
52 fail(error.code);
53 } else if (error && error.target && error.target.code) {
54 fail(error.target.code);
55 } else {
56 fail(error);
57 }
58 }
59 };
60 resolve(function (fs) {
61 requestAnimationFrame(function () {
62 fs.nativeEntry.createWriter(function (writer) {
63 var blob = new Blob([data]);
64 if (offset) {
65 writer.seek(offset);
66 }
67 writer.onwriteend = onSuccess;
68 writer.onerror = onFail;
69 writer.write(blob);
70 }, onFail);
71 });
72 }, fail, [uri, { create: true }]);
73};