-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (54 loc) · 2.03 KB
/
index.js
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
const express = require('express');
const Avatars = require('@dicebear/avatars').default;
const humanSprites = require('@dicebear/avatars-human-sprites').default;
const avataaarSprites = require('@dicebear/avatars-avataaars-sprites').default;
const aws = require('./aws');
const { convert } = require('convert-svg-to-png');
var app = express();
app.get('/api/v1/:avatar_lib/:width/:seed', function(req, res) {
const options = {};
let sprites = humanSprites;
const showUrl = req.query.show_url;
switch(req.params.avatar_lib) {
case 'human':
sprites = humanSprites
break;
case 'avaaatar':
sprites = avataaarSprites
break;
}
const avatars = new Avatars(sprites, options);
const svg = avatars.create(req.params.seed);
const fileName = `${req.params.avatar_lib}_${req.params.width}_${req.params.seed}.png`;
aws.getObject(fileName, async (err, data) => {
if (data) {
if (showUrl) {
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({'url': `http://${process.env.AWS_STORAGE_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${fileName}`}));
} else {
res.set('Content-Type', 'image/png');
res.end(data.Body);
}
return;
}
const png = await convert(svg, {
width: parseInt(req.params.width || 500, 10),
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
},
});
aws.uploadFile(fileName, png, (data) => {
if (showUrl) {
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({'url': data.Location}));
} else {
res.set('Content-Type', 'image/png');
res.end(png);
}
});
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});