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