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