UNPKG

ionic-coreo

Version:

Ionic2 module for integration with Coreo

170 lines 7.04 kB
"use strict"; var core_1 = require('@angular/core'); var ionic_angular_1 = require('ionic-angular'); var camera_1 = require('@ionic-native/camera'); var file_1 = require('@ionic-native/file'); var file_path_1 = require('@ionic-native/file-path'); var config_1 = require('./config'); var lodash_1 = require('lodash'); var CoreoPhoto = (function () { function CoreoPhoto(platform, config, camera, file, filePath) { this.platform = platform; this.config = config; this.camera = camera; this.file = file; this.filePath = filePath; } CoreoPhoto.prototype.capturePicture = function (options) { var _this = this; if (options === void 0) { options = {}; } return this.platform.ready() .then(function () { return _this.capturePictureFromCamera(options); }) .then(function (file) { return _this.movePictureFileToAppStorage(file); }) .catch(function (err) { return _this.handleError(err); }); }; CoreoPhoto.prototype.selectPicture = function (options) { var _this = this; if (options === void 0) { options = {}; } return this.platform.ready() .then(function () { return _this.selectPictureFromLibrary(options); }) .then(function (file) { return _this.resolveNativePath(file); }) .then(function (file) { return _this.copyPictureFileToAppStorage(file); }) .catch(function (err) { return _this.handleError(err); }); }; CoreoPhoto.prototype.capturePictureFromCamera = function (options) { if (options === void 0) { options = {}; } var opts = Object.assign({ quality: 80, sourceType: this.camera.PictureSourceType.CAMERA, destinationType: this.camera.DestinationType.FILE_URI, allowEdit: false, encodingType: this.camera.EncodingType.JPEG, correctOrientation: true, cameraDirection: this.camera.Direction.BACK, saveToPhotoAlbum: true }, options); return this.camera.getPicture(opts); }; CoreoPhoto.prototype.selectPictureFromLibrary = function (options) { if (options === void 0) { options = {}; } var opts = Object.assign({ sourceType: this.camera.PictureSourceType.PHOTOLIBRARY, destinationType: this.camera.DestinationType.FILE_URI }, options); return this.camera.getPicture(opts); }; CoreoPhoto.prototype.movePictureFileToAppStorage = function (file) { var _this = this; if (this.config.isIonicView) { return Promise.resolve(file); } var targetDir = cordova.file.dataDirectory; // Strip the name from the path var _a = this.splitToDirAndFilename(file), dir = _a.dir, filename = _a.filename; if (dir === targetDir) { return Promise.resolve(file); } // Rename the file to something unique, to prevent // a different file with the same name being // displayed from the cache instead. return this.file .moveFile(dir, filename, targetDir, this.generateUniqueFilename(filename)) .then(function (entry) { // Try to cleanup the temp files // in the background, ignoring // any errors which occur. _this.camera.cleanup().catch(function (err) { }); // Return the new file location. return entry.nativeURL; }) .catch(function (err) { // We couldn't use the file plugin. // Let's just use the original path. return file; }); }; CoreoPhoto.prototype.copyPictureFileToAppStorage = function (file) { if (this.config.isIonicView) { return Promise.resolve(file); } var targetDir = cordova.file.dataDirectory; // Strip the name from the path var _a = this.splitToDirAndFilename(file), dir = _a.dir, filename = _a.filename; if (dir === targetDir) { return Promise.resolve(file); } return this.file .copyFile(dir, filename, targetDir, this.generateUniqueFilename(filename)) .then(function (entry) { return entry.nativeURL; }) .catch(function (err) { // We couldn't use the file plugin. // Let's just use the original path. return file; }); }; CoreoPhoto.prototype.resolveNativePath = function (file) { // Try to resolve the actual path, // as Android returns a `content://` path which // can't be used to copy over the file. // If there is an error, just return the original path. try { return this.filePath .resolveNativePath(file) .catch(function () { return file; }); } catch (e) { return Promise.resolve(file); } }; CoreoPhoto.prototype.splitToDirAndFilename = function (path) { var lastSlashIndex = path.lastIndexOf('/'); return { dir: path.slice(0, lastSlashIndex + 1), filename: path.slice(lastSlashIndex + 1, path.length) }; }; CoreoPhoto.prototype.generateUniqueFilename = function (existing) { var existingParts = existing.split('.'); return Date.now() + "." + existingParts[existingParts.length - 1]; }; CoreoPhoto.prototype.handleError = function (err) { // If the user cancelled taking/selecting a photo, // an error will be thrown. // We want to ignore this so we'll return a resolved // promise in that case. // In all other cases, reject with the original error. if (lodash_1.isString(err)) { if (err === 'no image selected' || err === 'Camera cancelled.' || err === 'Selection cancelled.' // Android ) { // The user cancelled on us. // Don't throw the error, but instead // return an empty URL. return Promise.resolve(); } if (err === 'cordova_not_available') { // We're in the browser, send back a placeholder. return Promise.resolve('https://placekitten.com/g/600/1000'); } } // Otherwise, we really do want to throw the error. console.error('There was an error when taking a photo:'); console.error(JSON.stringify(err)); return Promise.reject(err); }; CoreoPhoto.decorators = [ { type: core_1.Injectable }, ]; /** @nocollapse */ CoreoPhoto.ctorParameters = function () { return [ { type: ionic_angular_1.Platform, }, { type: config_1.CoreoConfig, }, { type: camera_1.Camera, }, { type: file_1.File, }, { type: file_path_1.FilePath, }, ]; }; return CoreoPhoto; }()); exports.CoreoPhoto = CoreoPhoto; //# sourceMappingURL=photo.js.map