1 | 'use strict';
|
2 | const utils = require('handlebars-utils');
|
3 | const common = require('./common');
|
4 | const SafeString = require('handlebars').SafeString;
|
5 |
|
6 | const 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 |
|
17 | function 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 |
|
23 | let size = 'original';
|
24 |
|
25 | if (Number.isInteger(options.hash['width'])) {
|
26 | if (Number.isInteger(options.hash['height'])) {
|
27 |
|
28 | size = `${options.hash['width']}x${options.hash['height']}`
|
29 | } else {
|
30 |
|
31 | size = `${options.hash['width']}w`
|
32 | }
|
33 | }
|
34 |
|
35 | return new SafeString(`${cdnUrl}/images/stencil/${size}/${source}/${path}`);
|
36 | }
|
37 |
|
38 | function 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 |
|
48 | module.exports = {
|
49 | getObjectStorageImage,
|
50 | getObjectStorageImageSrcset
|
51 | };
|