storage.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2011 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile Core.
  6. *
  7. * SingleFile Core is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile Core is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile Core. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. (function() {
  21. var STORAGE_SIZE = 1073741824, FILENAME_MAX_LENGTH = 256, BOM, fs;
  22. singlefile.storage = {};
  23. singlefile.storage.isEnabled = typeof requestFileSystem != "undefined" && typeof ArrayBuffer != "undefined" && typeof Uint8Array != "undefined";
  24. function init() {
  25. var view;
  26. if (!singlefile.storage.isEnabled)
  27. return;
  28. BOM = new ArrayBuffer(3);
  29. view = new Uint8Array(BOM);
  30. view.set([ 0xEF, 0xBB, 0xBF ]);
  31. requestFileSystem(true, STORAGE_SIZE, function(filesystem) {
  32. fs = filesystem;
  33. singlefile.storage.isEnabled = true;
  34. }, function(e) {
  35. singlefile.storage.isEnabled = false;
  36. console.log(e);
  37. });
  38. }
  39. singlefile.storage.addContent = function(name, content, maxLength, callback, index) {
  40. var suffix = (index ? " (" + (index + 1) + ")" : ""), max = maxLength - suffix.length, filename = (name.length > max - 6 ? name.substring(0, max - 6)
  41. + "[...] " : name)
  42. + suffix + ".html";
  43. if (fs) {
  44. fs.root.getFile(filename, {
  45. create : true,
  46. exclusive : true
  47. }, function(fileEntry) {
  48. fileEntry.createWriter(function(fileWriter) {
  49. var blobBuilder = new BlobBuilder();
  50. blobBuilder.append(BOM);
  51. blobBuilder.append(content);
  52. fileWriter.onerror = function(e) {
  53. callback(false, filename);
  54. };
  55. fileWriter.onwrite = function(e) {
  56. callback(true, filename);
  57. };
  58. fileWriter.write(blobBuilder.getBlob());
  59. }, function(e) {
  60. console.log(e);
  61. callback(false, filename);
  62. });
  63. }, function(e) {
  64. if (e.code == e.INVALID_MODIFICATION_ERR) {
  65. index = index || 0;
  66. index++;
  67. singlefile.storage.addContent(name, content, maxLength, callback, index);
  68. } else {
  69. console.log(e);
  70. callback(false, filename);
  71. }
  72. });
  73. } else
  74. callback(false, filename);
  75. };
  76. singlefile.storage.reset = function() {
  77. var rootReader;
  78. if (fs) {
  79. rootReader = fs.root.createReader("/");
  80. rootReader.readEntries(function(entries) {
  81. var i;
  82. for (i = 0; i < entries.length; i++)
  83. entries[i].remove();
  84. });
  85. }
  86. };
  87. init();
  88. })();