UNPKG

js-uploader

Version:
234 lines 11.7 kB
import { __awaiter, __extends, __generator, __read, __spread } from "tslib"; import { EventEmitter, FileStore, getStorage } from './modules'; import { Observable, from, of, forkJoin, NEVER, merge } from 'rxjs'; import { bufferCount, concatMap, last, mapTo, mergeMap, takeUntil, tap, map, filter, reduce, switchMap, } from 'rxjs/operators'; import { EventType } from '../interface'; import { Logger } from '../shared'; import { isElectron } from '../utils'; var Base = /** @class */ (function (_super) { __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 ? from(input) : 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 Observable(function (ob) { var sub; try { var data = typeof input === 'function' ? input.apply(void 0, __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 from(items) .pipe(bufferCount(1000), concatMap(function (values) { return 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 from(items) .pipe(bufferCount(1000), concatMap(function (values) { return 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 from(items) .pipe(bufferCount(1000), concatMap(function (values) { return 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 = !isElectron() && file.raw ? _this.presistBlob(String(file.id), file.raw) : Promise.resolve(); promise .then(function () { Logger.warn("save file " + file.name); var upfile = file.raw instanceof Blob ? Object.assign({}, file, { raw: null }) : file; getStorage(_this.uploaderID).UploadFile.setItem(file.id, upfile).toPromise().then(resolve).catch(reject); }) .catch(reject); }); }; Base.prototype.presistBlob = function (key, blob) { return getStorage(this.uploaderID).BinaryLike.setItem(key, blob).toPromise(); }; Base.prototype.presistTaskWithoutBlob = function (tasks, nofication$) { var _this = this; Logger.info('Uploader -> presistTask -> tasks', tasks); var tryTakeUntil = function () { return takeUntil(nofication$ || NEVER); }; var job$ = tasks.map(function (task) { var uptask = Object.assign({}, task, { fileList: [] }); return from(task.fileIDList).pipe(map(function (id) { return FileStore.get(id); }), filter(function (file) { return !!file; }), 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 // }, []), bufferCount(1000), concatMap(function (values) { return getStorage(_this.uploaderID).UploadFile.setItems(values); }), concatMap(function () { return getStorage(_this.uploaderID).UploadTask.setItem(uptask.id, uptask); }), last(), tap(function () { _this.emit(EventType.TaskPresist, task); }), tryTakeUntil()); }); return forkJoin(job$).pipe(mapTo(tasks)); }; Base.prototype.presistTask = function (tasks, nofication$) { var _this = this; Logger.info('Uploader -> presistTask -> tasks', tasks); var tryTakeUntil = function () { return takeUntil(nofication$ || NEVER); }; var job$ = tasks.map(function (task) { var uptask = Object.assign({}, task, { fileList: [] }); return from(task.fileIDList).pipe(mergeMap(function (id) { return from(_this.presistUploadFile(FileStore.get(id))); }), last(), concatMap(function () { return getStorage(_this.uploaderID).UploadTask.setItem(uptask.id, uptask); }), tap(function () { _this.emit(EventType.TaskPresist, task); }), tryTakeUntil()); }); return forkJoin(job$).pipe(mapTo(tasks)); }; Base.prototype.removeChunkFromStroage = function () { var chunks = []; for (var _i = 0; _i < arguments.length; _i++) { chunks[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { return __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*/, 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*/, 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 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 of(ids) .pipe(concatMap(function () { return getStorage(_this.uploaderID).UploadFile.removeItems(ids); }), tap(function () { console.log('Storage.UploadFile.removeItems', ids); }), concatMap(function () { return getStorage(_this.uploaderID).BinaryLike.removeItems(ids); }), tap(function () { console.log('Storage.BinaryLike.removeItems', ids); }), concatMap(function () { return from(ids).pipe(mergeMap(function (id) { return getStorage(_this.uploaderID).FileChunk.getKeysWhenKeyStartsWith(id); }), reduce(function (arr, val) { return arr.concat(val); }, []), tap(function (val) { console.log('Storage.FileChunk.keysStartingWith', val); }), switchMap(function (chunkIds) { return from(_this.removeChunkFromStroage.apply(_this, __spread(chunkIds))); })); })) .toPromise(); }; Base.prototype.removeTaskFromStroage = function () { var tasks = []; for (var _i = 0; _i < arguments.length; _i++) { tasks[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var ids, fileIDs; var _this = this; return __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*/, getStorage(this.uploaderID) .UploadTask.removeItems(ids) .pipe(tap(function () { console.log('Storage.UploadTask.removeItems', ids); }), switchMap(function () { return from(_this.removeFileFromStroage.apply(_this, __spread(fileIDs))); })) .toPromise()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Base.prototype.clearStorage = function (uploaderID) { var id = uploaderID || this.uploaderID; return merge(getStorage(id).UploadTask.clear(), getStorage(id).UploadFile.clear(), getStorage(id).FileChunk.clear(), getStorage(id).BinaryLike.clear()).toPromise(); }; Base.prototype.hookWrap = function (fn, promiseValue) { return fn || Promise.resolve(promiseValue); }; return Base; }(EventEmitter)); export default Base; //# sourceMappingURL=Base.js.map