storage.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, requestFS = window.requestFileSystem || window.webkitRequestFileSystem, BBuilder = window.BlobBuilder
  22. || window.WebKitBlobBuilder;
  23. singlefile.storage = {};
  24. singlefile.storage.isEnabled = typeof requestFS != "undefined" && typeof ArrayBuffer != "undefined" && typeof Uint8Array != "undefined";
  25. function init() {
  26. var view;
  27. if (!singlefile.storage.isEnabled)
  28. return;
  29. BOM = new ArrayBuffer(3);
  30. view = new Uint8Array(BOM);
  31. view.set([ 0xEF, 0xBB, 0xBF ]);
  32. requestFS(true, STORAGE_SIZE, function(filesystem) {
  33. fs = filesystem;
  34. singlefile.storage.isEnabled = true;
  35. }, function(e) {
  36. singlefile.storage.isEnabled = false;
  37. console.log(e);
  38. });
  39. }
  40. singlefile.storage.addContent = function(name, content, maxLength, callback, index) {
  41. var suffix = (index ? " (" + (index + 1) + ")" : ""), max = maxLength - suffix.length, filename = (name.length > max - 6 ? name.substring(0, max - 6)
  42. + "[...] " : name)
  43. + suffix + ".html";
  44. if (fs) {
  45. fs.root.getFile(filename, {
  46. create : true,
  47. exclusive : true
  48. }, function(fileEntry) {
  49. fileEntry.createWriter(function(fileWriter) {
  50. var blobBuilder = new BBuilder();
  51. blobBuilder.append(BOM);
  52. blobBuilder.append(content);
  53. fileWriter.onerror = function(e) {
  54. callback(false, filename);
  55. };
  56. fileWriter.onwrite = function(e) {
  57. callback(true, filename);
  58. };
  59. fileWriter.write(blobBuilder.getBlob());
  60. }, function(e) {
  61. console.log(e);
  62. callback(false, filename);
  63. });
  64. }, function(e) {
  65. if (e.code == e.INVALID_MODIFICATION_ERR) {
  66. index = index || 0;
  67. index++;
  68. singlefile.storage.addContent(name, content, maxLength, callback, index);
  69. } else {
  70. console.log(e);
  71. callback(false, filename);
  72. }
  73. });
  74. } else
  75. callback(false, filename);
  76. };
  77. singlefile.storage.reset = function() {
  78. var rootReader;
  79. if (fs) {
  80. rootReader = fs.root.createReader("/");
  81. rootReader.readEntries(function(entries) {
  82. var i;
  83. for (i = 0; i < entries.length; i++)
  84. entries[i].remove();
  85. });
  86. }
  87. };
  88. init();
  89. })();