UNPKG

@meumobi/mmb-media-provider

Version:

A angular service to handle media on meumobi apps

235 lines 9.58 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { FileTransfer } from '@ionic-native/file-transfer'; import { File } from '@ionic-native/file'; import { Injectable } from '@angular/core'; import { Storage } from '@ionic/storage'; import { FileOpener } from '@ionic-native/file-opener'; import { Md5 } from 'ts-md5/dist/md5'; import { Platform } from 'ionic-angular/platform/platform'; import { BehaviorSubject } from 'rxjs'; var MediaService = /** @class */ (function () { function MediaService(fileTransfer, storage, file, fileOpener, plt) { this.fileTransfer = fileTransfer; this.storage = storage; this.file = file; this.fileOpener = fileOpener; this.plt = plt; this.files = {}; this.files$ = new BehaviorSubject({}); this.fileTranfers = {}; this.options = { localFolder: 'Downloads' }; this.mimes = { 'application/pdf': { label: 'View', icon: 'eye', download: true }, 'text/html': { label: 'Open', icon: 'open', download: false }, 'application/vnd.youtube.video+html': { label: 'Play', icon: 'play', download: false }, 'application/vnd.ms-excel': { label: 'View', icon: 'eye', download: true }, 'audio/mpeg': { label: 'Play', icon: 'play', download: true }, 'video/mp4': { label: 'Play', icon: 'play', download: true }, 'application/vnd.ms-powerpoint': { class: 'fa-file-powerpoint-o', label: 'View', icon: 'download', download: true, }, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': { label: 'View', icon: 'eye', download: true } }; this.getFilesFromStorage(); } MediaService.prototype.getFilesFromStorage = function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, this.storage.get('files') .then(function (data) { if (data) { _this.files = data; _this.files$.next(_this.files); } return _this.files; })]; }); }); }; MediaService.prototype.remove = function (file) { var _this = this; return this.getFileEntry(file.fullPath) .then(function () { _this.file.removeFile(_this.file.dataDirectory, file.path).then(function () { _this.removeFile(file); }); file.status = 'download'; return file; }); }; MediaService.prototype.decorateFile = function (file) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { file.behavior = this.getBehavior(file.type); console.log('Files Provider, decorateFile'); console.log(file); if (file.behavior.download && this.plt.is('cordova')) { file.name = file.name || this.getFileName(file); file.path = file.path || this.getFilePath(file.name); file.status = this.getFileStatus(file.path); file.fullPath = this.getFileFullPath(file.path); } else { file.status = 'open_by_link'; } return [2 /*return*/, file]; }); }); }; MediaService.prototype.abort = function (file) { this.fileTranfers[file.path].abort(); }; MediaService.prototype.download = function (file) { var _this = this; this.fileTranfers[file.path] = this.fileTransfer.create(); var result = new BehaviorSubject(file); file.status = 'downloading'; result.next(file); this.fileTranfers[file.path] .download(file.url, file.fullPath) .then(function () { _this.getFileEntry(file.fullPath) .then(function (data) { if (data.isFile) { file.status = 'downloaded'; _this.addFile(file); result.next(file); } else { file.status = 'download'; result.next(file); } }); }, function (error) { console.log(error); file.status = 'download'; result.next(file); }); return result; }; MediaService.prototype.getFileEntry = function (fullPath) { return this.file.resolveLocalFilesystemUrl(fullPath); }; MediaService.prototype.getFilePath = function (fileName) { return this.options.localFolder + "/" + fileName; }; MediaService.prototype.getFileFullPath = function (filePath) { return "" + this.file.dataDirectory + filePath; }; MediaService.prototype.getFileName = function (file) { var ext = file.extension; var name = Md5.hashStr(file.url); return name + "." + ext; }; MediaService.prototype.getFileStatus = function (filePath) { return this.files[filePath] ? 'downloaded' : 'download'; }; MediaService.prototype.getBehavior = function (fileType) { var defaultBehavior = { label: 'View', icon: 'eye', download: true, }; return fileType in this.mimes ? this.mimes[fileType] : defaultBehavior; }; MediaService.prototype.openFile = function (file) { this.fileOpener.open(file.fullPath, file.type); }; MediaService.prototype.addFile = function (file) { var _this = this; this.files[file.path] = file; this.storage.set('files', this.files) .then(function () { return _this.files$.next(_this.files); }); }; MediaService.prototype.removeFile = function (file) { var _this = this; delete this.files[file.path]; this.storage.set('files', this.files) .then(function () { return _this.files$.next(_this.files); }); }; MediaService.prototype.getFilesObserver = function () { return this.files$.asObservable(); }; MediaService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ MediaService.ctorParameters = function () { return [ { type: FileTransfer, }, { type: Storage, }, { type: File, }, { type: FileOpener, }, { type: Platform, }, ]; }; return MediaService; }()); export { MediaService }; //# sourceMappingURL=media.service.js.map