UNPKG

10.1 kBJavaScriptView Raw
1const _ = require('lodash');
2const cloudinary = module.exports;
3exports.config = require("./config");
4exports.utils = require("./utils");
5exports.uploader = require("./uploader");
6exports.api = require("./api");
7exports.PreloadedFile = require("./preloaded_file");
8exports.Cache = require('./cache');
9
10const optionConsume = cloudinary.utils.option_consume;
11const ensureOption = require('./utils/ensureOption').defaults(cloudinary.config());
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{
31 // preserve url options
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 * Generate an HTML img tag with a Cloudinary URL
41 * @param {string} source A Public ID or a URL
42 * @param {object} options Configuration options
43 * @param {srcset} options.srcset srcset options
44 * @param {object} options.attributes HTML attributes
45 * @param {number} options.html_width (deprecated) The HTML tag width
46 * @param {number} options.html_height (deprecated) The HTML tag height
47 * @param {boolean} options.client_hints Don't implement the client side responsive function.
48 * This argument can override the the same option in the global configuration.
49 * @param {boolean} options.responsive Setup the tag for the client side responsive function.
50 * @param {boolean} options.hidpi Setup the tag for the client side auto dpr function.
51 * @param {boolean} options.responsive_placeholder A place holder image URL to use with.
52 * the client side responsive function
53 * @return {string} An HTML img tag
54 */
55exports.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 * Creates an HTML video tag for the provided public_id
96 * @param {String} public_id the resource public ID
97 * @param {Object} [options] options for the resource and HTML tag
98 * @param {(String|Array<String>)} [options.source_types] Specify which
99 * source type the tag should include. defaults to webm, mp4 and ogv.
100 * @param {String} [options.source_transformation] specific transformations
101 * to use for a specific source type.
102 * @param {(String|Object)} [options.poster] image URL or
103 * poster options that may include a <tt>public_id</tt> key and
104 * poster-specific transformations
105 * @example <caption>Example of generating a video tag:</caption>
106 * cloudinary.video("mymovie.mp4");
107 * cloudinary.video("mymovie.mp4", {source_types: 'webm'});
108 * cloudinary.video("mymovie.ogv", {poster: "myspecialplaceholder.jpg"});
109 * cloudinary.video("mymovie.webm", {source_types: ['webm', 'mp4'], poster: {effect: 'sepia'}});
110 * @return {string} HTML video tag
111 */
112exports.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 * Generate a <code>source</code> tag.
166 * @param {string} public_id
167 * @param {object} options
168 * @param {srcset} options.srcset arguments required to generate the srcset attribute.
169 * @param {object} options.attributes HTML tag attributes
170 * @return {string}
171 */
172exports.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 * Generate a <code>picture</code> HTML tag.<br>
188 * The sources argument defines different transformations to apply for each
189 * media query.
190 * @param {string}public_id
191 * @param {object} options
192 * @param {object[]} options.sources a list of source arguments. A source tag will be rendered for each item
193 * @param {number} options.sources.min_width a minimum width query
194 * @param {number} options.sources.max_width a maximum width query
195 * @param {number} options.sources.transformation the transformation to apply to the source tag.
196 * @return {string} A picture HTML tag
197 * @example
198 *
199 * cloudinary.picture("sample", {
200 * sources: [
201 * {min_width: 1600, transformation: {crop: 'fill', width: 800, aspect_ratio: 2}},
202 * {min_width: 500, transformation: {crop: 'fill', width: 600, aspect_ratio: 2.3}},
203 * {transformation: {crop: 'crop', width: 400, gravity: 'auto'}},
204 * ]}
205 * );
206 */
207exports.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
222exports.cloudinary_js_config = cloudinary.utils.cloudinary_js_config;
223exports.CF_SHARED_CDN = cloudinary.utils.CF_SHARED_CDN;
224exports.AKAMAI_SHARED_CDN = cloudinary.utils.AKAMAI_SHARED_CDN;
225exports.SHARED_CDN = cloudinary.utils.SHARED_CDN;
226exports.BLANK = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
227exports.v2 = require('./v2');