UNPKG

10.2 kBJavaScriptView Raw
1const _ = require('lodash');
2exports.config = require("./config");
3exports.utils = require("./utils");
4exports.uploader = require("./uploader");
5exports.api = require("./api");
6exports.PreloadedFile = require("./preloaded_file");
7exports.Cache = require('./cache');
8
9const cloudinary = module.exports;
10
11const optionConsume = cloudinary.utils.option_consume;
12
13exports.url = function url(public_id, options) {
14 options = _.extend({}, options);
15 return cloudinary.utils.url(public_id, options);
16};
17
18const { generateImageResponsiveAttributes, generateMediaAttr } = require('./utils/srcsetUtils');
19
20/**
21 * Helper function, allows chaining transformation to the end of transformation list
22 *
23 * @private
24 * @param {object} options Original options
25 * @param {object|object[]} transformation Transformations to chain at the end
26 *
27 * @return {object} Resulting options
28 */
29function chainTransformations(options, transformation = []) {
30 // preserve url options
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 * Generate an HTML img tag with a Cloudinary URL
40 * @param {string} source A Public ID or a URL
41 * @param {object} options Configuration options
42 * @param {srcset} options.srcset srcset options
43 * @param {object} options.attributes HTML attributes
44 * @param {number} options.html_width (deprecated) The HTML tag width
45 * @param {number} options.html_height (deprecated) The HTML tag height
46 * @param {boolean} options.client_hints Don't implement the client side responsive function.
47 * This argument can override the the same option in the global configuration.
48 * @param {boolean} options.responsive Setup the tag for the client side responsive function.
49 * @param {boolean} options.hidpi Setup the tag for the client side auto dpr function.
50 * @param {boolean} options.responsive_placeholder A place holder image URL to use with.
51 * the client side responsive function
52 * @return {string} An HTML img tag
53 */
54exports.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 * Creates an HTML video tag for the provided public_id
95 * @param {String} public_id the resource public ID
96 * @param {Object} [options] options for the resource and HTML tag
97 * @param {(String|Array<String>)} [options.source_types] Specify which
98 * source type the tag should include. defaults to webm, mp4 and ogv.
99 * @param {String} [options.source_transformation] specific transformations
100 * to use for a specific source type.
101 * @param {(String|Object)} [options.poster] image URL or
102 * poster options that may include a <tt>public_id</tt> key and
103 * poster-specific transformations
104 * @example <caption>Example of generating a video tag:</caption>
105 * cloudinary.video("mymovie.mp4");
106 * cloudinary.video("mymovie.mp4", {source_types: 'webm'});
107 * cloudinary.video("mymovie.ogv", {poster: "myspecialplaceholder.jpg"});
108 * cloudinary.video("mymovie.webm", {source_types: ['webm', 'mp4'], poster: {effect: 'sepia'}});
109 * @return {string} HTML video tag
110 */
111exports.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); // calculate src and reduce 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 * Generate a <code>source</code> tag.
178 * @param {string} public_id
179 * @param {object} options
180 * @param {srcset} options.srcset arguments required to generate the srcset attribute.
181 * @param {object} options.attributes HTML tag attributes
182 * @return {string}
183 */
184exports.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 * Generate a <code>picture</code> HTML tag.<br>
200 * The sources argument defines different transformations to apply for each
201 * media query.
202 * @param {string}public_id
203 * @param {object} options
204 * @param {object[]} options.sources a list of source arguments. A source tag will be rendered for each item
205 * @param {number} options.sources.min_width a minimum width query
206 * @param {number} options.sources.max_width a maximum width query
207 * @param {number} options.sources.transformation the transformation to apply to the source tag.
208 * @return {string} A picture HTML tag
209 * @example
210 *
211 * cloudinary.picture("sample", {
212 * sources: [
213 * {min_width: 1600, transformation: {crop: 'fill', width: 800, aspect_ratio: 2}},
214 * {min_width: 500, transformation: {crop: 'fill', width: 600, aspect_ratio: 2.3}},
215 * {transformation: {crop: 'crop', width: 400, gravity: 'auto'}},
216 * ]}
217 * );
218 */
219exports.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
234exports.cloudinary_js_config = cloudinary.utils.cloudinary_js_config;
235exports.CF_SHARED_CDN = cloudinary.utils.CF_SHARED_CDN;
236exports.AKAMAI_SHARED_CDN = cloudinary.utils.AKAMAI_SHARED_CDN;
237exports.SHARED_CDN = cloudinary.utils.SHARED_CDN;
238exports.BLANK = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
239exports.v2 = require('./v2');