1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var core_1 = require("@angular/core");
|
4 | var platform_browser_1 = require("@angular/platform-browser");
|
5 | var ionic_core_1 = require("@coreo/ionic-core");
|
6 | var camera_1 = require("@ionic-native/camera");
|
7 | var file_1 = require("@ionic-native/file");
|
8 | var file_path_1 = require("@ionic-native/file-path");
|
9 | var ionic_angular_1 = require("ionic-angular");
|
10 | var isString = require('is-string');
|
11 | var 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 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | if (/^(?:http|https|file|cdvfile):\/\//.test(filename)) {
|
47 |
|
48 | return filename;
|
49 | }
|
50 |
|
51 | try {
|
52 | var path = cordova.file.dataDirectory + "/" + filename;
|
53 | return normalize ? ionic_angular_1.normalizeURL(path) : path;
|
54 | }
|
55 | catch (e) {
|
56 |
|
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 |
|
89 | var parts = this.splitToDirAndFilename(file);
|
90 | var srcDir = parts.dir;
|
91 | var srcFilename = parts.filename;
|
92 | if (srcDir === targetDir) {
|
93 |
|
94 |
|
95 | return Promise.resolve(srcFilename);
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 | var targetFilename = this.generateUniqueFilename(srcFilename);
|
101 | return this.file
|
102 | .moveFile(srcDir, srcFilename, targetDir, targetFilename)
|
103 | .then(function (entry) {
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | _this.camera.cleanup().catch(function (err) { });
|
111 |
|
112 |
|
113 | return targetFilename;
|
114 | })
|
115 | .catch(function (err) {
|
116 |
|
117 |
|
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 |
|
127 | var parts = this.splitToDirAndFilename(file);
|
128 | var srcDir = parts.dir;
|
129 | var srcFilename = parts.filename;
|
130 | if (srcDir === targetDir) {
|
131 |
|
132 |
|
133 | return Promise.resolve(srcFilename);
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 | var targetFilename = this.generateUniqueFilename(srcFilename);
|
139 | return this.file
|
140 | .copyFile(srcDir, srcFilename, targetDir, targetFilename)
|
141 | .then(function () {
|
142 |
|
143 |
|
144 | return targetFilename;
|
145 | })
|
146 | .catch(function (err) {
|
147 |
|
148 |
|
149 | return file;
|
150 | });
|
151 | };
|
152 | CoreoPhoto.prototype.resolveNativePath = function (file) {
|
153 |
|
154 |
|
155 |
|
156 |
|
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 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | if (isString(err)) {
|
185 | if (err === 'no image selected' ||
|
186 |
|
187 | err === 'No Image Selected' ||
|
188 |
|
189 | err === 'has no access to camera' ||
|
190 |
|
191 | err === 'has no access to assets' ||
|
192 |
|
193 | err === 'Camera cancelled.' ||
|
194 |
|
195 | err === 'Selection cancelled.'
|
196 | ) {
|
197 |
|
198 |
|
199 |
|
200 | return Promise.resolve();
|
201 | }
|
202 | if (err === 'cordova_not_available') {
|
203 |
|
204 | return Promise.resolve('https://placekitten.com/g/600/1000');
|
205 | }
|
206 | }
|
207 |
|
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 |
|
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 | }());
|
225 | exports.CoreoPhoto = CoreoPhoto;
|
226 | var 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 |
|
239 | CoreoPhotoPipe.ctorParameters = function () { return [
|
240 | { type: CoreoPhoto, },
|
241 | ]; };
|
242 | return CoreoPhotoPipe;
|
243 | }());
|
244 | exports.CoreoPhotoPipe = CoreoPhotoPipe;
|
245 | var 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 |
|
260 | CoreoPhotoBgUrlPipe.ctorParameters = function () { return [
|
261 | { type: CoreoPhoto, },
|
262 | { type: platform_browser_1.DomSanitizer, },
|
263 | ]; };
|
264 | return CoreoPhotoBgUrlPipe;
|
265 | }());
|
266 | exports.CoreoPhotoBgUrlPipe = CoreoPhotoBgUrlPipe;
|
267 |
|
\ | No newline at end of file |