1 | const _ = require('lodash');
|
2 | exports.config = require("./config");
|
3 | exports.utils = require("./utils");
|
4 | exports.uploader = require("./uploader");
|
5 | exports.api = require("./api");
|
6 | let account = require("./provisioning/account");
|
7 |
|
8 | exports.provisioning = {
|
9 | account: account
|
10 | };
|
11 | exports.PreloadedFile = require("./preloaded_file");
|
12 | exports.Cache = require('./cache');
|
13 |
|
14 | const cloudinary = module.exports;
|
15 |
|
16 | const optionConsume = cloudinary.utils.option_consume;
|
17 |
|
18 | exports.url = function url(public_id, options) {
|
19 | options = _.extend({}, options);
|
20 | return cloudinary.utils.url(public_id, options);
|
21 | };
|
22 |
|
23 | const { generateImageResponsiveAttributes, generateMediaAttr } = require('./utils/srcsetUtils');
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function chainTransformations(options, transformation = []) {
|
35 |
|
36 | let urlOptions = cloudinary.utils.extractUrlParams(options);
|
37 | let currentTransformation = cloudinary.utils.extractTransformationParams(options);
|
38 | transformation = cloudinary.utils.build_array(transformation);
|
39 | urlOptions.transformation = [currentTransformation, ...transformation];
|
40 | return urlOptions;
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | exports.image = function image(source, options) {
|
60 | let localOptions = _.extend({}, options);
|
61 | let srcsetParam = optionConsume(localOptions, 'srcset');
|
62 | let attributes = optionConsume(localOptions, 'attributes', {});
|
63 | let src = cloudinary.utils.url(source, localOptions);
|
64 | if ("html_width" in localOptions) localOptions.width = optionConsume(localOptions, "html_width");
|
65 | if ("html_height" in localOptions) localOptions.height = optionConsume(localOptions, "html_height");
|
66 |
|
67 | let client_hints = optionConsume(localOptions, "client_hints", cloudinary.config().client_hints);
|
68 | let responsive = optionConsume(localOptions, "responsive");
|
69 | let hidpi = optionConsume(localOptions, "hidpi");
|
70 |
|
71 | if ((responsive || hidpi) && !client_hints) {
|
72 | localOptions["data-src"] = src;
|
73 | let classes = [responsive ? "cld-responsive" : "cld-hidpi"];
|
74 | let current_class = optionConsume(localOptions, "class");
|
75 | if (current_class) classes.push(current_class);
|
76 | localOptions.class = classes.join(" ");
|
77 | src = optionConsume(localOptions, "responsive_placeholder", cloudinary.config().responsive_placeholder);
|
78 | if (src === "blank") {
|
79 | src = cloudinary.BLANK;
|
80 | }
|
81 | }
|
82 | let html = "<img ";
|
83 | if (src) html += "src='" + src + "' ";
|
84 | let responsiveAttributes = {};
|
85 | if (cloudinary.utils.isString(srcsetParam)) {
|
86 | responsiveAttributes.srcset = srcsetParam;
|
87 | } else {
|
88 | responsiveAttributes = generateImageResponsiveAttributes(source, attributes, srcsetParam, options);
|
89 | }
|
90 | if (!cloudinary.utils.isEmpty(responsiveAttributes)) {
|
91 | delete localOptions.width;
|
92 | delete localOptions.height;
|
93 | }
|
94 | html += cloudinary.utils.html_attrs(_.extend(localOptions, responsiveAttributes, attributes)) + "/>";
|
95 | return html;
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | exports.video = function video(public_id, options) {
|
117 | options = _.extend({}, options);
|
118 | public_id = public_id.replace(/\.(mp4|ogv|webm)$/, '');
|
119 | let source_types = optionConsume(options, 'source_types', []);
|
120 | let source_transformation = optionConsume(options, 'source_transformation', {});
|
121 | let sources = optionConsume(options, 'sources', []);
|
122 | let fallback = optionConsume(options, 'fallback_content', '');
|
123 |
|
124 | if (source_types.length === 0) source_types = cloudinary.utils.DEFAULT_VIDEO_SOURCE_TYPES;
|
125 | let video_options = _.cloneDeep(options);
|
126 |
|
127 | if (video_options.hasOwnProperty('poster')) {
|
128 | if (_.isPlainObject(video_options.poster)) {
|
129 | if (video_options.poster.hasOwnProperty('public_id')) {
|
130 | video_options.poster = cloudinary.utils.url(video_options.poster.public_id, video_options.poster);
|
131 | } else {
|
132 | video_options.poster = cloudinary.utils.url(public_id, _.extend({}, cloudinary.utils.DEFAULT_POSTER_OPTIONS, video_options.poster));
|
133 | }
|
134 | }
|
135 | } else {
|
136 | video_options.poster = cloudinary.utils.url(public_id, _.extend({}, cloudinary.utils.DEFAULT_POSTER_OPTIONS, options));
|
137 | }
|
138 |
|
139 | if (!video_options.poster) delete video_options.poster;
|
140 |
|
141 | let html = '<video ';
|
142 |
|
143 | if (!video_options.hasOwnProperty('resource_type')) video_options.resource_type = 'video';
|
144 | let multi_source_types = _.isArray(source_types) && source_types.length > 1;
|
145 | let has_sources = _.isArray(sources) && sources.length > 0;
|
146 | let source = public_id;
|
147 | if (!multi_source_types && !has_sources) {
|
148 | source = source + '.' + cloudinary.utils.build_array(source_types)[0];
|
149 | }
|
150 | let src = cloudinary.utils.url(source, video_options);
|
151 | if (!multi_source_types && !has_sources) video_options.src = src;
|
152 | if (video_options.hasOwnProperty("html_width")) video_options.width = optionConsume(video_options, 'html_width');
|
153 | if (video_options.hasOwnProperty("html_height")) video_options.height = optionConsume(video_options, 'html_height');
|
154 | html = html + cloudinary.utils.html_attrs(video_options) + '>';
|
155 | if (multi_source_types && !has_sources) {
|
156 | sources = source_types.map(source_type => ({
|
157 | type: source_type,
|
158 | transformations: source_transformation[source_type] || {}
|
159 | }));
|
160 | }
|
161 | if (_.isArray(sources) && sources.length > 0) {
|
162 | html += sources.map((source_data) => {
|
163 | let source_type = source_data.type;
|
164 | let codecs = source_data.codecs;
|
165 | let transformation = source_data.transformations || {};
|
166 | src = cloudinary.utils.url(source + "." + source_type, _.extend({ resource_type: 'video' }, _.cloneDeep(options), _.cloneDeep(transformation)));
|
167 | return cloudinary.utils.create_source_tag(src, source_type, codecs);
|
168 | }).join('');
|
169 | }
|
170 | return `${html}${fallback}</video>`;
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | exports.source = function source(public_id, options = {}) {
|
183 | let srcsetParam = cloudinary.utils.extend({}, options.srcset, cloudinary.config().srcset);
|
184 | let attributes = options.attributes || {};
|
185 |
|
186 | cloudinary.utils.extend(attributes, generateImageResponsiveAttributes(public_id, attributes, srcsetParam, options));
|
187 | if (!attributes.srcset) {
|
188 | attributes.srcset = cloudinary.url(public_id, options);
|
189 | }
|
190 | if (!attributes.media && options.media) {
|
191 | attributes.media = generateMediaAttr(options.media);
|
192 | }
|
193 | return `<source ${cloudinary.utils.html_attrs(attributes)}>`;
|
194 | };
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | exports.picture = function picture(public_id, options = {}) {
|
218 | let sources = options.sources || [];
|
219 | options = cloudinary.utils.clone(options);
|
220 | delete options.sources;
|
221 | cloudinary.utils.patchFetchFormat(options);
|
222 | return "<picture>"
|
223 | + sources.map((source) => {
|
224 | let sourceOptions = chainTransformations(options, source.transformation);
|
225 | sourceOptions.media = source;
|
226 | return cloudinary.source(public_id, sourceOptions);
|
227 | }).join('')
|
228 | + cloudinary.image(public_id, options)
|
229 | + "</picture>";
|
230 | };
|
231 |
|
232 | exports.cloudinary_js_config = cloudinary.utils.cloudinary_js_config;
|
233 | exports.CF_SHARED_CDN = cloudinary.utils.CF_SHARED_CDN;
|
234 | exports.AKAMAI_SHARED_CDN = cloudinary.utils.AKAMAI_SHARED_CDN;
|
235 | exports.SHARED_CDN = cloudinary.utils.SHARED_CDN;
|
236 | exports.BLANK = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
|
237 | exports.v2 = require('./v2');
|