requestFileSystem.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. //For browser platform: not all browsers use this file.
  23. function checkBrowser() {
  24. if (cordova.platformId === "browser" && require('./isChrome')()) {
  25. module.exports = window.requestFileSystem || window.webkitRequestFileSystem;
  26. return true;
  27. }
  28. return false;
  29. }
  30. if (checkBrowser()) {
  31. return;
  32. }
  33. var argscheck = require('cordova/argscheck'),
  34. FileError = require('./FileError'),
  35. FileSystem = require('./FileSystem'),
  36. exec = require('cordova/exec');
  37. var fileSystems = require('./fileSystems');
  38. /**
  39. * Request a file system in which to store application data.
  40. * @param type local file system type
  41. * @param size indicates how much storage space, in bytes, the application expects to need
  42. * @param successCallback invoked with a FileSystem object
  43. * @param errorCallback invoked if error occurs retrieving file system
  44. */
  45. var requestFileSystem = function(type, size, successCallback, errorCallback) {
  46. argscheck.checkArgs('nnFF', 'requestFileSystem', arguments);
  47. var fail = function(code) {
  48. if (errorCallback) {
  49. errorCallback(new FileError(code));
  50. }
  51. };
  52. if (type < 0) {
  53. fail(FileError.SYNTAX_ERR);
  54. } else {
  55. // if successful, return a FileSystem object
  56. var success = function(file_system) {
  57. if (file_system) {
  58. if (successCallback) {
  59. fileSystems.getFs(file_system.name, function(fs) {
  60. // This should happen only on platforms that haven't implemented requestAllFileSystems (windows)
  61. if (!fs) {
  62. fs = new FileSystem(file_system.name, file_system.root);
  63. }
  64. successCallback(fs);
  65. });
  66. }
  67. }
  68. else {
  69. // no FileSystem object returned
  70. fail(FileError.NOT_FOUND_ERR);
  71. }
  72. };
  73. exec(success, fail, "File", "requestFileSystem", [type, size]);
  74. }
  75. };
  76. module.exports = requestFileSystem;
  77. })();