openEventsPlatformApp/plugins/cordova-plugin-file/www/browser/Preparing.js
plugins/cordova-plugin-file/www/browser/Preparing.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(function () {
23 /*global require*/
24
25 //Only Chrome uses this file.
26 if (!require('./isChrome')()) {
27 return;
28 }
29
30 var channel = require('cordova/channel');
31 var FileError = require('./FileError');
32 var PERSISTENT_FS_QUOTA = 5 * 1024 * 1024;
33 var filePluginIsReadyEvent = new Event('filePluginIsReady');
34
35 var entryFunctionsCreated = false;
36 var quotaWasRequested = false;
37 var eventWasThrown = false;
38
39 if (!window.requestFileSystem) {
40 window.requestFileSystem = function(type, size, win, fail) {
41 if (fail) {
42 fail("Not supported");
43 }
44 };
45 } else {
46 window.requestFileSystem(window.TEMPORARY, 1, createFileEntryFunctions, function() {});
47 }
48
49 if (!window.resolveLocalFileSystemURL) {
50 window.resolveLocalFileSystemURL = function(url, win, fail) {
51 if(fail) {
52 fail("Not supported");
53 }
54 };
55 }
56
57 // Resolves a filesystem entry by its path - which is passed either in standard (filesystem:file://) or
58 // Cordova-specific (cdvfile://) universal way.
59 // Aligns with specification: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#widl-LocalFileSystem-resolveLocalFileSystemURL
60 var nativeResolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL;
61 window.resolveLocalFileSystemURL = function(url, win, fail) {
62 /* If url starts with `cdvfile` then we need convert it to Chrome real url first:
63 cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file */
64 if (url.trim().substr(0,7) === "cdvfile") {
65 /* Quirk:
66 Plugin supports cdvfile://localhost (local resources) only.
67 I.e. external resources are not supported via cdvfile. */
68 if (url.indexOf("cdvfile://localhost") !== -1) {
69 // Browser supports temporary and persistent only
70 var indexPersistent = url.indexOf('persistent');
71 var indexTemporary = url.indexOf('temporary');
72
73 /* Chrome urls start with 'filesystem:' prefix. See quirk:
74 toURL function in Chrome returns filesystem:-prefixed path depending on application host.
75 For example, filesystem:file:///persistent/somefile.txt,
76 filesystem:http://localhost:8080/persistent/somefile.txt. */
77 var prefix = 'filesystem:file:///';
78 if (location.protocol !== 'file:') {
79 prefix = 'filesystem:' + location.origin + '/';
80 }
81
82 var result;
83 if (indexPersistent !== -1) {
84 // cdvfile://localhost/persistent/path/to/file -> filesystem:file://persistent/path/to/file
85 // or filesystem:http://localhost:8080/persistent/path/to/file
86 result = prefix + 'persistent' + url.substr(indexPersistent + 10);
87 nativeResolveLocalFileSystemURL(result, win, fail);
88 return;
89 }
90
91 if (indexTemporary !== -1) {
92 // cdvfile://localhost/temporary/path/to/file -> filesystem:file://temporary/path/to/file
93 // or filesystem:http://localhost:8080/temporary/path/to/file
94 result = prefix + 'temporary' + url.substr(indexTemporary + 9);
95 nativeResolveLocalFileSystemURL(result, win, fail);
96 return;
97 }
98 }
99
100 // cdvfile other than local file resource is not supported
101 if (fail) {
102 fail(new FileError(FileError.ENCODING_ERR));
103 }
104 } else {
105 nativeResolveLocalFileSystemURL(url, win, fail);
106 }
107 };
108
109 function createFileEntryFunctions(fs) {
110 fs.root.getFile('todelete_658674_833_4_cdv', {create: true}, function(fileEntry) {
111 var fileEntryType = Object.getPrototypeOf(fileEntry);
112 var entryType = Object.getPrototypeOf(fileEntryType);
113
114 // Save the original method
115 var origToURL = entryType.toURL;
116 entryType.toURL = function () {
117 var origURL = origToURL.call(this);
118 if (this.isDirectory && origURL.substr(-1) !== '/') {
119 return origURL + '/';
120 }
121 return origURL;
122 };
123
124 entryType.toNativeURL = function () {
125 console.warn("DEPRECATED: Update your code to use 'toURL'");
126 return this.toURL();
127 };
128
129 entryType.toInternalURL = function() {
130 if (this.toURL().indexOf("persistent") > -1) {
131 return "cdvfile://localhost/persistent" + this.fullPath;
132 }
133
134 if (this.toURL().indexOf("temporary") > -1) {
135 return "cdvfile://localhost/temporary" + this.fullPath;
136 }
137 };
138
139 entryType.setMetadata = function(win, fail /*, metadata*/) {
140 if (fail) {
141 fail("Not supported");
142 }
143 };
144
145 fileEntry.createWriter(function(writer) {
146 var originalWrite = writer.write;
147 var writerProto = Object.getPrototypeOf(writer);
148 writerProto.write = function(blob) {
149 if(blob instanceof Blob) {
150 originalWrite.apply(this, [blob]);
151 } else {
152 var realBlob = new Blob([blob]);
153 originalWrite.apply(this, [realBlob]);
154 }
155 };
156
157 fileEntry.remove(function(){ entryFunctionsCreated = true; }, function(){ /* empty callback */ });
158 });
159 });
160 }
161
162 window.initPersistentFileSystem = function(size, win, fail) {
163 if (navigator.webkitPersistentStorage) {
164 navigator.webkitPersistentStorage.requestQuota(size, win, fail);
165 return;
166 }
167
168 fail("This browser does not support this function");
169 };
170
171 window.isFilePluginReadyRaised = function () { return eventWasThrown; };
172
173 window.initPersistentFileSystem(PERSISTENT_FS_QUOTA, function() {
174 console.log('Persistent fs quota granted');
175 quotaWasRequested = true;
176 }, function(e){
177 console.log('Error occured while trying to request Persistent fs quota: ' + JSON.stringify(e));
178 });
179
180 channel.onCordovaReady.subscribe(function () {
181 function dispatchEventIfReady() {
182 if (entryFunctionsCreated && quotaWasRequested) {
183 window.dispatchEvent(filePluginIsReadyEvent);
184 eventWasThrown = true;
185 } else {
186 setTimeout(dispatchEventIfReady, 100);
187 }
188 }
189
190 dispatchEventIfReady();
191 }, false);
192})();