@capgo/capacitor-file-compressor 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ export class FileCompressorWeb extends WebPlugin {
3
+ async compressImage(options) {
4
+ var _a, _b, _c, _d;
5
+ if (!options.blob) {
6
+ throw new Error('blob is required on web platform');
7
+ }
8
+ const quality = (_a = options.quality) !== null && _a !== void 0 ? _a : 0.6;
9
+ const mimeType = (_b = options.mimeType) !== null && _b !== void 0 ? _b : 'image/jpeg';
10
+ // Validate mime type for web
11
+ if (mimeType !== 'image/jpeg' && mimeType !== 'image/webp') {
12
+ throw new Error('Only image/jpeg and image/webp are supported on web platform');
13
+ }
14
+ // Create image element to load the blob
15
+ const img = await this.createImageFromBlob(options.blob);
16
+ // Calculate dimensions
17
+ let width = (_c = options.width) !== null && _c !== void 0 ? _c : img.width;
18
+ let height = (_d = options.height) !== null && _d !== void 0 ? _d : img.height;
19
+ // Maintain aspect ratio if only one dimension is provided
20
+ if (options.width && !options.height) {
21
+ height = (img.height / img.width) * width;
22
+ }
23
+ else if (options.height && !options.width) {
24
+ width = (img.width / img.height) * height;
25
+ }
26
+ // Create canvas and draw image
27
+ const canvas = document.createElement('canvas');
28
+ canvas.width = width;
29
+ canvas.height = height;
30
+ const ctx = canvas.getContext('2d');
31
+ if (!ctx) {
32
+ throw new Error('Failed to get canvas context');
33
+ }
34
+ ctx.drawImage(img, 0, 0, width, height);
35
+ // Convert canvas to blob
36
+ const blob = await new Promise((resolve, reject) => {
37
+ canvas.toBlob((blob) => {
38
+ if (blob) {
39
+ resolve(blob);
40
+ }
41
+ else {
42
+ reject(new Error('Failed to create blob from canvas'));
43
+ }
44
+ }, mimeType, quality);
45
+ });
46
+ return { blob };
47
+ }
48
+ async getPluginVersion() {
49
+ return { version: '7.0.0' };
50
+ }
51
+ createImageFromBlob(blob) {
52
+ return new Promise((resolve, reject) => {
53
+ const img = new Image();
54
+ const url = URL.createObjectURL(blob);
55
+ img.onload = () => {
56
+ URL.revokeObjectURL(url);
57
+ resolve(img);
58
+ };
59
+ img.onerror = () => {
60
+ URL.revokeObjectURL(url);
61
+ reject(new Error('Failed to load image from blob'));
62
+ };
63
+ img.src = url;
64
+ });
65
+ }
66
+ }
67
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC9C,KAAK,CAAC,aAAa,CAAC,OAA6B;;QAC/C,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;YACjB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;SACrD;QAED,MAAM,OAAO,SAAG,OAAO,CAAC,OAAO,mCAAI,GAAG,CAAC;QACvC,MAAM,QAAQ,SAAG,OAAO,CAAC,QAAQ,mCAAI,YAAY,CAAC;QAElD,6BAA6B;QAC7B,IAAI,QAAQ,KAAK,YAAY,IAAI,QAAQ,KAAK,YAAY,EAAE;YAC1D,MAAM,IAAI,KAAK,CAAC,8DAA8D,CAAC,CAAC;SACjF;QAED,wCAAwC;QACxC,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAEzD,uBAAuB;QACvB,IAAI,KAAK,SAAG,OAAO,CAAC,KAAK,mCAAI,GAAG,CAAC,KAAK,CAAC;QACvC,IAAI,MAAM,SAAG,OAAO,CAAC,MAAM,mCAAI,GAAG,CAAC,MAAM,CAAC;QAE1C,0DAA0D;QAC1D,IAAI,OAAO,CAAC,KAAK,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YACpC,MAAM,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG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,CAAC;YACxB,MAAM,GAAG,GAAG,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;YAEtC,GAAG,CAAC,MAAM,GAAG,GAAG,EAAE;gBAChB,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;gBACzB,OAAO,CAAC,GAAG,CAAC,CAAC;YACf,CAAC,CAAC;YAEF,GAAG,CAAC,OAAO,GAAG,GAAG,EAAE;gBACjB,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;gBACzB,MAAM,CAAC,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC,CAAC;YACtD,CAAC,CAAC;YAEF,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;QAChB,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type { CompressImageOptions, CompressImageResult, FileCompressorPlugin } from './definitions';\n\nexport class FileCompressorWeb extends WebPlugin implements FileCompressorPlugin {\n async compressImage(options: CompressImageOptions): Promise<CompressImageResult> {\n if (!options.blob) {\n throw new Error('blob is required on web platform');\n }\n\n const quality = options.quality ?? 0.6;\n const mimeType = options.mimeType ?? 'image/jpeg';\n\n // Validate mime type for web\n if (mimeType !== 'image/jpeg' && mimeType !== 'image/webp') {\n throw new Error('Only image/jpeg and image/webp are supported on web platform');\n }\n\n // Create image element to load the blob\n const img = await this.createImageFromBlob(options.blob);\n\n // Calculate dimensions\n let width = options.width ?? img.width;\n let height = options.height ?? img.height;\n\n // Maintain aspect ratio if only one dimension is provided\n if (options.width && !options.height) {\n height = (img.height / img.width) * width;\n } else if (options.height && !options.width) {\n width = (img.width / img.height) * height;\n }\n\n // Create canvas and draw image\n const canvas = document.createElement('canvas');\n canvas.width = width;\n canvas.height = height;\n\n const ctx = canvas.getContext('2d');\n if (!ctx) {\n throw new Error('Failed to get canvas context');\n }\n\n ctx.drawImage(img, 0, 0, width, height);\n\n // Convert canvas to blob\n const blob = await new Promise<Blob>((resolve, reject) => {\n canvas.toBlob(\n (blob) => {\n if (blob) {\n resolve(blob);\n } else {\n reject(new Error('Failed to create blob from canvas'));\n }\n },\n mimeType,\n quality,\n );\n });\n\n return { blob };\n }\n\n async getPluginVersion(): Promise<{ version: string }> {\n return { version: '7.0.0' };\n }\n\n private createImageFromBlob(blob: Blob): Promise<HTMLImageElement> {\n return new Promise((resolve, reject) => {\n const img = new Image();\n const url = URL.createObjectURL(blob);\n\n img.onload = () => {\n URL.revokeObjectURL(url);\n resolve(img);\n };\n\n img.onerror = () => {\n URL.revokeObjectURL(url);\n reject(new Error('Failed to load image from blob'));\n };\n\n img.src = url;\n });\n }\n}\n"]}
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ var core = require('@capacitor/core');
4
+
5
+ const FileCompressor = core.registerPlugin('FileCompressor', {
6
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.FileCompressorWeb()),
7
+ });
8
+
9
+ class FileCompressorWeb extends core.WebPlugin {
10
+ async compressImage(options) {
11
+ var _a, _b, _c, _d;
12
+ if (!options.blob) {
13
+ throw new Error('blob is required on web platform');
14
+ }
15
+ const quality = (_a = options.quality) !== null && _a !== void 0 ? _a : 0.6;
16
+ const mimeType = (_b = options.mimeType) !== null && _b !== void 0 ? _b : 'image/jpeg';
17
+ // Validate mime type for web
18
+ if (mimeType !== 'image/jpeg' && mimeType !== 'image/webp') {
19
+ throw new Error('Only image/jpeg and image/webp are supported on web platform');
20
+ }
21
+ // Create image element to load the blob
22
+ const img = await this.createImageFromBlob(options.blob);
23
+ // Calculate dimensions
24
+ let width = (_c = options.width) !== null && _c !== void 0 ? _c : img.width;
25
+ let height = (_d = options.height) !== null && _d !== void 0 ? _d : img.height;
26
+ // Maintain aspect ratio if only one dimension is provided
27
+ if (options.width && !options.height) {
28
+ height = (img.height / img.width) * width;
29
+ }
30
+ else if (options.height && !options.width) {
31
+ width = (img.width / img.height) * height;
32
+ }
33
+ // Create canvas and draw image
34
+ const canvas = document.createElement('canvas');
35
+ canvas.width = width;
36
+ canvas.height = height;
37
+ const ctx = canvas.getContext('2d');
38
+ if (!ctx) {
39
+ throw new Error('Failed to get canvas context');
40
+ }
41
+ ctx.drawImage(img, 0, 0, width, height);
42
+ // Convert canvas to blob
43
+ const blob = await new Promise((resolve, reject) => {
44
+ canvas.toBlob((blob) => {
45
+ if (blob) {
46
+ resolve(blob);
47
+ }
48
+ else {
49
+ reject(new Error('Failed to create blob from canvas'));
50
+ }
51
+ }, mimeType, quality);
52
+ });
53
+ return { blob };
54
+ }
55
+ async getPluginVersion() {
56
+ return { version: '7.0.0' };
57
+ }
58
+ createImageFromBlob(blob) {
59
+ return new Promise((resolve, reject) => {
60
+ const img = new Image();
61
+ const url = URL.createObjectURL(blob);
62
+ img.onload = () => {
63
+ URL.revokeObjectURL(url);
64
+ resolve(img);
65
+ };
66
+ img.onerror = () => {
67
+ URL.revokeObjectURL(url);
68
+ reject(new Error('Failed to load image from blob'));
69
+ };
70
+ img.src = url;
71
+ });
72
+ }
73
+ }
74
+
75
+ var web = /*#__PURE__*/Object.freeze({
76
+ __proto__: null,
77
+ FileCompressorWeb: FileCompressorWeb
78
+ });
79
+
80
+ exports.FileCompressor = FileCompressor;
81
+ //# sourceMappingURL=plugin.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst FileCompressor = registerPlugin('FileCompressor', {\n web: () => import('./web').then((m) => new m.FileCompressorWeb()),\n});\nexport * from './definitions';\nexport { FileCompressor };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class FileCompressorWeb extends WebPlugin {\n async compressImage(options) {\n var _a, _b, _c, _d;\n if (!options.blob) {\n throw new Error('blob is required on web platform');\n }\n const quality = (_a = options.quality) !== null && _a !== void 0 ? _a : 0.6;\n const mimeType = (_b = options.mimeType) !== null && _b !== void 0 ? _b : 'image/jpeg';\n // Validate mime type for web\n if (mimeType !== 'image/jpeg' && mimeType !== 'image/webp') {\n throw new Error('Only image/jpeg and image/webp are supported on web platform');\n }\n // Create image element to load the blob\n const img = await this.createImageFromBlob(options.blob);\n // Calculate dimensions\n let width = (_c = options.width) !== null && _c !== void 0 ? _c : img.width;\n let height = (_d = options.height) !== null && _d !== void 0 ? _d : img.height;\n // Maintain aspect ratio if only one dimension is provided\n if (options.width && !options.height) {\n height = (img.height / img.width) * width;\n }\n else if (options.height && !options.width) {\n width = (img.width / img.height) * height;\n }\n // Create canvas and draw image\n const canvas = document.createElement('canvas');\n canvas.width = width;\n canvas.height = height;\n const ctx = canvas.getContext('2d');\n if (!ctx) {\n throw new Error('Failed to get canvas context');\n }\n ctx.drawImage(img, 0, 0, width, height);\n // Convert canvas to blob\n const blob = await new Promise((resolve, reject) => {\n canvas.toBlob((blob) => {\n if (blob) {\n resolve(blob);\n }\n else {\n reject(new Error('Failed to create blob from canvas'));\n }\n }, mimeType, quality);\n });\n return { blob };\n }\n async getPluginVersion() {\n return { version: '7.0.0' };\n }\n createImageFromBlob(blob) {\n return new Promise((resolve, reject) => {\n const img = new Image();\n const url = URL.createObjectURL(blob);\n img.onload = () => {\n URL.revokeObjectURL(url);\n resolve(img);\n };\n img.onerror = () => {\n URL.revokeObjectURL(url);\n reject(new Error('Failed to load image from blob'));\n };\n img.src = url;\n });\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,cAAc,GAAGA,mBAAc,CAAC,gBAAgB,EAAE;AACxD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,iBAAiB,EAAE,CAAC;AACrE,CAAC;;ACFM,MAAM,iBAAiB,SAASC,cAAS,CAAC;AACjD,IAAI,MAAM,aAAa,CAAC,OAAO,EAAE;AACjC,QAAQ,IAAI,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE;AAC1B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AM;AAC/B,gBAAgB,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC;AACxC,gBAAgB,OAAO,CAAC,GAAG,CAAC;AAC5B,YAAY,CAAC;AACb,YAAY,GAAG,CAAC,OAAO,GAAG,MAAM;AAChC,gBAAgB,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC;AACxC,gBAAgB,MAAM,CAAC,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;AACnE,YAAY,CAAC;AACb,YAAY,GAAG,CAAC,GAAG,GAAG,GAAG;AACzB,QAAQ,CAAC,CAAC;AACV,IAAI;AACJ;;;;;;;;;"}
package/dist/plugin.js ADDED
@@ -0,0 +1,84 @@
1
+ var capacitorFileCompressor = (function (exports, core) {
2
+ 'use strict';
3
+
4
+ const FileCompressor = core.registerPlugin('FileCompressor', {
5
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.FileCompressorWeb()),
6
+ });
7
+
8
+ class FileCompressorWeb extends core.WebPlugin {
9
+ async compressImage(options) {
10
+ var _a, _b, _c, _d;
11
+ if (!options.blob) {
12
+ throw new Error('blob is required on web platform');
13
+ }
14
+ const quality = (_a = options.quality) !== null && _a !== void 0 ? _a : 0.6;
15
+ const mimeType = (_b = options.mimeType) !== null && _b !== void 0 ? _b : 'image/jpeg';
16
+ // Validate mime type for web
17
+ if (mimeType !== 'image/jpeg' && mimeType !== 'image/webp') {
18
+ throw new Error('Only image/jpeg and image/webp are supported on web platform');
19
+ }
20
+ // Create image element to load the blob
21
+ const img = await this.createImageFromBlob(options.blob);
22
+ // Calculate dimensions
23
+ let width = (_c = options.width) !== null && _c !== void 0 ? _c : img.width;
24
+ let height = (_d = options.height) !== null && _d !== void 0 ? _d : img.height;
25
+ // Maintain aspect ratio if only one dimension is provided
26
+ if (options.width && !options.height) {
27
+ height = (img.height / img.width) * width;
28
+ }
29
+ else if (options.height && !options.width) {
30
+ width = (img.width / img.height) * height;
31
+ }
32
+ // Create canvas and draw image
33
+ const canvas = document.createElement('canvas');
34
+ canvas.width = width;
35
+ canvas.height = height;
36
+ const ctx = canvas.getContext('2d');
37
+ if (!ctx) {
38
+ throw new Error('Failed to get canvas context');
39
+ }
40
+ ctx.drawImage(img, 0, 0, width, height);
41
+ // Convert canvas to blob
42
+ const blob = await new Promise((resolve, reject) => {
43
+ canvas.toBlob((blob) => {
44
+ if (blob) {
45
+ resolve(blob);
46
+ }
47
+ else {
48
+ reject(new Error('Failed to create blob from canvas'));
49
+ }
50
+ }, mimeType, quality);
51
+ });
52
+ return { blob };
53
+ }
54
+ async getPluginVersion() {
55
+ return { version: '7.0.0' };
56
+ }
57
+ createImageFromBlob(blob) {
58
+ return new Promise((resolve, reject) => {
59
+ const img = new Image();
60
+ const url = URL.createObjectURL(blob);
61
+ img.onload = () => {
62
+ URL.revokeObjectURL(url);
63
+ resolve(img);
64
+ };
65
+ img.onerror = () => {
66
+ URL.revokeObjectURL(url);
67
+ reject(new Error('Failed to load image from blob'));
68
+ };
69
+ img.src = url;
70
+ });
71
+ }
72
+ }
73
+
74
+ var web = /*#__PURE__*/Object.freeze({
75
+ __proto__: null,
76
+ FileCompressorWeb: FileCompressorWeb
77
+ });
78
+
79
+ exports.FileCompressor = FileCompressor;
80
+
81
+ return exports;
82
+
83
+ })({}, capacitorExports);
84
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst FileCompressor = registerPlugin('FileCompressor', {\n web: () => import('./web').then((m) => new m.FileCompressorWeb()),\n});\nexport * from './definitions';\nexport { FileCompressor };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class FileCompressorWeb extends WebPlugin {\n async compressImage(options) {\n var _a, _b, _c, _d;\n if (!options.blob) {\n throw new Error('blob is required on web platform');\n }\n const quality = (_a = options.quality) !== null && _a !== void 0 ? _a : 0.6;\n const mimeType = (_b = options.mimeType) !== null && _b !== void 0 ? _b : 'image/jpeg';\n // Validate mime type for web\n if (mimeType !== 'image/jpeg' && mimeType !== 'image/webp') {\n throw new Error('Only image/jpeg and image/webp are supported on web platform');\n }\n // Create image element to load the blob\n const img = await this.createImageFromBlob(options.blob);\n // Calculate dimensions\n let width = (_c = options.width) !== null && _c !== void 0 ? _c : img.width;\n let height = (_d = options.height) !== null && _d !== void 0 ? _d : img.height;\n // Maintain aspect ratio if only one dimension is provided\n if (options.width && !options.height) {\n height = (img.height / img.width) * width;\n }\n else if (options.height && !options.width) {\n width = (img.width / img.height) * height;\n }\n // Create canvas and draw image\n const canvas = document.createElement('canvas');\n canvas.width = width;\n canvas.height = height;\n const ctx = canvas.getContext('2d');\n if (!ctx) {\n throw new Error('Failed to get canvas context');\n }\n ctx.drawImage(img, 0, 0, width, height);\n // Convert canvas to blob\n const blob = await new Promise((resolve, reject) => {\n canvas.toBlob((blob) => {\n if (blob) {\n resolve(blob);\n }\n else {\n reject(new Error('Failed to create blob from canvas'));\n }\n }, mimeType, quality);\n });\n return { blob };\n }\n async getPluginVersion() {\n return { version: '7.0.0' };\n }\n createImageFromBlob(blob) {\n return new Promise((resolve, reject) => {\n const img = new Image();\n const url = URL.createObjectURL(blob);\n img.onload = () => {\n URL.revokeObjectURL(url);\n resolve(img);\n };\n img.onerror = () => {\n URL.revokeObjectURL(url);\n reject(new Error('Failed to load image from blob'));\n };\n img.src = url;\n });\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,cAAc,GAAGA,mBAAc,CAAC,gBAAgB,EAAE;IACxD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,iBAAiB,EAAE,CAAC;IACrE,CAAC;;ICFM,MAAM,iBAAiB,SAASC,cAAS,CAAC;IACjD,IAAI,MAAM,aAAa,CAAC,OAAO,EAAE;IACjC,QAAQ,IAAI,EAAE,EAAE,EAAE,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AY,GAAG,CAAC,MAAM,GAAG,MAAM;IAC/B,gBAAgB,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC;IACxC,gBAAgB,OAAO,CAAC,GAAG,CAAC;IAC5B,YAAY,CAAC;IACb,YAAY,GAAG,CAAC,OAAO,GAAG,MAAM;IAChC,gBAAgB,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC;IACxC,gBAAgB,MAAM,CAAC,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACnE,YAAY,CAAC;IACb,YAAY,GAAG,CAAC,GAAG,GAAG,GAAG;IACzB,QAAQ,CAAC,CAAC;IACV,IAAI;IACJ;;;;;;;;;;;;;;;"}
@@ -0,0 +1,116 @@
1
+ import Foundation
2
+ import Capacitor
3
+ import UIKit
4
+
5
+ @objc(FileCompressorPlugin)
6
+ public class FileCompressorPlugin: CAPPlugin, CAPBridgedPlugin {
7
+ private let pluginVersion: String = "7.0.0"
8
+ public let identifier = "FileCompressorPlugin"
9
+ public let jsName = "FileCompressor"
10
+ public let pluginMethods: [CAPPluginMethod] = [
11
+ CAPPluginMethod(name: "compressImage", returnType: CAPPluginReturnPromise),
12
+ CAPPluginMethod(name: "getPluginVersion", returnType: CAPPluginReturnPromise)
13
+ ]
14
+
15
+ @objc func compressImage(_ call: CAPPluginCall) {
16
+ guard let path = call.getString("path") else {
17
+ call.reject("path is required")
18
+ return
19
+ }
20
+
21
+ let quality = call.getFloat("quality") ?? 0.6
22
+ let width = call.getInt("width")
23
+ let height = call.getInt("height")
24
+ let mimeType = call.getString("mimeType") ?? "image/jpeg"
25
+
26
+ // Validate mime type for iOS (only JPEG supported)
27
+ if mimeType != "image/jpeg" {
28
+ call.reject("Only image/jpeg is supported on iOS")
29
+ return
30
+ }
31
+
32
+ // Validate quality range
33
+ if quality < 0.0 || quality > 1.0 {
34
+ call.reject("quality must be between 0.0 and 1.0")
35
+ return
36
+ }
37
+
38
+ // Load image from path
39
+ guard let image = loadImage(from: path) else {
40
+ call.reject("Failed to load image from path")
41
+ return
42
+ }
43
+
44
+ // Resize image if dimensions are provided
45
+ var processedImage = image
46
+ if let targetWidth = width, let targetHeight = height {
47
+ processedImage = resizeImage(image: image, targetWidth: CGFloat(targetWidth), targetHeight: CGFloat(targetHeight))
48
+ } else if let targetWidth = width {
49
+ let aspectRatio = image.size.height / image.size.width
50
+ let targetHeight = CGFloat(targetWidth) * aspectRatio
51
+ processedImage = resizeImage(image: image, targetWidth: CGFloat(targetWidth), targetHeight: targetHeight)
52
+ } else if let targetHeight = height {
53
+ let aspectRatio = image.size.width / image.size.height
54
+ let targetWidth = CGFloat(targetHeight) * aspectRatio
55
+ processedImage = resizeImage(image: image, targetWidth: targetWidth, targetHeight: CGFloat(targetHeight))
56
+ }
57
+
58
+ // Compress image to JPEG
59
+ guard let imageData = processedImage.jpegData(compressionQuality: CGFloat(quality)) else {
60
+ call.reject("Failed to compress image")
61
+ return
62
+ }
63
+
64
+ // Save compressed image to temporary file
65
+ let tempDirectory = FileManager.default.temporaryDirectory
66
+ let fileName = "compressed_\(UUID().uuidString).jpg"
67
+ let fileURL = tempDirectory.appendingPathComponent(fileName)
68
+
69
+ do {
70
+ try imageData.write(to: fileURL)
71
+ call.resolve([
72
+ "path": fileURL.path
73
+ ])
74
+ } catch {
75
+ call.reject("Failed to save compressed image: \(error.localizedDescription)")
76
+ }
77
+ }
78
+
79
+ private func loadImage(from path: String) -> UIImage? {
80
+ // Handle different path types
81
+ if path.hasPrefix("file://") {
82
+ let url = URL(string: path)
83
+ if let url = url, let data = try? Data(contentsOf: url) {
84
+ return UIImage(data: data)
85
+ }
86
+ } else if path.hasPrefix("/") {
87
+ // Absolute file path
88
+ if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
89
+ return UIImage(data: data)
90
+ }
91
+ } else if path.hasPrefix("content://") || path.hasPrefix("file:///") {
92
+ // Try to parse as URL
93
+ if let url = URL(string: path), let data = try? Data(contentsOf: url) {
94
+ return UIImage(data: data)
95
+ }
96
+ }
97
+
98
+ // Try loading from photo library asset identifier
99
+ // This would require Photos framework integration
100
+
101
+ return nil
102
+ }
103
+
104
+ private func resizeImage(image: UIImage, targetWidth: CGFloat, targetHeight: CGFloat) -> UIImage {
105
+ let size = CGSize(width: targetWidth, height: targetHeight)
106
+ let renderer = UIGraphicsImageRenderer(size: size)
107
+
108
+ return renderer.image { _ in
109
+ image.draw(in: CGRect(origin: .zero, size: size))
110
+ }
111
+ }
112
+
113
+ @objc func getPluginVersion(_ call: CAPPluginCall) {
114
+ call.resolve(["version": self.pluginVersion])
115
+ }
116
+ }
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "@capgo/capacitor-file-compressor",
3
+ "version": "7.0.0",
4
+ "description": "Capacitor plugin for efficient image compression supporting PNG, JPEG, and WebP formats across iOS, Android, and Web platforms",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Sources",
14
+ "ios/Tests",
15
+ "Package.swift",
16
+ "CapgoCapacitorFileCompressor.podspec"
17
+ ],
18
+ "author": "Martin Donadieu <martin@capgo.app>",
19
+ "license": "MIT",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/Cap-go/capacitor-file-compressor.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/Cap-go/capacitor-file-compressor/issues"
26
+ },
27
+ "keywords": [
28
+ "capacitor",
29
+ "plugin",
30
+ "native"
31
+ ],
32
+ "scripts": {
33
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
34
+ "verify:ios": "xcodebuild -scheme CapgoCapacitorFileCompressor -destination generic/platform=iOS",
35
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
36
+ "verify:web": "npm run build",
37
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
38
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
39
+ "eslint": "eslint . --ext ts",
40
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
41
+ "swiftlint": "node-swiftlint",
42
+ "docgen": "docgen --api FileCompressorPlugin --output-readme README.md --output-json dist/docs.json",
43
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
44
+ "clean": "rimraf ./dist",
45
+ "watch": "tsc --watch",
46
+ "prepublishOnly": "npm run build"
47
+ },
48
+ "devDependencies": {
49
+ "@capacitor/android": "^7.0.0",
50
+ "@capacitor/core": "^7.0.0",
51
+ "@capacitor/docgen": "^0.3.0",
52
+ "@capacitor/ios": "^7.0.0",
53
+ "@ionic/eslint-config": "^0.4.0",
54
+ "@ionic/prettier-config": "^4.0.0",
55
+ "@ionic/swiftlint-config": "^2.0.0",
56
+ "eslint": "^8.57.0",
57
+ "prettier": "^3.4.2",
58
+ "prettier-plugin-java": "^2.6.6",
59
+ "rimraf": "^6.0.1",
60
+ "rollup": "^4.30.1",
61
+ "swiftlint": "^2.0.0",
62
+ "typescript": "~4.1.5"
63
+ },
64
+ "peerDependencies": {
65
+ "@capacitor/core": ">=7.0.0"
66
+ },
67
+ "prettier": "@ionic/prettier-config",
68
+ "swiftlint": "@ionic/swiftlint-config",
69
+ "eslintConfig": {
70
+ "extends": "@ionic/eslint-config/recommended"
71
+ },
72
+ "capacitor": {
73
+ "ios": {
74
+ "src": "ios"
75
+ },
76
+ "android": {
77
+ "src": "android"
78
+ }
79
+ }
80
+ }