-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
164 lines (147 loc) · 5.03 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* SMotion - Stop Motion Animation in a Web Browser
* Copyright (C) 2017 Chris Luby <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const path = require("path");
const express = require("express");
const bodyParser = require('body-parser');
const handlebars = require('express-handlebars');
const packageInfo = require("./package.json");
const config = require("./config/config.json");
const Take = require("./lib/Take.js");
// HTTP Server
const httpServer = express();
httpServer.use(bodyParser.json({limit: "100mb"}));
httpServer.engine("handlebars", handlebars({
"defaultLayout": "main",
"helpers": {
"toJSON": function(o) {
return JSON.stringify(o, null, 2);
}
}
}));
httpServer.set("view engine", "handlebars");
httpServer.use("/styles", express.static(path.join(__dirname, "site/styles")));
httpServer.use("/images", express.static(path.join(__dirname, "images")));
httpServer.use("/scripts", express.static(path.join(__dirname, "scripts")));
httpServer.use(["/favicon*", "/apple-touch-icon-*", "/mstile-*"], function (req, res) {
res.sendFile(path.join(__dirname, "site" + req.originalUrl));
});
httpServer.use("/help", express.static(path.join(__dirname, "site")));
httpServer.get("/", function(req, res) {
const data = {
"title": `${packageInfo.description} (v${packageInfo.version})`,
"version": `${packageInfo.version}`
};
res.render("home", data);
});
httpServer.use("/takes", express.static(config.takesDir));
httpServer.get("/take/list", function(req, res) {
res.send(Take.getList(config));
});
httpServer.put("/take", function(req, res) {
const take = req.body.take;
const takeToCopy = req.body.takeToCopy;
console.log("IN: ", JSON.stringify(take, null, 2));
if (takeToCopy) {
console.log("COPY: ", JSON.stringify(takeToCopy, null, 2));
}
try {
const newTake = Take.newTake(config, take, takeToCopy);
console.log("OUT: ", JSON.stringify(newTake, null, 2));
res.send({"take": newTake});
} catch (e) {
console.log("ERR: ", e.toString());
res.send({"err": e.toString()});
}
});
httpServer.post("/take", function(req, res) {
const take = req.body.take;
console.log("IN: ", JSON.stringify(take, null, 2));
try {
const newTake = Take.saveTake(config, take);
console.log("OUT: ", JSON.stringify(newTake, null, 2));
res.send({"take": newTake});
} catch (e) {
console.log("ERR: ", e.toString());
res.send({"err": e.toString()});
}
});
httpServer.delete("/take", function(req, res) {
const take = req.body.take;
console.log("IN: ", JSON.stringify(take, null, 2));
try {
Take.deleteTake(config, take);
res.send({});
} catch (e) {
console.log("ERR: ", e.toString());
res.send({"err": e.toString()});
}
});
httpServer.delete("/take/frame", function(req, res) {
const take = req.body.take;
console.log("IN: ", JSON.stringify(take, null, 2));
try {
const newTake = Take.removeCurrentFrame(config, take);
console.log("OUT: ", JSON.stringify(newTake, null, 2));
res.send({"take": newTake});
} catch (e) {
console.log("ERR: ", e.toString());
res.send({"err": e.toString()});
}
});
httpServer.put("/take/frame", function(req, res) {
const take = req.body.take;
console.log("IN: ", JSON.stringify(take, null, 2));
try {
const newTake = Take.insertAtCurrentFrame(config, take);
console.log("OUT: ", JSON.stringify(newTake, null, 2));
res.send({"take": newTake});
} catch (e) {
console.log("ERR: ", e.toString());
res.send({"err": e.toString()});
}
});
httpServer.post("/take/frame/image", function(req, res) {
const take = req.body.take;
const data = req.body.data;
console.log("IN: ", JSON.stringify(take, null, 2));
console.log(data.substr(0,100));
try {
const newTake = Take.saveCurrentFrameImage(config, take, data);
console.log("OUT: ", JSON.stringify(newTake, null, 2));
res.send({"take": newTake});
} catch (e) {
console.log("ERR: ", e.toString());
res.send({"err": e.toString()});
}
});
httpServer.post("/take/render", function(req, res) {
const take = req.body.take;
console.log("IN: ", JSON.stringify(take, null, 2));
try {
const newTake = Take.render(config, take);
console.log("OUT: ", JSON.stringify(newTake, null, 2));
res.send({"take": newTake});
} catch (e) {
console.log("ERR: ", e.toString());
res.send({"err": e.toString()});
}
});
httpServer.listen(config.port);
console.log(`${packageInfo.description} listening on ${config.port}`);
console.log("CONFIG: ", JSON.stringify(config, null, 2));