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