@zenith-open/zenithcms-plugin-cloudinary 1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aman T Shekar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ import { ZenithPlugin } from '@zenith-open/zenithcms-types';
2
+ export interface CloudinaryConfig {
3
+ cloudName?: string;
4
+ apiKey?: string;
5
+ apiSecret?: string;
6
+ }
7
+ export declare const cloudinaryPlugin: (config?: CloudinaryConfig) => ZenithPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cloudinaryPlugin = void 0;
4
+ const cloudinary_1 = require("cloudinary");
5
+ const cloudinaryPlugin = (config) => {
6
+ return {
7
+ id: 'cloudinary-storage',
8
+ name: 'Cloudinary Storage',
9
+ description: 'Handles media uploads and delivery via Cloudinary.',
10
+ apply: () => { },
11
+ onReady: async (ctx) => {
12
+ const resolveConfig = async (overrideSettings, siteId) => {
13
+ const activeConfig = {
14
+ cloudName: process.env.CLOUDINARY_CLOUD_NAME || config?.cloudName,
15
+ apiKey: process.env.CLOUDINARY_API_KEY || config?.apiKey,
16
+ apiSecret: process.env.CLOUDINARY_API_SECRET || config?.apiSecret
17
+ };
18
+ if (overrideSettings) {
19
+ if (overrideSettings.cloudinaryCloudName)
20
+ activeConfig.cloudName = overrideSettings.cloudinaryCloudName;
21
+ if (overrideSettings.cloudinaryApiKey)
22
+ activeConfig.apiKey = overrideSettings.cloudinaryApiKey;
23
+ if (overrideSettings.cloudinaryApiSecret && overrideSettings.cloudinaryApiSecret !== '[MASKED_CREDENTIAL]')
24
+ activeConfig.apiSecret = overrideSettings.cloudinaryApiSecret;
25
+ return activeConfig;
26
+ }
27
+ try {
28
+ const adapter = ctx.adapter;
29
+ if (adapter && adapter.findOne) {
30
+ const query = siteId ? { siteId } : {};
31
+ const settings = await adapter.findOne('z_settings', query);
32
+ if (settings) {
33
+ if (settings.cloudinaryCloudName)
34
+ activeConfig.cloudName = settings.cloudinaryCloudName;
35
+ if (settings.cloudinaryApiKey)
36
+ activeConfig.apiKey = settings.cloudinaryApiKey;
37
+ if (settings.cloudinaryApiSecret && settings.cloudinaryApiSecret !== '[MASKED_CREDENTIAL]')
38
+ activeConfig.apiSecret = settings.cloudinaryApiSecret;
39
+ }
40
+ }
41
+ }
42
+ catch (e) {
43
+ ctx.logger.warn('[Cloudinary Plugin] Failed to load config from database settings');
44
+ }
45
+ return activeConfig;
46
+ };
47
+ const mediaAdapter = {
48
+ async testConnection(overrideSettings, siteId) {
49
+ const activeConfig = await resolveConfig(overrideSettings, siteId);
50
+ if (!activeConfig.cloudName || !activeConfig.apiKey || !activeConfig.apiSecret) {
51
+ throw new Error('Cloudinary credentials missing');
52
+ }
53
+ cloudinary_1.v2.config({
54
+ cloud_name: activeConfig.cloudName,
55
+ api_key: activeConfig.apiKey,
56
+ api_secret: activeConfig.apiSecret
57
+ });
58
+ const result = await cloudinary_1.v2.api.ping();
59
+ return result.status === 'ok';
60
+ },
61
+ async uploadFile(fileInput, options = {}) {
62
+ const activeConfig = await resolveConfig(undefined, options.siteId);
63
+ if (!activeConfig.cloudName || !activeConfig.apiKey || !activeConfig.apiSecret) {
64
+ throw new Error('Cloudinary service is not configured');
65
+ }
66
+ cloudinary_1.v2.config({
67
+ cloud_name: activeConfig.cloudName,
68
+ api_key: activeConfig.apiKey,
69
+ api_secret: activeConfig.apiSecret
70
+ });
71
+ const sanitizedFilename = options.filename
72
+ ? options.filename.replace(/[^a-zA-Z0-9.-]/g, '-')
73
+ : undefined;
74
+ const uploadOptions = {
75
+ folder: options.folder || 'zenithcms',
76
+ public_id: sanitizedFilename ? `${Date.now()}-${sanitizedFilename}` : undefined,
77
+ resource_type: 'auto',
78
+ };
79
+ if (typeof fileInput === 'string') {
80
+ return await cloudinary_1.v2.uploader.upload(fileInput, uploadOptions);
81
+ }
82
+ else {
83
+ return new Promise((resolve, reject) => {
84
+ const stream = cloudinary_1.v2.uploader.upload_stream(uploadOptions, (error, result) => {
85
+ if (error || !result)
86
+ return reject(error || new Error('Upload failed'));
87
+ resolve(result);
88
+ });
89
+ stream.end(fileInput);
90
+ });
91
+ }
92
+ },
93
+ async deleteFile(publicId, options = {}) {
94
+ const activeConfig = await resolveConfig(undefined, options.siteId);
95
+ if (!activeConfig.cloudName || !activeConfig.apiKey || !activeConfig.apiSecret) {
96
+ ctx.logger.warn('[Cloudinary Plugin] Skipping file deletion: credentials missing');
97
+ return;
98
+ }
99
+ cloudinary_1.v2.config({
100
+ cloud_name: activeConfig.cloudName,
101
+ api_key: activeConfig.apiKey,
102
+ api_secret: activeConfig.apiSecret
103
+ });
104
+ await cloudinary_1.v2.uploader.destroy(publicId);
105
+ }
106
+ };
107
+ // @ts-ignore - Dynamically registering to core MediaService if it's available via global or injection
108
+ // The Zenith engine uses global registry for media adapters
109
+ const core = require('@zenith-open/zenithcms-core');
110
+ if (core && core.MediaService) {
111
+ core.MediaService.registerAdapter(mediaAdapter);
112
+ ctx.logger.info('[Cloudinary Plugin] Successfully registered with Core MediaService.');
113
+ }
114
+ else {
115
+ ctx.logger.error('[Cloudinary Plugin] Could not find core MediaService to register.');
116
+ }
117
+ }
118
+ };
119
+ };
120
+ exports.cloudinaryPlugin = cloudinaryPlugin;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@zenith-open/zenithcms-plugin-cloudinary",
3
+ "version": "1.0.0",
4
+ "description": "Cloudinary Media Plugin for Zenith CMS",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "dependencies": {
8
+ "cloudinary": "^2.5.0"
9
+ },
10
+ "devDependencies": {
11
+ "typescript": "^5.4.5"
12
+ },
13
+ "peerDependencies": {
14
+ "@zenith-open/zenithcms-core": "1.0.0-beta.1",
15
+ "@zenith-open/zenithcms-types": "1.0.0-beta.1"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsc"
26
+ }
27
+ }