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