UNPKG

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