UNPKG

10.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var core_1 = require("@angular/core");
4var platform_browser_1 = require("@angular/platform-browser");
5var ionic_core_1 = require("@coreo/ionic-core");
6var camera_1 = require("@ionic-native/camera");
7var file_1 = require("@ionic-native/file");
8var file_path_1 = require("@ionic-native/file-path");
9var ionic_angular_1 = require("ionic-angular");
10var isString = require('is-string');
11var CoreoPhoto = (function () {
12 function CoreoPhoto(platform, config, camera, file, filePath) {
13 this.platform = platform;
14 this.config = config;
15 this.camera = camera;
16 this.file = file;
17 this.filePath = filePath;
18 }
19 CoreoPhoto.prototype.capturePicture = function (options) {
20 var _this = this;
21 if (options === void 0) { options = {}; }
22 return this.platform.ready()
23 .then(function () { return _this.capturePictureFromCamera(options); })
24 .then(function (file) { return _this.movePictureFileToAppStorage(file); })
25 .catch(function (err) { return _this.handleError(err); });
26 };
27 CoreoPhoto.prototype.selectPicture = function (options) {
28 var _this = this;
29 if (options === void 0) { options = {}; }
30 return this.platform.ready()
31 .then(function () { return _this.selectPictureFromLibrary(options); })
32 .then(function (file) { return _this.resolveNativePath(file); })
33 .then(function (file) { return _this.copyPictureFileToAppStorage(file); })
34 .catch(function (err) { return _this.handleError(err); });
35 };
36 CoreoPhoto.prototype.resolvePicturePath = function (filename, normalize) {
37 if (normalize === void 0) { normalize = true; }
38 if (!filename) {
39 return filename;
40 }
41 // Gets the full path of a picture which was previously
42 // captured / selected by this service.
43 // This allows us to only store the filename,
44 // which guards against the application directory
45 // changing (happens e.g. when iOS app is updated).
46 if (/^(?:http|https|file|cdvfile):\/\//.test(filename)) {
47 // This is already a full path - return as-is.
48 return filename;
49 }
50 // Otherwise, prefix with the persistent dir.
51 try {
52 var path = cordova.file.dataDirectory + "/" + filename;
53 return normalize ? ionic_angular_1.normalizeURL(path) : path;
54 }
55 catch (e) {
56 // Cordova not available. Return the original filename.
57 return filename;
58 }
59 };
60 CoreoPhoto.prototype.capturePictureFromCamera = function (options) {
61 if (options === void 0) { options = {}; }
62 var opts = Object.assign({
63 quality: 80,
64 sourceType: this.camera.PictureSourceType.CAMERA,
65 destinationType: this.camera.DestinationType.FILE_URI,
66 allowEdit: false,
67 encodingType: this.camera.EncodingType.JPEG,
68 correctOrientation: true,
69 cameraDirection: this.camera.Direction.BACK,
70 saveToPhotoAlbum: true
71 }, options);
72 return this.camera.getPicture(opts);
73 };
74 CoreoPhoto.prototype.selectPictureFromLibrary = function (options) {
75 if (options === void 0) { options = {}; }
76 var opts = Object.assign({
77 sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
78 destinationType: this.camera.DestinationType.FILE_URI
79 }, options);
80 return this.camera.getPicture(opts);
81 };
82 CoreoPhoto.prototype.movePictureFileToAppStorage = function (file) {
83 var _this = this;
84 if (this.config.isIonicView) {
85 return Promise.resolve(file);
86 }
87 var targetDir = cordova.file.dataDirectory;
88 // Strip the name from the path
89 var parts = this.splitToDirAndFilename(file);
90 var srcDir = parts.dir;
91 var srcFilename = parts.filename;
92 if (srcDir === targetDir) {
93 // We only return the filename, as the dataDirectory
94 // can change if the app is upgraded.
95 return Promise.resolve(srcFilename);
96 }
97 // Rename the file to something unique, to prevent
98 // a different file with the same name being
99 // displayed from the cache instead.
100 var targetFilename = this.generateUniqueFilename(srcFilename);
101 return this.file
102 .moveFile(srcDir, srcFilename, targetDir, targetFilename)
103 .then(function (entry) {
104 // Try to cleanup the temp files
105 // in the background, ignoring
106 // any errors which occur.
107 // Try to cleanup the temp files
108 // in the background, ignoring
109 // any errors which occur.
110 _this.camera.cleanup().catch(function (err) { });
111 // We only return the filename, as the dataDirectory
112 // can change if the app is upgraded.
113 return targetFilename;
114 })
115 .catch(function (err) {
116 // We couldn't use the file plugin.
117 // Let's just use the original path.
118 return file;
119 });
120 };
121 CoreoPhoto.prototype.copyPictureFileToAppStorage = function (file) {
122 if (this.config.isIonicView) {
123 return Promise.resolve(file);
124 }
125 var targetDir = cordova.file.dataDirectory;
126 // Strip the name from the path
127 var parts = this.splitToDirAndFilename(file);
128 var srcDir = parts.dir;
129 var srcFilename = parts.filename;
130 if (srcDir === targetDir) {
131 // We only return the filename, as the dataDirectory
132 // can change if the app is upgraded.
133 return Promise.resolve(srcFilename);
134 }
135 // Rename the file to something unique, to prevent
136 // a different file with the same name being
137 // displayed from the cache instead.
138 var targetFilename = this.generateUniqueFilename(srcFilename);
139 return this.file
140 .copyFile(srcDir, srcFilename, targetDir, targetFilename)
141 .then(function () {
142 // We only return the filename, as the dataDirectory
143 // can change if the app is upgraded.
144 return targetFilename;
145 })
146 .catch(function (err) {
147 // We couldn't use the file plugin.
148 // Let's just use the original path.
149 return file;
150 });
151 };
152 CoreoPhoto.prototype.resolveNativePath = function (file) {
153 // Try to resolve the actual path,
154 // as Android returns a `content://` path which
155 // can't be used to copy over the file.
156 // If there is an error, just return the original path.
157 try {
158 return this.filePath
159 .resolveNativePath(file)
160 .catch(function () { return file; });
161 }
162 catch (e) {
163 return Promise.resolve(file);
164 }
165 };
166 CoreoPhoto.prototype.splitToDirAndFilename = function (path) {
167 var lastSlashIndex = path.lastIndexOf('/');
168 return {
169 dir: path.slice(0, lastSlashIndex + 1),
170 filename: path.slice(lastSlashIndex + 1, path.length)
171 };
172 };
173 CoreoPhoto.prototype.generateUniqueFilename = function (existing) {
174 var existingParts = existing.split('.');
175 return Date.now() + "." + existingParts[existingParts.length - 1];
176 };
177 CoreoPhoto.prototype.handleError = function (err) {
178 // If the user cancelled taking/selecting a photo,
179 // or the permissions were deined on iOS,
180 // an error will be thrown.
181 // We want to ignore this so we'll return a resolved
182 // promise in that case.
183 // In all other cases, reject with the original error.
184 if (isString(err)) {
185 if (err === 'no image selected' ||
186 // iOS
187 err === 'No Image Selected' ||
188 // iOS
189 err === 'has no access to camera' ||
190 // iOS
191 err === 'has no access to assets' ||
192 // iOS
193 err === 'Camera cancelled.' ||
194 // Android
195 err === 'Selection cancelled.' // Android
196 ) {
197 // The user cancelled on us.
198 // Don't throw the error, but instead
199 // return an empty URL.
200 return Promise.resolve();
201 }
202 if (err === 'cordova_not_available') {
203 // We're in the browser, send back a placeholder.
204 return Promise.resolve('https://placekitten.com/g/600/1000');
205 }
206 }
207 // Otherwise, we really do want to throw the error.
208 console.error('There was an error when taking a photo:');
209 console.error(JSON.stringify(err));
210 return Promise.reject(err);
211 };
212 CoreoPhoto.decorators = [
213 { type: core_1.Injectable },
214 ];
215 /** @nocollapse */
216 CoreoPhoto.ctorParameters = function () { return [
217 { type: ionic_angular_1.Platform, },
218 { type: ionic_core_1.CoreoConfig, },
219 { type: camera_1.Camera, },
220 { type: file_1.File, },
221 { type: file_path_1.FilePath, },
222 ]; };
223 return CoreoPhoto;
224}());
225exports.CoreoPhoto = CoreoPhoto;
226var CoreoPhotoPipe = (function () {
227 function CoreoPhotoPipe(coreoPhoto) {
228 this.coreoPhoto = coreoPhoto;
229 }
230 CoreoPhotoPipe.prototype.transform = function (filename) {
231 return this.coreoPhoto.resolvePicturePath(filename);
232 };
233 CoreoPhotoPipe.decorators = [
234 { type: core_1.Pipe, args: [{
235 name: 'coreoPhoto'
236 },] },
237 ];
238 /** @nocollapse */
239 CoreoPhotoPipe.ctorParameters = function () { return [
240 { type: CoreoPhoto, },
241 ]; };
242 return CoreoPhotoPipe;
243}());
244exports.CoreoPhotoPipe = CoreoPhotoPipe;
245var CoreoPhotoBgUrlPipe = (function () {
246 function CoreoPhotoBgUrlPipe(coreoPhoto, sanitized) {
247 this.coreoPhoto = coreoPhoto;
248 this.sanitized = sanitized;
249 }
250 CoreoPhotoBgUrlPipe.prototype.transform = function (filename) {
251 var path = this.coreoPhoto.resolvePicturePath(filename);
252 return this.sanitized.bypassSecurityTrustStyle("url('" + path + "')");
253 };
254 CoreoPhotoBgUrlPipe.decorators = [
255 { type: core_1.Pipe, args: [{
256 name: 'coreoPhotoBgUrl'
257 },] },
258 ];
259 /** @nocollapse */
260 CoreoPhotoBgUrlPipe.ctorParameters = function () { return [
261 { type: CoreoPhoto, },
262 { type: platform_browser_1.DomSanitizer, },
263 ]; };
264 return CoreoPhotoBgUrlPipe;
265}());
266exports.CoreoPhotoBgUrlPipe = CoreoPhotoBgUrlPipe;
267//# sourceMappingURL=photo.js.map
\No newline at end of file