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
/
result-converter.js
187 lines (148 loc) · 5.21 KB
/
result-converter.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
var config = require('config').Application;
var querystring = require('querystring');
var utils = require('./utils.js');
var RomListResponse = function(id, resultItems, errorMessage) {
this.id = id;
this.result = resultItems;
this.error = errorMessage;
};
var RomListItem = function(url, timestamp, md5sum, filename, channel, changes, api_level, incrementalId) {
this.url = url;
this.timestamp = timestamp;
this.md5sum = md5sum;
this.filename = filename;
this.channel = channel;
this.changes = changes;
this.api_level = api_level;
this.incremental = incrementalId;
};
var SuccessfulIncrementalReponse = function(timestamp, filename, download_url, md5sum, incremental) {
this.date_created_unix = timestamp;
this.filename = filename;
this.download_url = download_url;
this.md5sum = md5sum;
this.incremental = incremental;
}
var FailedIncrementalReponse = function(errors) {
this.errors = errors;
}
var IncrementalErrorItem = function(message) {
this.message = message;
}
module.exports.convertIncrementalErrors = function(errorMessages) {
var errors = [];
if (Array.isArray(errorMessages)) {
for (message in errorMessages) {
errors.push(new IncrementalErrorItem(message));
}
} else {
errors.push(new IncrementalErrorItem(errorMessages));
}
return new FailedIncrementalReponse(errors);
}
module.exports.convertIncremental = function(incremental) {
var targetRom = incremental.targetRom;
var unixTimestamp = utils.toUnixTimestamp(targetRom.timestamp);
var downloadUrl;
if (config.isDownloadProxyEnabled) {
downloadUrl = module.exports.getProxyDownloadUrl(config.proxyIncrementalDownloadBaseUrl, incremental);
} else {
downloadUrl = module.exports.getRealIncrementalDownloadUrl(incremental);
}
return new SuccessfulIncrementalReponse(unixTimestamp, incremental.filename, downloadUrl, incremental.md5sum, targetRom.incrementalId);
}
module.exports.getRealDownloadUrl = function(baseUrl, updateItem) {
var url = baseUrl;
if (updateItem.RomVariant.subdirectory && updateItem.RomVariant.subdirectory.length > 0) {
url += '/' + updateItem.RomVariant.subdirectory;
}
url += '/' + updateItem.filename;
return url;
}
module.exports.getProxyDownloadUrl = function(baseUrl, updateItem) {
var url = baseUrl;
url += '/' + updateItem.id;
url += '?' + querystring.stringify({ directory: updateItem.RomVariant.subdirectory, filename: updateItem.filename });
return url;
}
module.exports.getRealIncrementalDownloadUrl = function(incremental) {
return module.exports.getRealDownloadUrl(config.realIncrementalDownloadBaseUrl, incremental);
}
module.exports.getRomDownloadUrl = function(rom) {
if (config.isDownloadProxyEnabled) {
return module.exports.getProxyDownloadUrl(config.proxyRomDownloadBaseUrl, rom);
}
return module.exports.getRealRomDownloadUrl(rom);
}
module.exports.convertRomList = function(id, updateList) {
var list = Array();
if (updateList && updateList.length > 0) {
updateList.forEach(function(rom) {
var changelogUrl = module.exports.getChangelogUrl(rom);
var downloadUrl = module.exports.getRomDownloadUrl(rom);
var timestampInSeconds = Math.round(rom.timestamp.getTime() / 1000);
var item = new RomListItem(downloadUrl, timestampInSeconds, rom.md5sum, rom.filename, rom.updateChannel, changelogUrl, rom.apiLevel, rom.incrementalId);
list.push(item);
});
}
return new RomListResponse(id, list, null);
}
module.exports.convertRomListError = function(id, errorMessage) {
return new RomListResponse(id, null, errorMessage);
}
module.exports.getRealRomDownloadUrl = function(rom) {
return module.exports.getRealDownloadUrl(config.realRomDownloadBaseUrl, rom);
}
module.exports.getRomMd5sumUrl = function(rom) {
return module.exports.getRealRomDownloadUrl(rom) + '.md5sum';
}
module.exports.getChangelogUrl = function(rom) {
return config.changelogBaseUrl + '/' + rom.id;
}
var _getChangelogContent = function(rom) {
var content = "";
if (rom && rom.changelog && rom.changelog.length > 0) {
if (rom.sourceCodeTimestamp && rom.sourceCodeTimestamp > 0) {
content += "===================================\n";
content += "Since ";
content += rom.sourceCodeTimestamp.toUTCString();
content += "\n===================================\n\n";
}
content += rom.changelog + "\n";
}
return content;
}
module.exports.getChangelogContent = function(rom, findParentRomHandler, resultCallback) {
var content = _getChangelogContent(rom);
var i = 0;
if (config.additionalPreviousChangelogs > 0) {
var updateChangelogWithParent = function(parent) {
if (parent) {
++i;
currentRom = parent;
if (content.length > 0) {
// Spearator between two roms.
content += "\n\n";
}
content += _getChangelogContent(currentRom);
if (i < config.additionalPreviousChangelogs) {
findParentRomHandler(parent, function(anotherParent) {
updateChangelogWithParent(anotherParent);
});
} else {
// Parent limit reached -> emit the result.
resultCallback(content);
}
} else {
// There are no more parents -> emit the result.
resultCallback(content);
}
};
findParentRomHandler(rom, function(parent) {
updateChangelogWithParent(parent);
});
} else {
// There are no "parents" to fetch -> emit the result.
resultCallback(content);
}
}