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