UNPKG

1.65 kBJavaScriptView Raw
1'use strict';
2const utils = require('handlebars-utils');
3const common = require('./common');
4const SafeString = require('handlebars').SafeString;
5
6const srcsets = {
7 '80w': '80w',
8 '160w': '160w',
9 '320w': '320w',
10 '640w': '640w',
11 '960w': '960w',
12 '1280w': '1280w',
13 '1920w': '1920w',
14 '2560w': '2560w',
15};
16
17function getObjectStorageImage(cdnUrl, source, path, options) {
18 if (!utils.isString (path) || common.isValidURL(path)) {
19 throw new TypeError("Invalid image path - please use a filename or folder path starting from the appropriate folder");
20 }
21
22 // Return original image if there are no arguments
23 let size = 'original';
24
25 if (Number.isInteger(options.hash['width'])) {
26 if (Number.isInteger(options.hash['height'])) {
27 // Return image of specified size
28 size = `${options.hash['width']}x${options.hash['height']}`
29 } else {
30 // Return image of specified width
31 size = `${options.hash['width']}w`
32 }
33 }
34
35 return new SafeString(`${cdnUrl}/images/stencil/${size}/${source}/${path}`);
36}
37
38function getObjectStorageImageSrcset(cdnUrl, source, path) {
39 if (!utils.isString (path) || common.isValidURL(path)) {
40 throw new TypeError("Invalid image path - please use a filename or folder path starting from the appropriate folder");
41 }
42
43 return new SafeString(Object.keys(srcsets).map(descriptor => {
44 return ([`${cdnUrl}/images/stencil/${srcsets[descriptor]}/${source}/${path} ${descriptor}`].join(' '));
45 }).join(', '));
46}
47
48module.exports = {
49 getObjectStorageImage,
50 getObjectStorageImageSrcset
51};