ぼざクリ タグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

831 lines
29 KiB

  1. (function(global, factory) {
  2. typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
  3. factory(global.ActiveStorage = {}));
  4. })(this, (function(exports) {
  5. "use strict";
  6. var sparkMd5 = {
  7. exports: {}
  8. };
  9. (function(module, exports) {
  10. (function(factory) {
  11. {
  12. module.exports = factory();
  13. }
  14. })((function(undefined$1) {
  15. var hex_chr = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" ];
  16. function md5cycle(x, k) {
  17. var a = x[0], b = x[1], c = x[2], d = x[3];
  18. a += (b & c | ~b & d) + k[0] - 680876936 | 0;
  19. a = (a << 7 | a >>> 25) + b | 0;
  20. d += (a & b | ~a & c) + k[1] - 389564586 | 0;
  21. d = (d << 12 | d >>> 20) + a | 0;
  22. c += (d & a | ~d & b) + k[2] + 606105819 | 0;
  23. c = (c << 17 | c >>> 15) + d | 0;
  24. b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
  25. b = (b << 22 | b >>> 10) + c | 0;
  26. a += (b & c | ~b & d) + k[4] - 176418897 | 0;
  27. a = (a << 7 | a >>> 25) + b | 0;
  28. d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
  29. d = (d << 12 | d >>> 20) + a | 0;
  30. c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
  31. c = (c << 17 | c >>> 15) + d | 0;
  32. b += (c & d | ~c & a) + k[7] - 45705983 | 0;
  33. b = (b << 22 | b >>> 10) + c | 0;
  34. a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
  35. a = (a << 7 | a >>> 25) + b | 0;
  36. d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
  37. d = (d << 12 | d >>> 20) + a | 0;
  38. c += (d & a | ~d & b) + k[10] - 42063 | 0;
  39. c = (c << 17 | c >>> 15) + d | 0;
  40. b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
  41. b = (b << 22 | b >>> 10) + c | 0;
  42. a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
  43. a = (a << 7 | a >>> 25) + b | 0;
  44. d += (a & b | ~a & c) + k[13] - 40341101 | 0;
  45. d = (d << 12 | d >>> 20) + a | 0;
  46. c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
  47. c = (c << 17 | c >>> 15) + d | 0;
  48. b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
  49. b = (b << 22 | b >>> 10) + c | 0;
  50. a += (b & d | c & ~d) + k[1] - 165796510 | 0;
  51. a = (a << 5 | a >>> 27) + b | 0;
  52. d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
  53. d = (d << 9 | d >>> 23) + a | 0;
  54. c += (d & b | a & ~b) + k[11] + 643717713 | 0;
  55. c = (c << 14 | c >>> 18) + d | 0;
  56. b += (c & a | d & ~a) + k[0] - 373897302 | 0;
  57. b = (b << 20 | b >>> 12) + c | 0;
  58. a += (b & d | c & ~d) + k[5] - 701558691 | 0;
  59. a = (a << 5 | a >>> 27) + b | 0;
  60. d += (a & c | b & ~c) + k[10] + 38016083 | 0;
  61. d = (d << 9 | d >>> 23) + a | 0;
  62. c += (d & b | a & ~b) + k[15] - 660478335 | 0;
  63. c = (c << 14 | c >>> 18) + d | 0;
  64. b += (c & a | d & ~a) + k[4] - 405537848 | 0;
  65. b = (b << 20 | b >>> 12) + c | 0;
  66. a += (b & d | c & ~d) + k[9] + 568446438 | 0;
  67. a = (a << 5 | a >>> 27) + b | 0;
  68. d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
  69. d = (d << 9 | d >>> 23) + a | 0;
  70. c += (d & b | a & ~b) + k[3] - 187363961 | 0;
  71. c = (c << 14 | c >>> 18) + d | 0;
  72. b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
  73. b = (b << 20 | b >>> 12) + c | 0;
  74. a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
  75. a = (a << 5 | a >>> 27) + b | 0;
  76. d += (a & c | b & ~c) + k[2] - 51403784 | 0;
  77. d = (d << 9 | d >>> 23) + a | 0;
  78. c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
  79. c = (c << 14 | c >>> 18) + d | 0;
  80. b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
  81. b = (b << 20 | b >>> 12) + c | 0;
  82. a += (b ^ c ^ d) + k[5] - 378558 | 0;
  83. a = (a << 4 | a >>> 28) + b | 0;
  84. d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
  85. d = (d << 11 | d >>> 21) + a | 0;
  86. c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
  87. c = (c << 16 | c >>> 16) + d | 0;
  88. b += (c ^ d ^ a) + k[14] - 35309556 | 0;
  89. b = (b << 23 | b >>> 9) + c | 0;
  90. a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
  91. a = (a << 4 | a >>> 28) + b | 0;
  92. d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
  93. d = (d << 11 | d >>> 21) + a | 0;
  94. c += (d ^ a ^ b) + k[7] - 155497632 | 0;
  95. c = (c << 16 | c >>> 16) + d | 0;
  96. b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
  97. b = (b << 23 | b >>> 9) + c | 0;
  98. a += (b ^ c ^ d) + k[13] + 681279174 | 0;
  99. a = (a << 4 | a >>> 28) + b | 0;
  100. d += (a ^ b ^ c) + k[0] - 358537222 | 0;
  101. d = (d << 11 | d >>> 21) + a | 0;
  102. c += (d ^ a ^ b) + k[3] - 722521979 | 0;
  103. c = (c << 16 | c >>> 16) + d | 0;
  104. b += (c ^ d ^ a) + k[6] + 76029189 | 0;
  105. b = (b << 23 | b >>> 9) + c | 0;
  106. a += (b ^ c ^ d) + k[9] - 640364487 | 0;
  107. a = (a << 4 | a >>> 28) + b | 0;
  108. d += (a ^ b ^ c) + k[12] - 421815835 | 0;
  109. d = (d << 11 | d >>> 21) + a | 0;
  110. c += (d ^ a ^ b) + k[15] + 530742520 | 0;
  111. c = (c << 16 | c >>> 16) + d | 0;
  112. b += (c ^ d ^ a) + k[2] - 995338651 | 0;
  113. b = (b << 23 | b >>> 9) + c | 0;
  114. a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
  115. a = (a << 6 | a >>> 26) + b | 0;
  116. d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
  117. d = (d << 10 | d >>> 22) + a | 0;
  118. c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
  119. c = (c << 15 | c >>> 17) + d | 0;
  120. b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
  121. b = (b << 21 | b >>> 11) + c | 0;
  122. a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
  123. a = (a << 6 | a >>> 26) + b | 0;
  124. d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
  125. d = (d << 10 | d >>> 22) + a | 0;
  126. c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
  127. c = (c << 15 | c >>> 17) + d | 0;
  128. b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
  129. b = (b << 21 | b >>> 11) + c | 0;
  130. a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
  131. a = (a << 6 | a >>> 26) + b | 0;
  132. d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
  133. d = (d << 10 | d >>> 22) + a | 0;
  134. c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
  135. c = (c << 15 | c >>> 17) + d | 0;
  136. b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
  137. b = (b << 21 | b >>> 11) + c | 0;
  138. a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
  139. a = (a << 6 | a >>> 26) + b | 0;
  140. d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
  141. d = (d << 10 | d >>> 22) + a | 0;
  142. c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
  143. c = (c << 15 | c >>> 17) + d | 0;
  144. b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
  145. b = (b << 21 | b >>> 11) + c | 0;
  146. x[0] = a + x[0] | 0;
  147. x[1] = b + x[1] | 0;
  148. x[2] = c + x[2] | 0;
  149. x[3] = d + x[3] | 0;
  150. }
  151. function md5blk(s) {
  152. var md5blks = [], i;
  153. for (i = 0; i < 64; i += 4) {
  154. md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
  155. }
  156. return md5blks;
  157. }
  158. function md5blk_array(a) {
  159. var md5blks = [], i;
  160. for (i = 0; i < 64; i += 4) {
  161. md5blks[i >> 2] = a[i] + (a[i + 1] << 8) + (a[i + 2] << 16) + (a[i + 3] << 24);
  162. }
  163. return md5blks;
  164. }
  165. function md51(s) {
  166. var n = s.length, state = [ 1732584193, -271733879, -1732584194, 271733878 ], i, length, tail, tmp, lo, hi;
  167. for (i = 64; i <= n; i += 64) {
  168. md5cycle(state, md5blk(s.substring(i - 64, i)));
  169. }
  170. s = s.substring(i - 64);
  171. length = s.length;
  172. tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  173. for (i = 0; i < length; i += 1) {
  174. tail[i >> 2] |= s.charCodeAt(i) << (i % 4 << 3);
  175. }
  176. tail[i >> 2] |= 128 << (i % 4 << 3);
  177. if (i > 55) {
  178. md5cycle(state, tail);
  179. for (i = 0; i < 16; i += 1) {
  180. tail[i] = 0;
  181. }
  182. }
  183. tmp = n * 8;
  184. tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
  185. lo = parseInt(tmp[2], 16);
  186. hi = parseInt(tmp[1], 16) || 0;
  187. tail[14] = lo;
  188. tail[15] = hi;
  189. md5cycle(state, tail);
  190. return state;
  191. }
  192. function md51_array(a) {
  193. var n = a.length, state = [ 1732584193, -271733879, -1732584194, 271733878 ], i, length, tail, tmp, lo, hi;
  194. for (i = 64; i <= n; i += 64) {
  195. md5cycle(state, md5blk_array(a.subarray(i - 64, i)));
  196. }
  197. a = i - 64 < n ? a.subarray(i - 64) : new Uint8Array(0);
  198. length = a.length;
  199. tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  200. for (i = 0; i < length; i += 1) {
  201. tail[i >> 2] |= a[i] << (i % 4 << 3);
  202. }
  203. tail[i >> 2] |= 128 << (i % 4 << 3);
  204. if (i > 55) {
  205. md5cycle(state, tail);
  206. for (i = 0; i < 16; i += 1) {
  207. tail[i] = 0;
  208. }
  209. }
  210. tmp = n * 8;
  211. tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
  212. lo = parseInt(tmp[2], 16);
  213. hi = parseInt(tmp[1], 16) || 0;
  214. tail[14] = lo;
  215. tail[15] = hi;
  216. md5cycle(state, tail);
  217. return state;
  218. }
  219. function rhex(n) {
  220. var s = "", j;
  221. for (j = 0; j < 4; j += 1) {
  222. s += hex_chr[n >> j * 8 + 4 & 15] + hex_chr[n >> j * 8 & 15];
  223. }
  224. return s;
  225. }
  226. function hex(x) {
  227. var i;
  228. for (i = 0; i < x.length; i += 1) {
  229. x[i] = rhex(x[i]);
  230. }
  231. return x.join("");
  232. }
  233. if (hex(md51("hello")) !== "5d41402abc4b2a76b9719d911017c592") ;
  234. if (typeof ArrayBuffer !== "undefined" && !ArrayBuffer.prototype.slice) {
  235. (function() {
  236. function clamp(val, length) {
  237. val = val | 0 || 0;
  238. if (val < 0) {
  239. return Math.max(val + length, 0);
  240. }
  241. return Math.min(val, length);
  242. }
  243. ArrayBuffer.prototype.slice = function(from, to) {
  244. var length = this.byteLength, begin = clamp(from, length), end = length, num, target, targetArray, sourceArray;
  245. if (to !== undefined$1) {
  246. end = clamp(to, length);
  247. }
  248. if (begin > end) {
  249. return new ArrayBuffer(0);
  250. }
  251. num = end - begin;
  252. target = new ArrayBuffer(num);
  253. targetArray = new Uint8Array(target);
  254. sourceArray = new Uint8Array(this, begin, num);
  255. targetArray.set(sourceArray);
  256. return target;
  257. };
  258. })();
  259. }
  260. function toUtf8(str) {
  261. if (/[\u0080-\uFFFF]/.test(str)) {
  262. str = unescape(encodeURIComponent(str));
  263. }
  264. return str;
  265. }
  266. function utf8Str2ArrayBuffer(str, returnUInt8Array) {
  267. var length = str.length, buff = new ArrayBuffer(length), arr = new Uint8Array(buff), i;
  268. for (i = 0; i < length; i += 1) {
  269. arr[i] = str.charCodeAt(i);
  270. }
  271. return returnUInt8Array ? arr : buff;
  272. }
  273. function arrayBuffer2Utf8Str(buff) {
  274. return String.fromCharCode.apply(null, new Uint8Array(buff));
  275. }
  276. function concatenateArrayBuffers(first, second, returnUInt8Array) {
  277. var result = new Uint8Array(first.byteLength + second.byteLength);
  278. result.set(new Uint8Array(first));
  279. result.set(new Uint8Array(second), first.byteLength);
  280. return returnUInt8Array ? result : result.buffer;
  281. }
  282. function hexToBinaryString(hex) {
  283. var bytes = [], length = hex.length, x;
  284. for (x = 0; x < length - 1; x += 2) {
  285. bytes.push(parseInt(hex.substr(x, 2), 16));
  286. }
  287. return String.fromCharCode.apply(String, bytes);
  288. }
  289. function SparkMD5() {
  290. this.reset();
  291. }
  292. SparkMD5.prototype.append = function(str) {
  293. this.appendBinary(toUtf8(str));
  294. return this;
  295. };
  296. SparkMD5.prototype.appendBinary = function(contents) {
  297. this._buff += contents;
  298. this._length += contents.length;
  299. var length = this._buff.length, i;
  300. for (i = 64; i <= length; i += 64) {
  301. md5cycle(this._hash, md5blk(this._buff.substring(i - 64, i)));
  302. }
  303. this._buff = this._buff.substring(i - 64);
  304. return this;
  305. };
  306. SparkMD5.prototype.end = function(raw) {
  307. var buff = this._buff, length = buff.length, i, tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], ret;
  308. for (i = 0; i < length; i += 1) {
  309. tail[i >> 2] |= buff.charCodeAt(i) << (i % 4 << 3);
  310. }
  311. this._finish(tail, length);
  312. ret = hex(this._hash);
  313. if (raw) {
  314. ret = hexToBinaryString(ret);
  315. }
  316. this.reset();
  317. return ret;
  318. };
  319. SparkMD5.prototype.reset = function() {
  320. this._buff = "";
  321. this._length = 0;
  322. this._hash = [ 1732584193, -271733879, -1732584194, 271733878 ];
  323. return this;
  324. };
  325. SparkMD5.prototype.getState = function() {
  326. return {
  327. buff: this._buff,
  328. length: this._length,
  329. hash: this._hash.slice()
  330. };
  331. };
  332. SparkMD5.prototype.setState = function(state) {
  333. this._buff = state.buff;
  334. this._length = state.length;
  335. this._hash = state.hash;
  336. return this;
  337. };
  338. SparkMD5.prototype.destroy = function() {
  339. delete this._hash;
  340. delete this._buff;
  341. delete this._length;
  342. };
  343. SparkMD5.prototype._finish = function(tail, length) {
  344. var i = length, tmp, lo, hi;
  345. tail[i >> 2] |= 128 << (i % 4 << 3);
  346. if (i > 55) {
  347. md5cycle(this._hash, tail);
  348. for (i = 0; i < 16; i += 1) {
  349. tail[i] = 0;
  350. }
  351. }
  352. tmp = this._length * 8;
  353. tmp = tmp.toString(16).match(/(.*?)(.{0,8})$/);
  354. lo = parseInt(tmp[2], 16);
  355. hi = parseInt(tmp[1], 16) || 0;
  356. tail[14] = lo;
  357. tail[15] = hi;
  358. md5cycle(this._hash, tail);
  359. };
  360. SparkMD5.hash = function(str, raw) {
  361. return SparkMD5.hashBinary(toUtf8(str), raw);
  362. };
  363. SparkMD5.hashBinary = function(content, raw) {
  364. var hash = md51(content), ret = hex(hash);
  365. return raw ? hexToBinaryString(ret) : ret;
  366. };
  367. SparkMD5.ArrayBuffer = function() {
  368. this.reset();
  369. };
  370. SparkMD5.ArrayBuffer.prototype.append = function(arr) {
  371. var buff = concatenateArrayBuffers(this._buff.buffer, arr, true), length = buff.length, i;
  372. this._length += arr.byteLength;
  373. for (i = 64; i <= length; i += 64) {
  374. md5cycle(this._hash, md5blk_array(buff.subarray(i - 64, i)));
  375. }
  376. this._buff = i - 64 < length ? new Uint8Array(buff.buffer.slice(i - 64)) : new Uint8Array(0);
  377. return this;
  378. };
  379. SparkMD5.ArrayBuffer.prototype.end = function(raw) {
  380. var buff = this._buff, length = buff.length, tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], i, ret;
  381. for (i = 0; i < length; i += 1) {
  382. tail[i >> 2] |= buff[i] << (i % 4 << 3);
  383. }
  384. this._finish(tail, length);
  385. ret = hex(this._hash);
  386. if (raw) {
  387. ret = hexToBinaryString(ret);
  388. }
  389. this.reset();
  390. return ret;
  391. };
  392. SparkMD5.ArrayBuffer.prototype.reset = function() {
  393. this._buff = new Uint8Array(0);
  394. this._length = 0;
  395. this._hash = [ 1732584193, -271733879, -1732584194, 271733878 ];
  396. return this;
  397. };
  398. SparkMD5.ArrayBuffer.prototype.getState = function() {
  399. var state = SparkMD5.prototype.getState.call(this);
  400. state.buff = arrayBuffer2Utf8Str(state.buff);
  401. return state;
  402. };
  403. SparkMD5.ArrayBuffer.prototype.setState = function(state) {
  404. state.buff = utf8Str2ArrayBuffer(state.buff, true);
  405. return SparkMD5.prototype.setState.call(this, state);
  406. };
  407. SparkMD5.ArrayBuffer.prototype.destroy = SparkMD5.prototype.destroy;
  408. SparkMD5.ArrayBuffer.prototype._finish = SparkMD5.prototype._finish;
  409. SparkMD5.ArrayBuffer.hash = function(arr, raw) {
  410. var hash = md51_array(new Uint8Array(arr)), ret = hex(hash);
  411. return raw ? hexToBinaryString(ret) : ret;
  412. };
  413. return SparkMD5;
  414. }));
  415. })(sparkMd5);
  416. var SparkMD5 = sparkMd5.exports;
  417. const fileSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
  418. class FileChecksum {
  419. static create(file, callback) {
  420. const instance = new FileChecksum(file);
  421. instance.create(callback);
  422. }
  423. constructor(file) {
  424. this.file = file;
  425. this.chunkSize = 2097152;
  426. this.chunkCount = Math.ceil(this.file.size / this.chunkSize);
  427. this.chunkIndex = 0;
  428. }
  429. create(callback) {
  430. this.callback = callback;
  431. this.md5Buffer = new SparkMD5.ArrayBuffer;
  432. this.fileReader = new FileReader;
  433. this.fileReader.addEventListener("load", (event => this.fileReaderDidLoad(event)));
  434. this.fileReader.addEventListener("error", (event => this.fileReaderDidError(event)));
  435. this.readNextChunk();
  436. }
  437. fileReaderDidLoad(event) {
  438. this.md5Buffer.append(event.target.result);
  439. if (!this.readNextChunk()) {
  440. const binaryDigest = this.md5Buffer.end(true);
  441. const base64digest = btoa(binaryDigest);
  442. this.callback(null, base64digest);
  443. }
  444. }
  445. fileReaderDidError(event) {
  446. this.callback(`Error reading ${this.file.name}`);
  447. }
  448. readNextChunk() {
  449. if (this.chunkIndex < this.chunkCount || this.chunkIndex == 0 && this.chunkCount == 0) {
  450. const start = this.chunkIndex * this.chunkSize;
  451. const end = Math.min(start + this.chunkSize, this.file.size);
  452. const bytes = fileSlice.call(this.file, start, end);
  453. this.fileReader.readAsArrayBuffer(bytes);
  454. this.chunkIndex++;
  455. return true;
  456. } else {
  457. return false;
  458. }
  459. }
  460. }
  461. function getMetaValue(name) {
  462. const element = findElement(document.head, `meta[name="${name}"]`);
  463. if (element) {
  464. return element.getAttribute("content");
  465. }
  466. }
  467. function findElements(root, selector) {
  468. if (typeof root == "string") {
  469. selector = root;
  470. root = document;
  471. }
  472. const elements = root.querySelectorAll(selector);
  473. return toArray(elements);
  474. }
  475. function findElement(root, selector) {
  476. if (typeof root == "string") {
  477. selector = root;
  478. root = document;
  479. }
  480. return root.querySelector(selector);
  481. }
  482. function dispatchEvent(element, type, eventInit = {}) {
  483. const {disabled: disabled} = element;
  484. const {bubbles: bubbles, cancelable: cancelable, detail: detail} = eventInit;
  485. const event = document.createEvent("Event");
  486. event.initEvent(type, bubbles || true, cancelable || true);
  487. event.detail = detail || {};
  488. try {
  489. element.disabled = false;
  490. element.dispatchEvent(event);
  491. } finally {
  492. element.disabled = disabled;
  493. }
  494. return event;
  495. }
  496. function toArray(value) {
  497. if (Array.isArray(value)) {
  498. return value;
  499. } else if (Array.from) {
  500. return Array.from(value);
  501. } else {
  502. return [].slice.call(value);
  503. }
  504. }
  505. class BlobRecord {
  506. constructor(file, checksum, url, customHeaders = {}) {
  507. this.file = file;
  508. this.attributes = {
  509. filename: file.name,
  510. content_type: file.type || "application/octet-stream",
  511. byte_size: file.size,
  512. checksum: checksum
  513. };
  514. this.xhr = new XMLHttpRequest;
  515. this.xhr.open("POST", url, true);
  516. this.xhr.responseType = "json";
  517. this.xhr.setRequestHeader("Content-Type", "application/json");
  518. this.xhr.setRequestHeader("Accept", "application/json");
  519. this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  520. Object.keys(customHeaders).forEach((headerKey => {
  521. this.xhr.setRequestHeader(headerKey, customHeaders[headerKey]);
  522. }));
  523. const csrfToken = getMetaValue("csrf-token");
  524. if (csrfToken != undefined) {
  525. this.xhr.setRequestHeader("X-CSRF-Token", csrfToken);
  526. }
  527. this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
  528. this.xhr.addEventListener("error", (event => this.requestDidError(event)));
  529. }
  530. get status() {
  531. return this.xhr.status;
  532. }
  533. get response() {
  534. const {responseType: responseType, response: response} = this.xhr;
  535. if (responseType == "json") {
  536. return response;
  537. } else {
  538. return JSON.parse(response);
  539. }
  540. }
  541. create(callback) {
  542. this.callback = callback;
  543. this.xhr.send(JSON.stringify({
  544. blob: this.attributes
  545. }));
  546. }
  547. requestDidLoad(event) {
  548. if (this.status >= 200 && this.status < 300) {
  549. const {response: response} = this;
  550. const {direct_upload: direct_upload} = response;
  551. delete response.direct_upload;
  552. this.attributes = response;
  553. this.directUploadData = direct_upload;
  554. this.callback(null, this.toJSON());
  555. } else {
  556. this.requestDidError(event);
  557. }
  558. }
  559. requestDidError(event) {
  560. this.callback(`Error creating Blob for "${this.file.name}". Status: ${this.status}`);
  561. }
  562. toJSON() {
  563. const result = {};
  564. for (const key in this.attributes) {
  565. result[key] = this.attributes[key];
  566. }
  567. return result;
  568. }
  569. }
  570. class BlobUpload {
  571. constructor(blob) {
  572. this.blob = blob;
  573. this.file = blob.file;
  574. const {url: url, headers: headers} = blob.directUploadData;
  575. this.xhr = new XMLHttpRequest;
  576. this.xhr.open("PUT", url, true);
  577. this.xhr.responseType = "text";
  578. for (const key in headers) {
  579. this.xhr.setRequestHeader(key, headers[key]);
  580. }
  581. this.xhr.addEventListener("load", (event => this.requestDidLoad(event)));
  582. this.xhr.addEventListener("error", (event => this.requestDidError(event)));
  583. }
  584. create(callback) {
  585. this.callback = callback;
  586. this.xhr.send(this.file.slice());
  587. }
  588. requestDidLoad(event) {
  589. const {status: status, response: response} = this.xhr;
  590. if (status >= 200 && status < 300) {
  591. this.callback(null, response);
  592. } else {
  593. this.requestDidError(event);
  594. }
  595. }
  596. requestDidError(event) {
  597. this.callback(`Error storing "${this.file.name}". Status: ${this.xhr.status}`);
  598. }
  599. }
  600. let id = 0;
  601. class DirectUpload {
  602. constructor(file, url, delegate, customHeaders = {}) {
  603. this.id = ++id;
  604. this.file = file;
  605. this.url = url;
  606. this.delegate = delegate;
  607. this.customHeaders = customHeaders;
  608. }
  609. create(callback) {
  610. FileChecksum.create(this.file, ((error, checksum) => {
  611. if (error) {
  612. callback(error);
  613. return;
  614. }
  615. const blob = new BlobRecord(this.file, checksum, this.url, this.customHeaders);
  616. notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr);
  617. blob.create((error => {
  618. if (error) {
  619. callback(error);
  620. } else {
  621. const upload = new BlobUpload(blob);
  622. notify(this.delegate, "directUploadWillStoreFileWithXHR", upload.xhr);
  623. upload.create((error => {
  624. if (error) {
  625. callback(error);
  626. } else {
  627. callback(null, blob.toJSON());
  628. }
  629. }));
  630. }
  631. }));
  632. }));
  633. }
  634. }
  635. function notify(object, methodName, ...messages) {
  636. if (object && typeof object[methodName] == "function") {
  637. return object[methodName](...messages);
  638. }
  639. }
  640. class DirectUploadController {
  641. constructor(input, file) {
  642. this.input = input;
  643. this.file = file;
  644. this.directUpload = new DirectUpload(this.file, this.url, this);
  645. this.dispatch("initialize");
  646. }
  647. start(callback) {
  648. const hiddenInput = document.createElement("input");
  649. hiddenInput.type = "hidden";
  650. hiddenInput.name = this.input.name;
  651. this.input.insertAdjacentElement("beforebegin", hiddenInput);
  652. this.dispatch("start");
  653. this.directUpload.create(((error, attributes) => {
  654. if (error) {
  655. hiddenInput.parentNode.removeChild(hiddenInput);
  656. this.dispatchError(error);
  657. } else {
  658. hiddenInput.value = attributes.signed_id;
  659. }
  660. this.dispatch("end");
  661. callback(error);
  662. }));
  663. }
  664. uploadRequestDidProgress(event) {
  665. const progress = event.loaded / event.total * 100;
  666. if (progress) {
  667. this.dispatch("progress", {
  668. progress: progress
  669. });
  670. }
  671. }
  672. get url() {
  673. return this.input.getAttribute("data-direct-upload-url");
  674. }
  675. dispatch(name, detail = {}) {
  676. detail.file = this.file;
  677. detail.id = this.directUpload.id;
  678. return dispatchEvent(this.input, `direct-upload:${name}`, {
  679. detail: detail
  680. });
  681. }
  682. dispatchError(error) {
  683. const event = this.dispatch("error", {
  684. error: error
  685. });
  686. if (!event.defaultPrevented) {
  687. alert(error);
  688. }
  689. }
  690. directUploadWillCreateBlobWithXHR(xhr) {
  691. this.dispatch("before-blob-request", {
  692. xhr: xhr
  693. });
  694. }
  695. directUploadWillStoreFileWithXHR(xhr) {
  696. this.dispatch("before-storage-request", {
  697. xhr: xhr
  698. });
  699. xhr.upload.addEventListener("progress", (event => this.uploadRequestDidProgress(event)));
  700. }
  701. }
  702. const inputSelector = "input[type=file][data-direct-upload-url]:not([disabled])";
  703. class DirectUploadsController {
  704. constructor(form) {
  705. this.form = form;
  706. this.inputs = findElements(form, inputSelector).filter((input => input.files.length));
  707. }
  708. start(callback) {
  709. const controllers = this.createDirectUploadControllers();
  710. const startNextController = () => {
  711. const controller = controllers.shift();
  712. if (controller) {
  713. controller.start((error => {
  714. if (error) {
  715. callback(error);
  716. this.dispatch("end");
  717. } else {
  718. startNextController();
  719. }
  720. }));
  721. } else {
  722. callback();
  723. this.dispatch("end");
  724. }
  725. };
  726. this.dispatch("start");
  727. startNextController();
  728. }
  729. createDirectUploadControllers() {
  730. const controllers = [];
  731. this.inputs.forEach((input => {
  732. toArray(input.files).forEach((file => {
  733. const controller = new DirectUploadController(input, file);
  734. controllers.push(controller);
  735. }));
  736. }));
  737. return controllers;
  738. }
  739. dispatch(name, detail = {}) {
  740. return dispatchEvent(this.form, `direct-uploads:${name}`, {
  741. detail: detail
  742. });
  743. }
  744. }
  745. const processingAttribute = "data-direct-uploads-processing";
  746. const submitButtonsByForm = new WeakMap;
  747. let started = false;
  748. function start() {
  749. if (!started) {
  750. started = true;
  751. document.addEventListener("click", didClick, true);
  752. document.addEventListener("submit", didSubmitForm, true);
  753. document.addEventListener("ajax:before", didSubmitRemoteElement);
  754. }
  755. }
  756. function didClick(event) {
  757. const button = event.target.closest("button, input");
  758. if (button && button.type === "submit" && button.form) {
  759. submitButtonsByForm.set(button.form, button);
  760. }
  761. }
  762. function didSubmitForm(event) {
  763. handleFormSubmissionEvent(event);
  764. }
  765. function didSubmitRemoteElement(event) {
  766. if (event.target.tagName == "FORM") {
  767. handleFormSubmissionEvent(event);
  768. }
  769. }
  770. function handleFormSubmissionEvent(event) {
  771. const form = event.target;
  772. if (form.hasAttribute(processingAttribute)) {
  773. event.preventDefault();
  774. return;
  775. }
  776. const controller = new DirectUploadsController(form);
  777. const {inputs: inputs} = controller;
  778. if (inputs.length) {
  779. event.preventDefault();
  780. form.setAttribute(processingAttribute, "");
  781. inputs.forEach(disable);
  782. controller.start((error => {
  783. form.removeAttribute(processingAttribute);
  784. if (error) {
  785. inputs.forEach(enable);
  786. } else {
  787. submitForm(form);
  788. }
  789. }));
  790. }
  791. }
  792. function submitForm(form) {
  793. let button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit], button[type=submit]");
  794. if (button) {
  795. const {disabled: disabled} = button;
  796. button.disabled = false;
  797. button.focus();
  798. button.click();
  799. button.disabled = disabled;
  800. } else {
  801. button = document.createElement("input");
  802. button.type = "submit";
  803. button.style.display = "none";
  804. form.appendChild(button);
  805. button.click();
  806. form.removeChild(button);
  807. }
  808. submitButtonsByForm.delete(form);
  809. }
  810. function disable(input) {
  811. input.disabled = true;
  812. }
  813. function enable(input) {
  814. input.disabled = false;
  815. }
  816. function autostart() {
  817. if (window.ActiveStorage) {
  818. start();
  819. }
  820. }
  821. setTimeout(autostart, 1);
  822. exports.DirectUpload = DirectUpload;
  823. exports.DirectUploadController = DirectUploadController;
  824. exports.DirectUploadsController = DirectUploadsController;
  825. exports.dispatchEvent = dispatchEvent;
  826. exports.start = start;
  827. Object.defineProperty(exports, "__esModule", {
  828. value: true
  829. });
  830. }));