1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const url = require('url');
|
10 | const extend = require("lodash/extend");
|
11 | const isObject = require("lodash/isObject");
|
12 | const isString = require("lodash/isString");
|
13 | const isUndefined = require("lodash/isUndefined");
|
14 | const isEmpty = require("lodash/isEmpty");
|
15 | const entries = require('./utils/entries');
|
16 |
|
17 | let cloudinary_config = void 0;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function putNestedValue(params, key, value) {
|
32 | let chain = key.split(/[\[\]]+/).filter(i => i.length);
|
33 | let outer = params;
|
34 | let lastKey = chain.pop();
|
35 | for (let j = 0; j < chain.length; j++) {
|
36 | let innerKey = chain[j];
|
37 | let inner = outer[innerKey];
|
38 | if (inner == null) {
|
39 | inner = {};
|
40 | outer[innerKey] = inner;
|
41 | }
|
42 | outer = inner;
|
43 | }
|
44 | outer[lastKey] = value;
|
45 | return params;
|
46 | }
|
47 |
|
48 | function parseCloudinaryConfigFromEnvURL(ENV_STR) {
|
49 | let conf = {};
|
50 |
|
51 | let uri = url.parse(ENV_STR, true);
|
52 |
|
53 | if (uri.protocol === 'cloudinary:') {
|
54 | conf = Object.assign({}, conf, {
|
55 | cloud_name: uri.host,
|
56 | api_key: uri.auth && uri.auth.split(":")[0],
|
57 | api_secret: uri.auth && uri.auth.split(":")[1],
|
58 | private_cdn: uri.pathname != null,
|
59 | secure_distribution: uri.pathname && uri.pathname.substring(1)
|
60 | });
|
61 | } else if (uri.protocol === 'account:') {
|
62 | conf = Object.assign({}, conf, {
|
63 | account_id: uri.host,
|
64 | provisioning_api_key: uri.auth && uri.auth.split(":")[0],
|
65 | provisioning_api_secret: uri.auth && uri.auth.split(":")[1]
|
66 | });
|
67 | }
|
68 |
|
69 | return conf;
|
70 | }
|
71 |
|
72 | function extendCloudinaryConfigFromQuery(ENV_URL, confToExtend = {}) {
|
73 | let uri = url.parse(ENV_URL, true);
|
74 | if (uri.query != null) {
|
75 | entries(uri.query).forEach(([key, value]) => putNestedValue(confToExtend, key, value));
|
76 | }
|
77 | }
|
78 |
|
79 | function extendCloudinaryConfig(parsedConfig, confToExtend = {}) {
|
80 | entries(parsedConfig).forEach(([key, value]) => {
|
81 | if (value !== undefined) {
|
82 | confToExtend[key] = value;
|
83 | }
|
84 | });
|
85 |
|
86 | return confToExtend;
|
87 | }
|
88 |
|
89 | module.exports = function (new_config, new_value) {
|
90 | if ((cloudinary_config == null) || new_config === true) {
|
91 | if (cloudinary_config == null) {
|
92 | cloudinary_config = {};
|
93 | } else {
|
94 | Object.keys(cloudinary_config).forEach(key => delete cloudinary_config[key]);
|
95 | }
|
96 |
|
97 | let CLOUDINARY_ENV_URL = process.env.CLOUDINARY_URL;
|
98 | let CLOUDINARY_ENV_ACCOUNT_URL = process.env.CLOUDINARY_ACCOUNT_URL;
|
99 | let CLOUDINARY_API_PROXY = process.env.CLOUDINARY_API_PROXY;
|
100 |
|
101 | if (CLOUDINARY_ENV_URL && !CLOUDINARY_ENV_URL.toLowerCase().startsWith('cloudinary://')) {
|
102 | throw new Error("Invalid CLOUDINARY_URL protocol. URL should begin with 'cloudinary://'");
|
103 | }
|
104 | if (CLOUDINARY_ENV_ACCOUNT_URL && !CLOUDINARY_ENV_ACCOUNT_URL.toLowerCase().startsWith('account://')) {
|
105 | throw new Error("Invalid CLOUDINARY_ACCOUNT_URL protocol. URL should begin with 'account://'");
|
106 | }
|
107 | if (!isEmpty(CLOUDINARY_API_PROXY)) {
|
108 | extendCloudinaryConfig({ api_proxy: CLOUDINARY_API_PROXY }, cloudinary_config);
|
109 | }
|
110 |
|
111 | [CLOUDINARY_ENV_URL, CLOUDINARY_ENV_ACCOUNT_URL].forEach((ENV_URL) => {
|
112 | if (ENV_URL) {
|
113 | let parsedConfig = parseCloudinaryConfigFromEnvURL(ENV_URL);
|
114 | extendCloudinaryConfig(parsedConfig, cloudinary_config);
|
115 |
|
116 |
|
117 | extendCloudinaryConfigFromQuery(ENV_URL, cloudinary_config);
|
118 | }
|
119 | });
|
120 | }
|
121 | if (!isUndefined(new_value)) {
|
122 | cloudinary_config[new_config] = new_value;
|
123 | } else if (isString(new_config)) {
|
124 | return cloudinary_config[new_config];
|
125 | } else if (isObject(new_config)) {
|
126 | extend(cloudinary_config, new_config);
|
127 | }
|
128 | return cloudinary_config;
|
129 | };
|