Preparing.js 7.6 KB

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