ionic-coreo
Version:
Ionic2 module for integration with Coreo
150 lines • 6.08 kB
JavaScript
;
var core_1 = require('@angular/core');
var ionic_angular_1 = require('ionic-angular');
var ionic_native_1 = require('ionic-native');
var config_1 = require('./config');
var lodash_1 = require('lodash');
var CoreoPhoto = (function () {
function CoreoPhoto(platform, config) {
this.platform = platform;
this.config = config;
}
CoreoPhoto.prototype.capturePicture = function () {
var _this = this;
return this.platform.ready()
.then(function () { return _this.capturePictureFromCamera(); })
.then(function (file) { return _this.movePictureFileToAppStorage(file); })
.catch(function (err) { return _this.handleError(err); });
};
CoreoPhoto.prototype.selectPicture = function () {
var _this = this;
return this.platform.ready()
.then(function () { return _this.selectPictureFromLibrary(); })
.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 () {
var opts = {
quality: 80,
sourceType: ionic_native_1.Camera.PictureSourceType.CAMERA,
destinationType: ionic_native_1.Camera.DestinationType.FILE_URI,
allowEdit: false,
encodingType: ionic_native_1.Camera.EncodingType.JPEG,
correctOrientation: true,
cameraDirection: ionic_native_1.Camera.Direction.BACK,
saveToPhotoAlbum: true
};
return ionic_native_1.Camera.getPicture(opts);
};
CoreoPhoto.prototype.selectPictureFromLibrary = function () {
var opts = {
sourceType: ionic_native_1.Camera.PictureSourceType.PHOTOLIBRARY,
destinationType: ionic_native_1.Camera.DestinationType.FILE_URI
};
return ionic_native_1.Camera.getPicture(opts);
};
CoreoPhoto.prototype.movePictureFileToAppStorage = 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 ionic_native_1.File
.moveFile(dir, filename, targetDir, filename)
.then(function (entry) {
// Try to cleanup the temp files
// in the background, ignoring
// any errors which occur.
ionic_native_1.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 ionic_native_1.File
.copyFile(dir, filename, targetDir, 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 ionic_native_1.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.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 = [
{ type: ionic_angular_1.Platform, },
{ type: config_1.CoreoConfig, },
];
return CoreoPhoto;
}());
exports.CoreoPhoto = CoreoPhoto;
//# sourceMappingURL=photo.js.map