WebOrbiton
v1.5.0

AtheraStudio Code

190 lines · 5.8 KB
  1. (function () {
  2. "use strict";
  3. ​
  4. var A = window.Athera;
  5. var util = A.util;
  6. var fs = A.fs;
  7. ​
  8. var DIR = ".backups";
  9. var MAX_SIZE = 0xffffffff;
  10. var MAX_ENTRIES = 0xffff;
  11. ​
  12. var crcTable = (function () {
  13. var table = new Uint32Array(256);
  14. for (var n = 0; n < 256; n++) {
  15. var c = n;
  16. for (var k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
  17. table[n] = c >>> 0;
  18. }
  19. return table;
  20. })();
  21. ​
  22. function crc32(bytes) {
  23. var c = 0xffffffff;
  24. for (var i = 0; i < bytes.length; i++) c = crcTable[(c ^ bytes[i]) & 255] ^ (c >>> 8);
  25. return (c ^ 0xffffffff) >>> 0;
  26. }
  27. ​
  28. function two(n) {
  29. return n < 10 ? "0" + n : String(n);
  30. }
  31. ​
  32. function stamp(d) {
  33. return d.getFullYear() + "-" + two(d.getMonth() + 1) + "-" + two(d.getDate()) + "-" +
  34. two(d.getHours()) + "-" + two(d.getMinutes()) + "-" + two(d.getSeconds());
  35. }
  36. ​
  37. function dosStamp(ms) {
  38. var d = new Date(ms);
  39. var year = Math.min(2107, Math.max(1980, d.getFullYear()));
  40. return {
  41. time: (d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() >> 1),
  42. date: ((year - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate()
  43. };
  44. }
  45. ​
  46. function tooLarge() {
  47. return new Error(A.t("backup.too_large"));
  48. }
  49. ​
  50. async function deflate(bytes) {
  51. if (typeof CompressionStream !== "function") return null;
  52. try {
  53. var stream = new Blob([bytes]).stream().pipeThrough(new CompressionStream("deflate-raw"));
  54. return new Uint8Array(await new Response(stream).arrayBuffer());
  55. } catch (e) {
  56. return null;
  57. }
  58. }
  59. ​
  60. async function walk(provider, dir, out) {
  61. var count = 0;
  62. for (var e of await provider.list(dir)) {
  63. var path = util.join(dir, e.name);
  64. if (e.kind === "dir") {
  65. if (fs.HIDDEN_DIRS.indexOf(e.name) >= 0) continue;
  66. if (!dir && e.name === fs.PRIVATE_DIR && !fs.includePrivate()) continue;
  67. count++;
  68. await walk(provider, path, out);
  69. } else if (fs.HIDDEN_FILES.indexOf(e.name) < 0) {
  70. count++;
  71. out.push({ path: path, dir: false });
  72. }
  73. }
  74. if (!count && dir) out.push({ path: dir, dir: true });
  75. }
  76. ​
  77. function localHeader(name, method, time, crc, packed, size) {
  78. var bytes = new Uint8Array(30);
  79. var v = new DataView(bytes.buffer);
  80. v.setUint32(0, 0x04034b50, true);
  81. v.setUint16(4, 20, true);
  82. v.setUint16(6, 0x0800, true);
  83. v.setUint16(8, method, true);
  84. v.setUint16(10, time.time, true);
  85. v.setUint16(12, time.date, true);
  86. v.setUint32(14, crc, true);
  87. v.setUint32(18, packed, true);
  88. v.setUint32(22, size, true);
  89. v.setUint16(26, name.length, true);
  90. return bytes;
  91. }
  92. ​
  93. function centralHeader(name, method, time, crc, packed, size, isDir, offset) {
  94. var bytes = new Uint8Array(46);
  95. var v = new DataView(bytes.buffer);
  96. v.setUint32(0, 0x02014b50, true);
  97. v.setUint16(4, 20, true);
  98. v.setUint16(6, 20, true);
  99. v.setUint16(8, 0x0800, true);
  100. v.setUint16(10, method, true);
  101. v.setUint16(12, time.time, true);
  102. v.setUint16(14, time.date, true);
  103. v.setUint32(16, crc, true);
  104. v.setUint32(20, packed, true);
  105. v.setUint32(24, size, true);
  106. v.setUint16(28, name.length, true);
  107. v.setUint32(38, isDir ? 0x10 : 0, true);
  108. v.setUint32(42, offset, true);
  109. return bytes;
  110. }
  111. ​
  112. function endRecord(count, size, offset) {
  113. var bytes = new Uint8Array(22);
  114. var v = new DataView(bytes.buffer);
  115. v.setUint32(0, 0x06054b50, true);
  116. v.setUint16(8, count, true);
  117. v.setUint16(10, count, true);
  118. v.setUint32(12, size, true);
  119. v.setUint32(16, offset, true);
  120. return bytes;
  121. }
  122. ​
  123. async function build(provider) {
  124. var items = [];
  125. await walk(provider, "", items);
  126. if (items.length > MAX_ENTRIES) throw tooLarge();
  127. ​
  128. var encoder = new TextEncoder();
  129. var parts = [], central = [], centralSize = 0, offset = 0;
  130. ​
  131. for (var item of items) {
  132. var name = encoder.encode(item.dir ? item.path + "/" : item.path);
  133. var raw = new Uint8Array(0), modified = Date.now();
  134. if (!item.dir) {
  135. var file = await provider.readFile(item.path);
  136. raw = new Uint8Array(await file.arrayBuffer());
  137. modified = file.lastModified || modified;
  138. }
  139. if (raw.length > MAX_SIZE) throw tooLarge();
  140. ​
  141. var data = raw, method = 0;
  142. if (raw.length) {
  143. var packed = await deflate(raw);
  144. if (packed && packed.length < raw.length) { data = packed; method = 8; }
  145. }
  146. ​
  147. var crc = raw.length ? crc32(raw) : 0;
  148. var time = dosStamp(modified);
  149. var local = localHeader(name, method, time, crc, data.length, raw.length);
  150. var entry = centralHeader(name, method, time, crc, data.length, raw.length, item.dir, offset);
  151. ​
  152. parts.push(local, name, data);
  153. central.push(entry, name);
  154. centralSize += entry.length + name.length;
  155. offset += local.length + name.length + data.length;
  156. if (offset > MAX_SIZE) throw tooLarge();
  157. }
  158. ​
  159. parts.push.apply(parts, central);
  160. parts.push(endRecord(items.length, centralSize, offset));
  161. return new Blob(parts, { type: "application/zip" });
  162. }
  163. ​
  164. var busy = false;
  165. ​
  166. async function run() {
  167. var provider = fs.provider;
  168. if (!provider) return A.ui.toast(A.t("app.open_folder_first"), "info");
  169. if (busy) return;
  170. busy = true;
  171. try {
  172. A.ui.toast(A.t("backup.started"), "info");
  173. var name = stamp(new Date()) + ".zip";
  174. var blob = await build(provider);
  175. if (provider.kind === "disk") {
  176. var path = util.join(DIR, name);
  177. await provider.write(path, blob);
  178. A.ui.toast(A.t("backup.done", { path: path }), "success");
  179. } else {
  180. fs.download(name, blob, "application/zip");
  181. A.ui.toast(A.t("backup.downloaded", { name: name }), "success");
  182. }
  183. } finally {
  184. busy = false;
  185. }
  186. }
  187. ​
  188. A.backup = { run: run, build: build, stamp: stamp, DIR: DIR };
  189. })();
  190. ​