1 | "use strict";
|
2 |
|
3 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var url = require('url');
|
14 | var extend = require("lodash/extend");
|
15 | var isObject = require("lodash/isObject");
|
16 | var isString = require("lodash/isString");
|
17 | var isUndefined = require("lodash/isUndefined");
|
18 | var isEmpty = require("lodash/isEmpty");
|
19 | var entries = require('./utils/entries');
|
20 |
|
21 | var cloudinary_config = void 0;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function putNestedValue(params, key, value) {
|
36 | var chain = key.split(/[\[\]]+/).filter(function (i) {
|
37 | return i.length;
|
38 | });
|
39 | var outer = params;
|
40 | var lastKey = chain.pop();
|
41 | for (var j = 0; j < chain.length; j++) {
|
42 | var innerKey = chain[j];
|
43 | var inner = outer[innerKey];
|
44 | if (inner == null) {
|
45 | inner = {};
|
46 | outer[innerKey] = inner;
|
47 | }
|
48 | outer = inner;
|
49 | }
|
50 | outer[lastKey] = value;
|
51 | return params;
|
52 | }
|
53 |
|
54 | function parseCloudinaryConfigFromEnvURL(ENV_STR) {
|
55 | var conf = {};
|
56 |
|
57 | var uri = url.parse(ENV_STR, true);
|
58 |
|
59 | if (uri.protocol === 'cloudinary:') {
|
60 | conf = Object.assign({}, conf, {
|
61 | cloud_name: uri.host,
|
62 | api_key: uri.auth && uri.auth.split(":")[0],
|
63 | api_secret: uri.auth && uri.auth.split(":")[1],
|
64 | private_cdn: uri.pathname != null,
|
65 | secure_distribution: uri.pathname && uri.pathname.substring(1)
|
66 | });
|
67 | } else if (uri.protocol === 'account:') {
|
68 | conf = Object.assign({}, conf, {
|
69 | account_id: uri.host,
|
70 | provisioning_api_key: uri.auth && uri.auth.split(":")[0],
|
71 | provisioning_api_secret: uri.auth && uri.auth.split(":")[1]
|
72 | });
|
73 | }
|
74 |
|
75 | return conf;
|
76 | }
|
77 |
|
78 | function extendCloudinaryConfigFromQuery(ENV_URL) {
|
79 | var confToExtend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
80 |
|
81 | var uri = url.parse(ENV_URL, true);
|
82 | if (uri.query != null) {
|
83 | entries(uri.query).forEach(function (_ref) {
|
84 | var _ref2 = _slicedToArray(_ref, 2),
|
85 | key = _ref2[0],
|
86 | value = _ref2[1];
|
87 |
|
88 | return putNestedValue(confToExtend, key, value);
|
89 | });
|
90 | }
|
91 | }
|
92 |
|
93 | function extendCloudinaryConfig(parsedConfig) {
|
94 | var confToExtend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
95 |
|
96 | entries(parsedConfig).forEach(function (_ref3) {
|
97 | var _ref4 = _slicedToArray(_ref3, 2),
|
98 | key = _ref4[0],
|
99 | value = _ref4[1];
|
100 |
|
101 | if (value !== undefined) {
|
102 | confToExtend[key] = value;
|
103 | }
|
104 | });
|
105 |
|
106 | return confToExtend;
|
107 | }
|
108 |
|
109 | module.exports = function (new_config, new_value) {
|
110 | if (cloudinary_config == null || new_config === true) {
|
111 | if (cloudinary_config == null) {
|
112 | cloudinary_config = {};
|
113 | } else {
|
114 | Object.keys(cloudinary_config).forEach(function (key) {
|
115 | return delete cloudinary_config[key];
|
116 | });
|
117 | }
|
118 |
|
119 | var CLOUDINARY_ENV_URL = process.env.CLOUDINARY_URL;
|
120 | var CLOUDINARY_ENV_ACCOUNT_URL = process.env.CLOUDINARY_ACCOUNT_URL;
|
121 | var CLOUDINARY_API_PROXY = process.env.CLOUDINARY_API_PROXY;
|
122 |
|
123 | if (CLOUDINARY_ENV_URL && !CLOUDINARY_ENV_URL.toLowerCase().startsWith('cloudinary://')) {
|
124 | throw new Error("Invalid CLOUDINARY_URL protocol. URL should begin with 'cloudinary://'");
|
125 | }
|
126 | if (CLOUDINARY_ENV_ACCOUNT_URL && !CLOUDINARY_ENV_ACCOUNT_URL.toLowerCase().startsWith('account://')) {
|
127 | throw new Error("Invalid CLOUDINARY_ACCOUNT_URL protocol. URL should begin with 'account://'");
|
128 | }
|
129 | if (!isEmpty(CLOUDINARY_API_PROXY)) {
|
130 | extendCloudinaryConfig({ api_proxy: CLOUDINARY_API_PROXY }, cloudinary_config);
|
131 | }
|
132 |
|
133 | [CLOUDINARY_ENV_URL, CLOUDINARY_ENV_ACCOUNT_URL].forEach(function (ENV_URL) {
|
134 | if (ENV_URL) {
|
135 | var parsedConfig = parseCloudinaryConfigFromEnvURL(ENV_URL);
|
136 | extendCloudinaryConfig(parsedConfig, cloudinary_config);
|
137 |
|
138 |
|
139 | extendCloudinaryConfigFromQuery(ENV_URL, cloudinary_config);
|
140 | }
|
141 | });
|
142 | }
|
143 | if (!isUndefined(new_value)) {
|
144 | cloudinary_config[new_config] = new_value;
|
145 | } else if (isString(new_config)) {
|
146 | return cloudinary_config[new_config];
|
147 | } else if (isObject(new_config)) {
|
148 | extend(cloudinary_config, new_config);
|
149 | }
|
150 | return cloudinary_config;
|
151 | }; |
\ | No newline at end of file |