UNPKG

js-uploader

Version:
236 lines 12.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var modules_1 = require("./modules"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var interface_1 = require("../interface"); var shared_1 = require("../shared"); var utils_1 = require("../utils"); var Base = /** @class */ (function (_super) { tslib_1.__extends(Base, _super); function Base(uploaderID) { var _this = _super.call(this) || this; _this.uploaderID = uploaderID; return _this; } Base.prototype.toObserverble = function (input) { return input && input instanceof Promise ? rxjs_1.from(input) : rxjs_1.of(input); }; Base.prototype.createObserverble = function (input) { var _this = this; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return new rxjs_1.Observable(function (ob) { var sub; try { var data = typeof input === 'function' ? input.apply(void 0, tslib_1.__spread(args)) : input; sub = _this.toObserverble(data).subscribe(ob); } catch (error) { ob.error(error); } return function () { return sub === null || sub === void 0 ? void 0 : sub.unsubscribe(); }; }); }; Base.prototype.presist = function (task, file, chunk) { task && this.presistTaskOnly(task); file && this.presistFileOnly(file); chunk && this.presistChunkOnly(chunk); }; Base.prototype.presistChunkOnly = function () { var _this = this; var chunks = []; for (var _i = 0; _i < arguments.length; _i++) { chunks[_i] = arguments[_i]; } var items = chunks === null || chunks === void 0 ? void 0 : chunks.map(function (chunk) { return ({ key: String(chunk.id), value: Object.assign({}, chunk, { data: null }), }); }); return rxjs_1.from(items) .pipe(operators_1.bufferCount(1000), operators_1.concatMap(function (values) { return modules_1.getStorage(_this.uploaderID).FileChunk.setItems(values); })) .toPromise(); }; Base.prototype.presistFileOnly = function () { var _this = this; var files = []; for (var _i = 0; _i < arguments.length; _i++) { files[_i] = arguments[_i]; } var items = files === null || files === void 0 ? void 0 : files.map(function (file) { return ({ key: String(file.id), value: Object.assign({}, file, { raw: null, chunkList: null }), }); }); return rxjs_1.from(items) .pipe(operators_1.bufferCount(1000), operators_1.concatMap(function (values) { return modules_1.getStorage(_this.uploaderID).UploadFile.setItems(values); })) .toPromise(); }; Base.prototype.presistTaskOnly = function () { var _this = this; var tasks = []; for (var _i = 0; _i < arguments.length; _i++) { tasks[_i] = arguments[_i]; } var items = tasks === null || tasks === void 0 ? void 0 : tasks.map(function (task) { return ({ key: String(task.id), value: Object.assign({}, task, { fileList: [] }), }); }); return rxjs_1.from(items) .pipe(operators_1.bufferCount(1000), operators_1.concatMap(function (values) { return modules_1.getStorage(_this.uploaderID).UploadTask.setItems(values); })) .toPromise(); }; Base.prototype.presistUploadFile = function (file) { var _this = this; return new Promise(function (resolve, reject) { if (!file) { return reject('no file!'); } var promise = !utils_1.isElectron() && file.raw ? _this.presistBlob(String(file.id), file.raw) : Promise.resolve(); promise .then(function () { shared_1.Logger.warn("save file " + file.name); var upfile = file.raw instanceof Blob ? Object.assign({}, file, { raw: null }) : file; modules_1.getStorage(_this.uploaderID).UploadFile.setItem(file.id, upfile).toPromise().then(resolve).catch(reject); }) .catch(reject); }); }; Base.prototype.presistBlob = function (key, blob) { return modules_1.getStorage(this.uploaderID).BinaryLike.setItem(key, blob).toPromise(); }; Base.prototype.presistTaskWithoutBlob = function (tasks, nofication$) { var _this = this; shared_1.Logger.info('Uploader -> presistTask -> tasks', tasks); var tryTakeUntil = function () { return operators_1.takeUntil(nofication$ || rxjs_1.NEVER); }; var job$ = tasks.map(function (task) { var uptask = Object.assign({}, task, { fileList: [] }); return rxjs_1.from(task.fileIDList).pipe(operators_1.map(function (id) { return modules_1.FileStore.get(id); }), operators_1.filter(function (file) { return !!file; }), operators_1.map(function (file) { var upfile = file.raw instanceof Blob ? Object.assign({}, file, { raw: null }) : file; return { key: upfile.id, value: upfile }; }), // reduce((arr: KeyValuePair[], val: KeyValuePair) => { // arr.push(val) // return arr // }, []), operators_1.bufferCount(1000), operators_1.concatMap(function (values) { return modules_1.getStorage(_this.uploaderID).UploadFile.setItems(values); }), operators_1.concatMap(function () { return modules_1.getStorage(_this.uploaderID).UploadTask.setItem(uptask.id, uptask); }), operators_1.last(), operators_1.tap(function () { _this.emit(interface_1.EventType.TaskPresist, task); }), tryTakeUntil()); }); return rxjs_1.forkJoin(job$).pipe(operators_1.mapTo(tasks)); }; Base.prototype.presistTask = function (tasks, nofication$) { var _this = this; shared_1.Logger.info('Uploader -> presistTask -> tasks', tasks); var tryTakeUntil = function () { return operators_1.takeUntil(nofication$ || rxjs_1.NEVER); }; var job$ = tasks.map(function (task) { var uptask = Object.assign({}, task, { fileList: [] }); return rxjs_1.from(task.fileIDList).pipe(operators_1.mergeMap(function (id) { return rxjs_1.from(_this.presistUploadFile(modules_1.FileStore.get(id))); }), operators_1.last(), operators_1.concatMap(function () { return modules_1.getStorage(_this.uploaderID).UploadTask.setItem(uptask.id, uptask); }), operators_1.tap(function () { _this.emit(interface_1.EventType.TaskPresist, task); }), tryTakeUntil()); }); return rxjs_1.forkJoin(job$).pipe(operators_1.mapTo(tasks)); }; Base.prototype.removeChunkFromStroage = function () { var chunks = []; for (var _i = 0; _i < arguments.length; _i++) { chunks[_i] = arguments[_i]; } return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(chunks === null || chunks === void 0 ? void 0 : chunks.length)) return [3 /*break*/, 4]; if (!(typeof chunks[0] === 'object')) return [3 /*break*/, 2]; return [4 /*yield*/, modules_1.getStorage(this.uploaderID) .FileChunk.removeItems(chunks.map(function (i) { return i.id; })) .toPromise()]; case 1: _a.sent(); console.log('Storage.FileChunk.removeItems'); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, modules_1.getStorage(this.uploaderID) .FileChunk.removeItems(chunks.map(function (i) { return i; })) .toPromise()]; case 3: _a.sent(); console.log('Storage.FileChunk.removeItems'); _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; Base.prototype.removeFileFromFileStore = function () { var fileIDs = []; for (var _i = 0; _i < arguments.length; _i++) { fileIDs[_i] = arguments[_i]; } fileIDs === null || fileIDs === void 0 ? void 0 : fileIDs.forEach(function (id) { return modules_1.FileStore.remove(id); }); }; Base.prototype.removeFileFromStroage = function () { var _this = this; var files = []; for (var _i = 0; _i < arguments.length; _i++) { files[_i] = arguments[_i]; } if (!(files === null || files === void 0 ? void 0 : files.length)) { return Promise.resolve(); } var isFile = typeof files[0] === 'object'; var ids = isFile ? files.map(function (i) { return String(i.id); }) : files.map(function (i) { return String(i); }); // this.removeFileFromFileStore(...ids) return rxjs_1.of(ids) .pipe(operators_1.concatMap(function () { return modules_1.getStorage(_this.uploaderID).UploadFile.removeItems(ids); }), operators_1.tap(function () { console.log('Storage.UploadFile.removeItems', ids); }), operators_1.concatMap(function () { return modules_1.getStorage(_this.uploaderID).BinaryLike.removeItems(ids); }), operators_1.tap(function () { console.log('Storage.BinaryLike.removeItems', ids); }), operators_1.concatMap(function () { return rxjs_1.from(ids).pipe(operators_1.mergeMap(function (id) { return modules_1.getStorage(_this.uploaderID).FileChunk.getKeysWhenKeyStartsWith(id); }), operators_1.reduce(function (arr, val) { return arr.concat(val); }, []), operators_1.tap(function (val) { console.log('Storage.FileChunk.keysStartingWith', val); }), operators_1.switchMap(function (chunkIds) { return rxjs_1.from(_this.removeChunkFromStroage.apply(_this, tslib_1.__spread(chunkIds))); })); })) .toPromise(); }; Base.prototype.removeTaskFromStroage = function () { var tasks = []; for (var _i = 0; _i < arguments.length; _i++) { tasks[_i] = arguments[_i]; } return tslib_1.__awaiter(this, void 0, void 0, function () { var ids, fileIDs; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(tasks === null || tasks === void 0 ? void 0 : tasks.length)) { return [2 /*return*/]; } ids = tasks.map(function (i) { return String(i.id); }); fileIDs = tasks.reduce(function (res, cur) { return res.concat(cur.fileIDList); }, []); return [4 /*yield*/, modules_1.getStorage(this.uploaderID) .UploadTask.removeItems(ids) .pipe(operators_1.tap(function () { console.log('Storage.UploadTask.removeItems', ids); }), operators_1.switchMap(function () { return rxjs_1.from(_this.removeFileFromStroage.apply(_this, tslib_1.__spread(fileIDs))); })) .toPromise()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Base.prototype.clearStorage = function (uploaderID) { var id = uploaderID || this.uploaderID; return rxjs_1.merge(modules_1.getStorage(id).UploadTask.clear(), modules_1.getStorage(id).UploadFile.clear(), modules_1.getStorage(id).FileChunk.clear(), modules_1.getStorage(id).BinaryLike.clear()).toPromise(); }; Base.prototype.hookWrap = function (fn, promiseValue) { return fn || Promise.resolve(promiseValue); }; return Base; }(modules_1.EventEmitter)); exports.default = Base; //# sourceMappingURL=Base.js.map