This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cm-update-server.js
233 lines (204 loc) · 6.02 KB
/
cm-update-server.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var restify = require('restify');
var config = require('config');
var serverConfig = config.Server;
var models = require('./models/');
var ResultConverter = require('./result-converter.js');
var server = restify.createServer({ name: 'cm-updater-server' });
var extractRequestParameters = function(req) {
var requestParameters = null;
// Prior to CM11 the CMUpdater app did not send the correct content-type.
// Thus auto-parsing the body did not work.
if (req.is('json')) {
requestParameters = req.body;
} else if (!!req.body) {
try {
requestParameters = JSON.parse(req.body);
} catch (err) {
// Ignore, should be handled with a 404 by the caller
// (the result is null in this case).
}
}
return requestParameters;
}
models.sequelize.sync().then(function() {
server.listen(serverConfig.listeningPort, serverConfig.listeningAddress, function () {
console.log('%s listening at %s', server.name, server.url);
});
server.use(restify.bodyParser(serverConfig.bodyParserserverConfiguration || {}));
// Conditionally enable the throttle module with the settings from the serverConfig.
if (serverConfig.throttleserverConfiguration && serverConfig.throttleserverConfiguration.isEnabled) {
server.use(restify.throttle(serverConfig.throttleserverConfiguration));
}
if (serverConfig.serveStaticConfiguration) {
serverConfig.serveStaticConfiguration.forEach(function(staticSettings) {
server.get(new RegExp(staticSettings.urlPattern), restify.serveStatic(staticSettings.options));
});
}
server.get('/changelog/:romId', function (req, res, next) {
models.Rom.findById(req.params.romId).then(function(rom) {
if (!rom) {
res.send(404);
return next();
}
if (!rom.isActive) {
res.send(410);
return next();
}
var findParentRomHandler = function(childRom, resultHandler) {
childRom.getParentRom().then(function(parentRom) {
resultHandler(parentRom);
}).catch(function(err) {
resultHandler(null);
});
}
ResultConverter.getChangelogContent(rom, findParentRomHandler, function(body) {
res.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/plain'
});
res.end(body);
return next();
});
}).catch(function(err) {
res.send(500);
return next();
});
});
server.get('/download/rom/:romId', function (req, res, next) {
models.Rom.find({
include: [
{
model: models.RomVariant
}
],
where: {
id: req.params.romId
}
}).then(function(rom) {
if (!rom) {
res.send(404);
} else if (!rom.isActive) {
res.send(410);
} else {
var realDownloadUrl = ResultConverter.getRealRomDownloadUrl(rom);
res.writeHead(301, { Location: realDownloadUrl });
res.end();
models.Download.build({
RomId: rom.id,
userAgent: req.headers['user-agent'],
}).save().catch(function (err) {
// Ignoring errors here since those have no impact for the user.
});
}
return next();
}).catch(function(err) {
res.send(500);
return next();
});
});
server.get('/download/incremental/:incrementalId', function (req, res, next) {
models.Incremental.find({
include: [
{
model: models.RomVariant
}
],
where: {
id: req.params.incrementalId
}
}).then(function(incremental) {
if (!incremental) {
res.send(404);
} else if (!incremental.isActive) {
res.send(410);
} else {
var realDownloadUrl = ResultConverter.getRealIncrementalDownloadUrl(incremental);
res.writeHead(301, { Location: realDownloadUrl });
res.end();
models.Download.build({
IncrementalId: incremental.id,
userAgent: req.headers['user-agent'],
}).save().catch(function (err) {
// Ignoring errors here since those have no impact for the user.
});
}
return next();
}).catch(function(err) {
res.send(500);
return next();
});
});
server.post('/api/v1/build/get_delta', function(req, res, next) {
var requestParameters = extractRequestParameters(req);
if (!requestParameters || !requestParameters.source_incremental || !requestParameters.target_incremental) {
res.send(400, ResultConverter.convertIncrementalErrors(
"source_incremental and target_incremental are required parameters!"));
return next();
}
models.Incremental.find({ include: [
{
model: models.Rom,
as: 'sourceRom',
where: {
incrementalId: requestParameters.source_incremental
}
},
{
model: models.Rom,
as: 'targetRom',
where: {
incrementalId: requestParameters.target_incremental
}
},
{
model: models.RomVariant,
},
]}).then(function(incremental) {
if (!incremental || !incremental.isActive) {
res.send(200, ResultConverter.convertIncrementalErrors("No matching incremental update found!"));
} else {
res.send(200, ResultConverter.convertIncremental(incremental));
}
return next();
}).catch(function(err) {
res.send(500, ResultConverter.convertIncrementalErrors('Database error.'));
return next();
});
});
server.post('/api', function(req, res, next) {
var requestParameters = extractRequestParameters(req);
var responseId = null;
if (!requestParameters || !requestParameters.params) {
res.send(400, ResultConverter.convertRomListError(responseId, 'params is a required parameter!'));
return next();
} else if (requestParameters.method != 'get_all_builds') {
res.send(400, ResultConverter.convertRomListError(responseId, requestParameters.method + ' is not a valid "method"!'));
return next();
}
models.Rom.findAll({
include: [
{
model: models.RomVariant,
include: [
{
model: models.Device,
where: {
name: requestParameters.params.device,
}
}
]
}
],
where: {
updateChannel: requestParameters.params.channels,
isActive: true,
},
}).then(function(roms) {
res.send(200, ResultConverter.convertRomList(responseId, roms));
return next();
}).catch(function(err) {
res.send(500, ResultConverter.convertRomListError(responseId, 'Database error.'));
return next();
});
});
});