UNPKG

11 kBJavaScriptView Raw
1"use strict";
2
3function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
4
5var _ = require('lodash');
6exports.config = require("./config");
7exports.utils = require("./utils");
8exports.uploader = require("./uploader");
9exports.api = require("./api");
10var account = require("./provisioning/account");
11
12exports.provisioning = {
13 account: account
14};
15exports.PreloadedFile = require("./preloaded_file");
16exports.Cache = require('./cache');
17
18var cloudinary = module.exports;
19
20var optionConsume = cloudinary.utils.option_consume;
21
22exports.url = function url(public_id, options) {
23 options = _.extend({}, options);
24 return cloudinary.utils.url(public_id, options);
25};
26
27var _require = require('./utils/srcsetUtils'),
28 generateImageResponsiveAttributes = _require.generateImageResponsiveAttributes,
29 generateMediaAttr = _require.generateMediaAttr;
30
31/**
32 * Helper function, allows chaining transformation to the end of transformation list
33 *
34 * @private
35 * @param {object} options Original options
36 * @param {object|object[]} transformation Transformations to chain at the end
37 *
38 * @return {object} Resulting options
39 */
40
41
42function chainTransformations(options) {
43 var transformation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
44
45 // preserve url options
46 var urlOptions = cloudinary.utils.extractUrlParams(options);
47 var currentTransformation = cloudinary.utils.extractTransformationParams(options);
48 transformation = cloudinary.utils.build_array(transformation);
49 urlOptions.transformation = [currentTransformation].concat(_toConsumableArray(transformation));
50 return urlOptions;
51}
52
53/**
54 * Generate an HTML img tag with a Cloudinary URL
55 * @param {string} source A Public ID or a URL
56 * @param {object} options Configuration options
57 * @param {srcset} options.srcset srcset options
58 * @param {object} options.attributes HTML attributes
59 * @param {number} options.html_width (deprecated) The HTML tag width
60 * @param {number} options.html_height (deprecated) The HTML tag height
61 * @param {boolean} options.client_hints Don't implement the client side responsive function.
62 * This argument can override the the same option in the global configuration.
63 * @param {boolean} options.responsive Setup the tag for the client side responsive function.
64 * @param {boolean} options.hidpi Setup the tag for the client side auto dpr function.
65 * @param {boolean} options.responsive_placeholder A place holder image URL to use with.
66 * the client side responsive function
67 * @return {string} An HTML img tag
68 */
69exports.image = function image(source, options) {
70 var localOptions = _.extend({}, options);
71 var srcsetParam = optionConsume(localOptions, 'srcset');
72 var attributes = optionConsume(localOptions, 'attributes', {});
73 var src = cloudinary.utils.url(source, localOptions);
74 if ("html_width" in localOptions) localOptions.width = optionConsume(localOptions, "html_width");
75 if ("html_height" in localOptions) localOptions.height = optionConsume(localOptions, "html_height");
76
77 var client_hints = optionConsume(localOptions, "client_hints", cloudinary.config().client_hints);
78 var responsive = optionConsume(localOptions, "responsive");
79 var hidpi = optionConsume(localOptions, "hidpi");
80
81 if ((responsive || hidpi) && !client_hints) {
82 localOptions["data-src"] = src;
83 var classes = [responsive ? "cld-responsive" : "cld-hidpi"];
84 var current_class = optionConsume(localOptions, "class");
85 if (current_class) classes.push(current_class);
86 localOptions.class = classes.join(" ");
87 src = optionConsume(localOptions, "responsive_placeholder", cloudinary.config().responsive_placeholder);
88 if (src === "blank") {
89 src = cloudinary.BLANK;
90 }
91 }
92 var html = "<img ";
93 if (src) html += "src='" + src + "' ";
94 var responsiveAttributes = {};
95 if (cloudinary.utils.isString(srcsetParam)) {
96 responsiveAttributes.srcset = srcsetParam;
97 } else {
98 responsiveAttributes = generateImageResponsiveAttributes(source, attributes, srcsetParam, options);
99 }
100 if (!cloudinary.utils.isEmpty(responsiveAttributes)) {
101 delete localOptions.width;
102 delete localOptions.height;
103 }
104 html += cloudinary.utils.html_attrs(_.extend(localOptions, responsiveAttributes, attributes)) + "/>";
105 return html;
106};
107
108/**
109 * Creates an HTML video tag for the provided public_id
110 * @param {String} public_id the resource public ID
111 * @param {Object} [options] options for the resource and HTML tag
112 * @param {(String|Array<String>)} [options.source_types] Specify which
113 * source type the tag should include. defaults to webm, mp4 and ogv.
114 * @param {String} [options.source_transformation] specific transformations
115 * to use for a specific source type.
116 * @param {(String|Object)} [options.poster] image URL or
117 * poster options that may include a <tt>public_id</tt> key and
118 * poster-specific transformations
119 * @example <caption>Example of generating a video tag:</caption>
120 * cloudinary.video("mymovie.mp4");
121 * cloudinary.video("mymovie.mp4", {source_types: 'webm'});
122 * cloudinary.video("mymovie.ogv", {poster: "myspecialplaceholder.jpg"});
123 * cloudinary.video("mymovie.webm", {source_types: ['webm', 'mp4'], poster: {effect: 'sepia'}});
124 * @return {string} HTML video tag
125 */
126exports.video = function video(public_id, options) {
127 options = _.extend({}, options);
128 public_id = public_id.replace(/\.(mp4|ogv|webm)$/, '');
129 var source_types = optionConsume(options, 'source_types', []);
130 var source_transformation = optionConsume(options, 'source_transformation', {});
131 var sources = optionConsume(options, 'sources', []);
132 var fallback = optionConsume(options, 'fallback_content', '');
133
134 if (source_types.length === 0) source_types = cloudinary.utils.DEFAULT_VIDEO_SOURCE_TYPES;
135 var video_options = _.cloneDeep(options);
136
137 if (video_options.hasOwnProperty('poster')) {
138 if (_.isPlainObject(video_options.poster)) {
139 if (video_options.poster.hasOwnProperty('public_id')) {
140 video_options.poster = cloudinary.utils.url(video_options.poster.public_id, video_options.poster);
141 } else {
142 video_options.poster = cloudinary.utils.url(public_id, _.extend({}, cloudinary.utils.DEFAULT_POSTER_OPTIONS, video_options.poster));
143 }
144 }
145 } else {
146 video_options.poster = cloudinary.utils.url(public_id, _.extend({}, cloudinary.utils.DEFAULT_POSTER_OPTIONS, options));
147 }
148
149 if (!video_options.poster) delete video_options.poster;
150
151 var html = '<video ';
152
153 if (!video_options.hasOwnProperty('resource_type')) video_options.resource_type = 'video';
154 var multi_source_types = _.isArray(source_types) && source_types.length > 1;
155 var has_sources = _.isArray(sources) && sources.length > 0;
156 var source = public_id;
157 if (!multi_source_types && !has_sources) {
158 source = source + '.' + cloudinary.utils.build_array(source_types)[0];
159 }
160 var src = cloudinary.utils.url(source, video_options);
161 if (!multi_source_types && !has_sources) video_options.src = src;
162 if (video_options.hasOwnProperty("html_width")) video_options.width = optionConsume(video_options, 'html_width');
163 if (video_options.hasOwnProperty("html_height")) video_options.height = optionConsume(video_options, 'html_height');
164 html = html + cloudinary.utils.html_attrs(video_options) + '>';
165 if (multi_source_types && !has_sources) {
166 sources = source_types.map(function (source_type) {
167 return {
168 type: source_type,
169 transformations: source_transformation[source_type] || {}
170 };
171 });
172 }
173 if (_.isArray(sources) && sources.length > 0) {
174 html += sources.map(function (source_data) {
175 var source_type = source_data.type;
176 var codecs = source_data.codecs;
177 var transformation = source_data.transformations || {};
178 src = cloudinary.utils.url(source + "." + source_type, _.extend({ resource_type: 'video' }, _.cloneDeep(options), _.cloneDeep(transformation)));
179 return cloudinary.utils.create_source_tag(src, source_type, codecs);
180 }).join('');
181 }
182 return `${html}${fallback}</video>`;
183};
184
185/**
186 * Generate a <code>source</code> tag.
187 * @param {string} public_id
188 * @param {object} options
189 * @param {srcset} options.srcset arguments required to generate the srcset attribute.
190 * @param {object} options.attributes HTML tag attributes
191 * @return {string}
192 */
193exports.source = function source(public_id) {
194 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
195
196 var srcsetParam = cloudinary.utils.extend({}, options.srcset, cloudinary.config().srcset);
197 var attributes = options.attributes || {};
198
199 cloudinary.utils.extend(attributes, generateImageResponsiveAttributes(public_id, attributes, srcsetParam, options));
200 if (!attributes.srcset) {
201 attributes.srcset = cloudinary.url(public_id, options);
202 }
203 if (!attributes.media && options.media) {
204 attributes.media = generateMediaAttr(options.media);
205 }
206 return `<source ${cloudinary.utils.html_attrs(attributes)}>`;
207};
208
209/**
210 * Generate a <code>picture</code> HTML tag.<br>
211 * The sources argument defines different transformations to apply for each
212 * media query.
213 * @param {string}public_id
214 * @param {object} options
215 * @param {object[]} options.sources a list of source arguments. A source tag will be rendered for each item
216 * @param {number} options.sources.min_width a minimum width query
217 * @param {number} options.sources.max_width a maximum width query
218 * @param {number} options.sources.transformation the transformation to apply to the source tag.
219 * @return {string} A picture HTML tag
220 * @example
221 *
222 * cloudinary.picture("sample", {
223 * sources: [
224 * {min_width: 1600, transformation: {crop: 'fill', width: 800, aspect_ratio: 2}},
225 * {min_width: 500, transformation: {crop: 'fill', width: 600, aspect_ratio: 2.3}},
226 * {transformation: {crop: 'crop', width: 400, gravity: 'auto'}},
227 * ]}
228 * );
229 */
230exports.picture = function picture(public_id) {
231 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
232
233 var sources = options.sources || [];
234 options = cloudinary.utils.clone(options);
235 delete options.sources;
236 cloudinary.utils.patchFetchFormat(options);
237 return "<picture>" + sources.map(function (source) {
238 var sourceOptions = chainTransformations(options, source.transformation);
239 sourceOptions.media = source;
240 return cloudinary.source(public_id, sourceOptions);
241 }).join('') + cloudinary.image(public_id, options) + "</picture>";
242};
243
244exports.cloudinary_js_config = cloudinary.utils.cloudinary_js_config;
245exports.CF_SHARED_CDN = cloudinary.utils.CF_SHARED_CDN;
246exports.AKAMAI_SHARED_CDN = cloudinary.utils.AKAMAI_SHARED_CDN;
247exports.SHARED_CDN = cloudinary.utils.SHARED_CDN;
248exports.BLANK = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
249exports.v2 = require('./v2');
\No newline at end of file