roboto-js 1.0.4 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,599 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _lodash = _interopRequireDefault(require("lodash"));
8
+ var _events = require("events");
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
11
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
12
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
13
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
14
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
15
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
16
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
18
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
19
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
20
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
21
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
22
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
23
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
24
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
25
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
26
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
27
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
28
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
29
+ // Make sure to use a browser-compatible version if in the browser
30
+ var RbtFile = exports["default"] = /*#__PURE__*/function (_EventEmitter) {
31
+ _inherits(RbtFile, _EventEmitter);
32
+ var _super = _createSuper(RbtFile);
33
+ // 1MB, for example
34
+
35
+ function RbtFile(record, axiosInstance, localDb) {
36
+ var _this;
37
+ _classCallCheck(this, RbtFile);
38
+ _this = _super.call(this); // Call the constructor of EventEmitter
39
+ _this.id = record.id;
40
+ _this._axios = axiosInstance;
41
+ _this._internalData = record;
42
+ _this._data = record.data ? record.data : record.dataJson ? JSON.parse(record.dataJson) : {};
43
+ _this._localDb = localDb;
44
+ _this.progress = 0;
45
+ _this.fileHeader = {};
46
+ return _this;
47
+ }
48
+ _createClass(RbtFile, [{
49
+ key: "get",
50
+ value: function (_get) {
51
+ function get(_x) {
52
+ return _get.apply(this, arguments);
53
+ }
54
+ get.toString = function () {
55
+ return _get.toString();
56
+ };
57
+ return get;
58
+ }(function (path) {
59
+ return get(this._data, path);
60
+ })
61
+ }, {
62
+ key: "set",
63
+ value: function (_set) {
64
+ function set(_x2, _x3) {
65
+ return _set.apply(this, arguments);
66
+ }
67
+ set.toString = function () {
68
+ return _set.toString();
69
+ };
70
+ return set;
71
+ }(function (path, value) {
72
+ set(this._data, path, value);
73
+ })
74
+ }, {
75
+ key: "getData",
76
+ value: function getData() {
77
+ return _objectSpread({}, this._data);
78
+ }
79
+ }, {
80
+ key: "toRecord",
81
+ value: function toRecord() {
82
+ return _objectSpread(_objectSpread({}, this._internalData), {}, {
83
+ dataJson: JSON.stringify(this._data)
84
+ });
85
+ }
86
+ }, {
87
+ key: "save",
88
+ value: function () {
89
+ var _save = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
90
+ var record, response, _e$response;
91
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
92
+ while (1) switch (_context.prev = _context.next) {
93
+ case 0:
94
+ if (this._internalData.type) {
95
+ _context.next = 2;
96
+ break;
97
+ }
98
+ throw new Error('Cannot save object without type');
99
+ case 2:
100
+ _context.prev = 2;
101
+ record = this.toRecord();
102
+ _context.next = 6;
103
+ return this._axios.post('/object_service/saveObject', [record]);
104
+ case 6:
105
+ response = _context.sent;
106
+ if (!(response.data.ok === false)) {
107
+ _context.next = 9;
108
+ break;
109
+ }
110
+ throw new Error(response.data.message);
111
+ case 9:
112
+ this._internalData = response.data;
113
+ return _context.abrupt("return", this);
114
+ case 13:
115
+ _context.prev = 13;
116
+ _context.t0 = _context["catch"](2);
117
+ console.error('RbtFile.save.error:', (_e$response = _context.t0.response) === null || _e$response === void 0 ? void 0 : _e$response.data);
118
+ throw _context.t0;
119
+ case 17:
120
+ case "end":
121
+ return _context.stop();
122
+ }
123
+ }, _callee, this, [[2, 13]]);
124
+ }));
125
+ function save() {
126
+ return _save.apply(this, arguments);
127
+ }
128
+ return save;
129
+ }()
130
+ }, {
131
+ key: "delete",
132
+ value: function () {
133
+ var _delete2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
134
+ var record, response, _e$response2;
135
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
136
+ while (1) switch (_context2.prev = _context2.next) {
137
+ case 0:
138
+ if (this._internalData.type) {
139
+ _context2.next = 2;
140
+ break;
141
+ }
142
+ throw new Error('Cannot delete object without type');
143
+ case 2:
144
+ _context2.prev = 2;
145
+ record = this.toRecord();
146
+ _context2.next = 6;
147
+ return this._axios.post('/object_service/deleteObject', [record]);
148
+ case 6:
149
+ response = _context2.sent;
150
+ if (!(response.data.ok === false)) {
151
+ _context2.next = 9;
152
+ break;
153
+ }
154
+ throw new Error(response.data.message);
155
+ case 9:
156
+ this._internalData = response.data;
157
+ return _context2.abrupt("return", this);
158
+ case 13:
159
+ _context2.prev = 13;
160
+ _context2.t0 = _context2["catch"](2);
161
+ console.error('RbtFile.delete.error:', (_e$response2 = _context2.t0.response) === null || _e$response2 === void 0 ? void 0 : _e$response2.data);
162
+ throw _context2.t0;
163
+ case 17:
164
+ case "end":
165
+ return _context2.stop();
166
+ }
167
+ }, _callee2, this, [[2, 13]]);
168
+ }));
169
+ function _delete() {
170
+ return _delete2.apply(this, arguments);
171
+ }
172
+ return _delete;
173
+ }()
174
+ }, {
175
+ key: "getFileBlobUrl",
176
+ value: function () {
177
+ var _getFileBlobUrl = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
178
+ var db, tx, cursor, fileData, blob;
179
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
180
+ while (1) switch (_context3.prev = _context3.next) {
181
+ case 0:
182
+ db = this._localDb;
183
+ tx = db.transaction('files', 'readonly');
184
+ _context3.next = 4;
185
+ return tx.store.openCursor();
186
+ case 4:
187
+ cursor = _context3.sent;
188
+ if (cursor) {
189
+ _context3.next = 7;
190
+ break;
191
+ }
192
+ return _context3.abrupt("return", null);
193
+ case 7:
194
+ fileData = cursor.value.chunk; // Assuming the first chunk has enough data
195
+ blob = new Blob([fileData], {
196
+ type: 'application/octet-stream'
197
+ });
198
+ return _context3.abrupt("return", URL.createObjectURL(blob));
199
+ case 10:
200
+ case "end":
201
+ return _context3.stop();
202
+ }
203
+ }, _callee3, this);
204
+ }));
205
+ function getFileBlobUrl() {
206
+ return _getFileBlobUrl.apply(this, arguments);
207
+ }
208
+ return getFileBlobUrl;
209
+ }()
210
+ }, {
211
+ key: "setProgress",
212
+ value: function setProgress(newProgress) {
213
+ this.progress = newProgress;
214
+ console.log("Progress: ".concat(this.progress * 100, "%"));
215
+ this.emit('progress', this.progress); // Emit a progress event
216
+ }
217
+ }, {
218
+ key: "readAndStoreFile",
219
+ value: function () {
220
+ var _readAndStoreFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(file) {
221
+ var _this2 = this;
222
+ var offset, _loop;
223
+ return _regeneratorRuntime().wrap(function _callee5$(_context6) {
224
+ while (1) switch (_context6.prev = _context6.next) {
225
+ case 0:
226
+ offset = 0;
227
+ this.fileHeader = {
228
+ id: this.id,
229
+ name: file.name,
230
+ size: file.size,
231
+ ext: this._extractFileExtension(file.name),
232
+ mimeType: file.type,
233
+ numFrames: Math.ceil(file.size / RbtFile.FRAME_SIZE)
234
+ };
235
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
236
+ var chunk;
237
+ return _regeneratorRuntime().wrap(function _loop$(_context5) {
238
+ while (1) switch (_context5.prev = _context5.next) {
239
+ case 0:
240
+ chunk = file.slice(offset, offset + RbtFile.FRAME_SIZE);
241
+ _context5.next = 3;
242
+ return new Promise(function (resolve, reject) {
243
+ var reader = new FileReader();
244
+ reader.onload = /*#__PURE__*/function () {
245
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(e) {
246
+ var arrayBuffer, frameIndex;
247
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
248
+ while (1) switch (_context4.prev = _context4.next) {
249
+ case 0:
250
+ arrayBuffer = e.target.result;
251
+ frameIndex = offset / RbtFile.FRAME_SIZE;
252
+ _context4.next = 4;
253
+ return _this2._storeChunkInIDB(arrayBuffer, frameIndex, _this2.fileHeader);
254
+ case 4:
255
+ _this2.setProgress((frameIndex + 1) / _this2.fileHeader.numFrames);
256
+ resolve();
257
+ case 6:
258
+ case "end":
259
+ return _context4.stop();
260
+ }
261
+ }, _callee4);
262
+ }));
263
+ return function (_x5) {
264
+ return _ref.apply(this, arguments);
265
+ };
266
+ }();
267
+ reader.onerror = function () {
268
+ return reject(reader.error);
269
+ };
270
+ reader.readAsArrayBuffer(chunk);
271
+ });
272
+ case 3:
273
+ offset += RbtFile.FRAME_SIZE;
274
+ case 4:
275
+ case "end":
276
+ return _context5.stop();
277
+ }
278
+ }, _loop);
279
+ });
280
+ case 3:
281
+ if (!(offset < file.size)) {
282
+ _context6.next = 7;
283
+ break;
284
+ }
285
+ return _context6.delegateYield(_loop(), "t0", 5);
286
+ case 5:
287
+ _context6.next = 3;
288
+ break;
289
+ case 7:
290
+ case "end":
291
+ return _context6.stop();
292
+ }
293
+ }, _callee5, this);
294
+ }));
295
+ function readAndStoreFile(_x4) {
296
+ return _readAndStoreFile.apply(this, arguments);
297
+ }
298
+ return readAndStoreFile;
299
+ }()
300
+ }, {
301
+ key: "_extractFileExtension",
302
+ value: function _extractFileExtension(fileName) {
303
+ // Find the last dot in the filename
304
+ var lastDotIndex = fileName.lastIndexOf('.');
305
+
306
+ // No dot found, or the dot is the first character (hidden files)
307
+ if (lastDotIndex === -1 || lastDotIndex === 0) return '';
308
+
309
+ // Extract the extension
310
+ return fileName.substring(lastDotIndex + 1);
311
+ }
312
+ }, {
313
+ key: "_storeChunkInIDB",
314
+ value: function () {
315
+ var _storeChunkInIDB2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(chunk, frameIndex, fileHeader) {
316
+ var db, tx, frameKey;
317
+ return _regeneratorRuntime().wrap(function _callee6$(_context7) {
318
+ while (1) switch (_context7.prev = _context7.next) {
319
+ case 0:
320
+ db = this._localDb;
321
+ if (db) {
322
+ _context7.next = 4;
323
+ break;
324
+ }
325
+ console.error('Database not initialized');
326
+ return _context7.abrupt("return");
327
+ case 4:
328
+ tx = db.transaction('files', 'readwrite');
329
+ frameKey = "".concat(this.id, "_").concat(frameIndex); // Unique key combining id and frameIndex
330
+ _context7.next = 8;
331
+ return tx.store.put({
332
+ key: frameKey,
333
+ chunk: chunk,
334
+ fileHeader: fileHeader
335
+ }, frameKey);
336
+ case 8:
337
+ _context7.next = 10;
338
+ return tx.done;
339
+ case 10:
340
+ case "end":
341
+ return _context7.stop();
342
+ }
343
+ }, _callee6, this);
344
+ }));
345
+ function _storeChunkInIDB(_x6, _x7, _x8) {
346
+ return _storeChunkInIDB2.apply(this, arguments);
347
+ }
348
+ return _storeChunkInIDB;
349
+ }()
350
+ }, {
351
+ key: "_readFrameFromIDB",
352
+ value: function () {
353
+ var _readFrameFromIDB2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(frameIndex) {
354
+ var db, frameKey, tx, frame;
355
+ return _regeneratorRuntime().wrap(function _callee7$(_context8) {
356
+ while (1) switch (_context8.prev = _context8.next) {
357
+ case 0:
358
+ db = this._localDb;
359
+ if (db) {
360
+ _context8.next = 4;
361
+ break;
362
+ }
363
+ console.error('Database not initialized');
364
+ return _context8.abrupt("return", null);
365
+ case 4:
366
+ frameKey = "".concat(this.id, "_").concat(frameIndex); // Same key as used in _storeChunkInIDB
367
+ tx = db.transaction('files', 'readonly');
368
+ _context8.next = 8;
369
+ return tx.store.get(frameKey);
370
+ case 8:
371
+ frame = _context8.sent;
372
+ return _context8.abrupt("return", frame ? frame.chunk : null);
373
+ case 10:
374
+ case "end":
375
+ return _context8.stop();
376
+ }
377
+ }, _callee7, this);
378
+ }));
379
+ function _readFrameFromIDB(_x9) {
380
+ return _readFrameFromIDB2.apply(this, arguments);
381
+ }
382
+ return _readFrameFromIDB;
383
+ }()
384
+ }, {
385
+ key: "uploadFile",
386
+ value: function () {
387
+ var _uploadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
388
+ var frameRes, frameIndex, frameData, statusRes;
389
+ return _regeneratorRuntime().wrap(function _callee8$(_context9) {
390
+ while (1) switch (_context9.prev = _context9.next) {
391
+ case 0:
392
+ if (!(!this.fileHeader || !this.fileHeader.numFrames)) {
393
+ _context9.next = 2;
394
+ break;
395
+ }
396
+ throw new Error("File not ready for upload.");
397
+ case 2:
398
+ this.fileRecord = this.toRecord();
399
+ frameIndex = 0;
400
+ case 4:
401
+ if (!(frameIndex < this.fileHeader.numFrames)) {
402
+ _context9.next = 24;
403
+ break;
404
+ }
405
+ _context9.prev = 5;
406
+ _context9.next = 8;
407
+ return this._readFrameFromIDB(frameIndex);
408
+ case 8:
409
+ frameData = _context9.sent;
410
+ if (frameData) {
411
+ _context9.next = 11;
412
+ break;
413
+ }
414
+ throw new Error("Failed to read frame ".concat(frameIndex));
415
+ case 11:
416
+ _context9.next = 13;
417
+ return this._uploadFrameToServer(frameData, frameIndex, this.fileHeader, this.fileRecord);
418
+ case 13:
419
+ frameRes = _context9.sent;
420
+ // Update progress after each frame is uploaded
421
+ this.setProgress((frameIndex + 1) / this.fileHeader.numFrames);
422
+ _context9.next = 21;
423
+ break;
424
+ case 17:
425
+ _context9.prev = 17;
426
+ _context9.t0 = _context9["catch"](5);
427
+ console.error("Error uploading frame ".concat(frameIndex, ":"), _context9.t0);
428
+ throw _context9.t0;
429
+ case 21:
430
+ frameIndex++;
431
+ _context9.next = 4;
432
+ break;
433
+ case 24:
434
+ _context9.prev = 24;
435
+ _context9.next = 27;
436
+ return this._checkUploadStatusUntilDone(this.fileRecord);
437
+ case 27:
438
+ statusRes = _context9.sent;
439
+ this.set('remoteSrc', statusRes.remoteSrc);
440
+ this.set('progress', 1);
441
+ _context9.next = 32;
442
+ return this.save();
443
+ case 32:
444
+ _context9.next = 37;
445
+ break;
446
+ case 34:
447
+ _context9.prev = 34;
448
+ _context9.t1 = _context9["catch"](24);
449
+ console.error('Error during upload status check:', _context9.t1);
450
+ // Handle error, possibly update UI or retry logic
451
+ case 37:
452
+ case "end":
453
+ return _context9.stop();
454
+ }
455
+ }, _callee8, this, [[5, 17], [24, 34]]);
456
+ }));
457
+ function uploadFile() {
458
+ return _uploadFile.apply(this, arguments);
459
+ }
460
+ return uploadFile;
461
+ }()
462
+ }, {
463
+ key: "_uploadFrameToServer",
464
+ value: function () {
465
+ var _uploadFrameToServer2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(frameData, frameIndex, fileHeader, fileRecord) {
466
+ var base64Data, payload, response;
467
+ return _regeneratorRuntime().wrap(function _callee9$(_context10) {
468
+ while (1) switch (_context10.prev = _context10.next) {
469
+ case 0:
470
+ base64Data = this.convertToBase64(frameData);
471
+ payload = ["@filekit.ffs_server_receiveAndStore_toS3_flow", {
472
+ "fileObject": fileRecord,
473
+ // using fileRecord from newFile.toRecord()
474
+ "fileHeader": fileHeader,
475
+ "frameIndex": frameIndex,
476
+ "frameData": "rpcBase64:".concat(base64Data),
477
+ "timeout": 86400000
478
+ }];
479
+ console.log(payload);
480
+ _context10.next = 5;
481
+ return this._axios.post('/file_service/ffs_runFlow', payload);
482
+ case 5:
483
+ response = _context10.sent;
484
+ if (!(!response || response.status != 200)) {
485
+ _context10.next = 8;
486
+ break;
487
+ }
488
+ throw new Error('Error uploading frame to server');
489
+ case 8:
490
+ return _context10.abrupt("return", response);
491
+ case 9:
492
+ case "end":
493
+ return _context10.stop();
494
+ }
495
+ }, _callee9, this);
496
+ }));
497
+ function _uploadFrameToServer(_x10, _x11, _x12, _x13) {
498
+ return _uploadFrameToServer2.apply(this, arguments);
499
+ }
500
+ return _uploadFrameToServer;
501
+ }()
502
+ }, {
503
+ key: "_checkUploadStatusUntilDone",
504
+ value: function () {
505
+ var _checkUploadStatusUntilDone2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(fileRecord) {
506
+ var _this3 = this;
507
+ return _regeneratorRuntime().wrap(function _callee11$(_context12) {
508
+ while (1) switch (_context12.prev = _context12.next) {
509
+ case 0:
510
+ return _context12.abrupt("return", new Promise(function (resolve, reject) {
511
+ var intervalId = setInterval( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10() {
512
+ var statusRes;
513
+ return _regeneratorRuntime().wrap(function _callee10$(_context11) {
514
+ while (1) switch (_context11.prev = _context11.next) {
515
+ case 0:
516
+ _context11.prev = 0;
517
+ _context11.next = 3;
518
+ return _this3._uploadGetStatusUpdate(fileRecord);
519
+ case 3:
520
+ statusRes = _context11.sent;
521
+ if (statusRes.data && statusRes.data.fileHeader && statusRes.data.fileHeader.status === 'DONE') {
522
+ clearInterval(intervalId); // Stop the interval
523
+ resolve(statusRes.data.fileHeader); // Resolve the promise with the final status
524
+ } else {
525
+ // Optionally update progress or handle other status cases here
526
+ //this.emit('progress', statusRes.progress); // Emit progress updates if available
527
+ }
528
+ _context11.next = 11;
529
+ break;
530
+ case 7:
531
+ _context11.prev = 7;
532
+ _context11.t0 = _context11["catch"](0);
533
+ clearInterval(intervalId); // Stop the interval on error
534
+ reject(_context11.t0); // Reject the promise if there's an error
535
+ case 11:
536
+ case "end":
537
+ return _context11.stop();
538
+ }
539
+ }, _callee10, null, [[0, 7]]);
540
+ })), 2000); // Check every 2 seconds
541
+ }));
542
+ case 1:
543
+ case "end":
544
+ return _context12.stop();
545
+ }
546
+ }, _callee11);
547
+ }));
548
+ function _checkUploadStatusUntilDone(_x14) {
549
+ return _checkUploadStatusUntilDone2.apply(this, arguments);
550
+ }
551
+ return _checkUploadStatusUntilDone;
552
+ }()
553
+ }, {
554
+ key: "_uploadGetStatusUpdate",
555
+ value: function () {
556
+ var _uploadGetStatusUpdate2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(fileRecord) {
557
+ var payload, response;
558
+ return _regeneratorRuntime().wrap(function _callee12$(_context13) {
559
+ while (1) switch (_context13.prev = _context13.next) {
560
+ case 0:
561
+ payload = ["@filekit.ffs_server_receiveAndStore_toS3_flow", {
562
+ "fileObject": fileRecord,
563
+ // using fileRecord from newFile.toRecord()
564
+ "getStatusUpdate": true
565
+ }];
566
+ console.log(payload);
567
+ _context13.next = 4;
568
+ return this._axios.post('/file_service/ffs_runFlow', payload);
569
+ case 4:
570
+ response = _context13.sent;
571
+ if (!(!response || response.status != 200)) {
572
+ _context13.next = 7;
573
+ break;
574
+ }
575
+ throw new Error('Error getting upload status');
576
+ case 7:
577
+ return _context13.abrupt("return", response);
578
+ case 8:
579
+ case "end":
580
+ return _context13.stop();
581
+ }
582
+ }, _callee12, this);
583
+ }));
584
+ function _uploadGetStatusUpdate(_x15) {
585
+ return _uploadGetStatusUpdate2.apply(this, arguments);
586
+ }
587
+ return _uploadGetStatusUpdate;
588
+ }()
589
+ }, {
590
+ key: "convertToBase64",
591
+ value: function convertToBase64(buffer) {
592
+ return btoa(new Uint8Array(buffer).reduce(function (data, _byte) {
593
+ return data + String.fromCharCode(_byte);
594
+ }, ''));
595
+ }
596
+ }]);
597
+ return RbtFile;
598
+ }(_events.EventEmitter);
599
+ _defineProperty(RbtFile, "FRAME_SIZE", 1024 * 1024);