forked from talmobi/v2-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 2.59 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
64
65
66
67
68
69
70
71
72
73
const ParrotConstructor = require("./src/ParrotConstructor");
const ParrotOptionsValidator = require("./src/ParrotOptionsValidator");
const path = require("path");
const fs = require('fs')
const express = require("express");
const request = require("request-promise");
var app = express();
app.get("/partyparrot", (req, res, done) => {
handleRequest(res, req.query);
});
app.get("/baseparrots", (req, res, done) => {
let srcpath = "./baseparrots";
let folders = fs.readdirSync(path.resolve(srcpath)).filter(file => fs.statSync(path.join(srcpath, file)).isDirectory());
res.end(`["${folders.join('","')}"]`);
});
app.get("/partyparrot/:baseparrot", (req, res, done) => {
req.query.baseparrot = req.params.baseparrot;
handleRequest(res, req.query);
});
function handleRequest(res, queryParams) {
let validator = new ParrotOptionsValidator();
let error = validator.validate(queryParams);
if(!error) {
constructParrot(res, queryParams);
} else {
console.error(error);
res.status(400).end(error);
}
}
function constructParrot(res, queryParams) {
let fileName = "generatedparrot.gif";
res.writeHead(200, { "Content-Type":"image/gif" });
let parrotConstructor = new ParrotConstructor();
var promises = [];
if(queryParams.baseparrot) {
parrotConstructor.setBaseParrot(queryParams.baseparrot);
}
parrotConstructor.start(res, {
delay: queryParams.delay,
colors: queryParams.colors ? queryParams.colors.split(",") : null
});
if(queryParams.overlay) {
var overlayPromise = parrotConstructor.addFollowingOverlayImage(queryParams.overlay,
parseInt(queryParams.overlayOffsetX),
parseInt(queryParams.overlayOffsetY),
queryParams.overlayWidth,
queryParams.overlayHeight,
queryParams.flipOverlayX ? true : false,
queryParams.flipOverlayY ? true : false);
promises.push(overlayPromise);
}
if (promises.length > 0) {
Promise.all(promises).then(() => {
parrotConstructor.finish();
}).catch((reason) => {
console.error(reason);
});
} else {
parrotConstructor.finish();
}
}
app.listen(process.env.PORT || 8080);