-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgrodeo.js
More file actions
98 lines (71 loc) · 2.78 KB
/
imgrodeo.js
File metadata and controls
98 lines (71 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express'),
webtask = require('webtask-tools'),
request = require('request'),
cloudinary = require('cloudinary');
const app = express();
const randomItem = ( function( array ) {
const randomIndex = Math.floor( Math.random() * array.length );
return array[ randomIndex ];
} );
const addTransformations = function( cldUrl, transformations ) {
return cldUrl.replace( /(v\d{10})/, transformations + '/$1' );
}
app.get('*?/:size', function ( req, res ) {
const s = req.params.size.split( 'x' );
const width = s[ 0 ];
const height = s[ 1 ];
let transformations = req.params[0];
if ( transformations && transformations.charAt( 0 ) === '/' ) {
transformations = transformations.slice( 1 ); // remove leading /
}
const folder = req.query.folder;
const defaultColors = [ 'rgb:56cbb7', 'rgb:5ac8ce', 'rgb:6dc3e2', 'rgb:85bcf0', 'rgb:9fb3f7', 'rgb:b7aaf8', 'rgb:cca1f0', 'rgb:de99e1', 'rgb:ed93ca', 'rgb:f690ad', 'rgb:f9928d', 'rgb:f59770', 'rgb:e9a15a', 'rgb:d6ac50', 'rgb:bcb856', 'rgb:9ec168', 'rgb:80c881', 'rgb:65cb9c' ];
const colors = ( req.query.colors ? req.query.colors.split( ',' ) : defaultColors );
const getUrl = ( function() {
return new Promise( function( resolve, reject ) {
if ( folder ) {
cloudinary.config({
"cloud_name": req.webtaskContext.secrets.CL_NAME,
"api_key": req.webtaskContext.secrets.CL_KEY,
"api_secret": req.webtaskContext.secrets.CL_SECRET
});
cloudinary.v2.api.resources( { type: 'upload', prefix: folder + '/', max_results: 500 }, function( error, result ) {
// res.send(result);
const randomResource = randomItem( result.resources );
const originalUrl = randomResource.secure_url;
resolve(
addTransformations(
originalUrl,
`c_fill,g_auto,w_${ width },h_${ height },q_auto`
)
);
} );
} else { // use a random color
const oneTransparentPixel = 'https://eric-cloudinary-res.cloudinary.com/image/upload/v1520024646/_.png';
const textSize = Math.round( parseInt( width ) / 10 );
// console.log({ textSize });
// const strokeSize = Math.round( textSize / 10 );
resolve(
addTransformations(
oneTransparentPixel,
`b_${ randomItem( colors ) },w_${ width },h_${ height }/$w_w,$h_h,co_rgb:00000080,l_text:Roboto_${ textSize }:$(w)${ encodeURIComponent( ' × ' ) }$(h)`
)
);
}
} );
} );
getUrl().then( ( url ) => {
let theUrl;
if ( transformations ) {
theUrl = addTransformations( url, transformations );
} else {
theUrl = url;
}
// don't cache
res.header( "Cache-Control", "no-cache, no-store, must-revalidate" );
// res.send( theUrl );
// console.log({ theUrl });
request( theUrl ).pipe( res );
} );
} );
module.exports = webtask.fromExpress(app);