forked from huaizhongoi/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.js
330 lines (307 loc) · 9.9 KB
/
utility.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
Array.prototype.forEachAsync = Array.prototype.mapAsync = async function (fn) {
return Promise.all(this.map(fn));
};
Array.prototype.filterAsync = async function (fn) {
let a = await this.mapAsync(fn);
return this.filter((x, i) => a[i]);
};
global.ErrorMessage = class ErrorMessage {
constructor(message, nextUrls, details) {
this.message = message;
this.nextUrls = nextUrls || {};
this.details = details;
}
};
let Promise = require('bluebird');
let path = require('path');
let fs = Promise.promisifyAll(require('fs-extra'));
let util = require('util');
let moment = require('moment');
let url = require('url');
let querystring = require('querystring');
let gravatar = require('gravatar');
let filesize = require('file-size');
let AsyncLock = require('async-lock');
let JSDOM = require('jsdom').JSDOM;
let renderer = require('./libs/renderer');
module.exports = {
resolvePath(s) {
let a = Array.from(arguments);
a.unshift(__dirname);
return path.resolve.apply(null, a);
},
markdown(obj, keys, noReplaceUI) {
let replaceUI = s => {
if (noReplaceUI) return s;
s = s.split('<pre>').join('<div class="ui existing segment"><pre style="margin-top: 0; margin-bottom: 0; ">').split('</pre>').join('</pre></div>')
.split('<table>').join('<table class="ui structured celled table">')
.split('<blockquote>').join('<div class="ui message">').split('</blockquote>').join('</div>');
let jsdom = new JSDOM(), document = jsdom.window.document;
document.body.innerHTML = s;
let a = document.querySelectorAll('p > img:only-child');
for (let img of Array.from(a)) {
img.style.display = 'block';
img.style.margin = '0 auto';
}
return document.body.innerHTML;
};
return new Promise((resolve, reject) => {
if (!keys) {
if (!obj || !obj.trim()) resolve("");
else renderer.markdown(obj, s => {
resolve(replaceUI(s));
});
} else {
let res = obj, cnt = keys.length;
for (let key of keys) {
renderer.markdown(res[key], (s) => {
res[key] = replaceUI(s);
if (!--cnt) resolve(res);
});
}
}
});
},
formatDate(ts, format) {
if (ts == null) {
return "Unknown";
}
let m = moment(ts * 1000);
m.locale('eu');
return m.format(format || 'L H:mm:ss');
},
formatTime(x) {
let sgn = x < 0 ? '-' : '';
x = Math.abs(x);
function toStringWithPad(x) {
x = parseInt(x);
if (x < 10) return '0' + x.toString();
else return x.toString();
}
return sgn + util.format('%s:%s:%s', toStringWithPad(x / 3600), toStringWithPad(x / 60 % 60), toStringWithPad(x % 60));
},
formatSize(x, precision) {
if (typeof x !== 'number') return '0 B';
let unit = 'B', units = ['K', 'M', 'G', 'T'];
for (let i in units) if (x > 1024) x /= 1024, unit = units[i];
var fixed = x === Math.round(x) ? x.toString() : x.toFixed(precision);
return fixed + ' ' + unit;
},
getFormattedCodeKey(code, lang_format) {
if (lang_format) {
return lang_format + '\n' + syzoj.utils.md5(code);
}
return null;
},
judgeServer(suffix) {
return JSON.stringify(url.resolve(syzoj.config.judge_server_addr, suffix));
},
parseDate(s) {
return parseInt(+new Date(s) / 1000);
},
getCurrentDate(removeTime) {
let d = new Date;
if (removeTime) {
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
}
return parseInt(+d / 1000);
},
makeUrl(req_params, form) {
let res = '';
if (!req_params) res = '/';
else if (req_params.originalUrl) {
let u = url.parse(req_params.originalUrl);
res = u.pathname;
} else {
if (!Array.isArray(req_params)) req_params = [req_params];
for (let param of req_params) res += '/' + param;
}
let encoded = querystring.encode(form);
if (encoded) res += '?' + encoded;
return res;
},
highlight(code, lang) {
return new Promise((resolve, reject) => {
renderer.highlight(code, lang, res => {
resolve(res);
});
});
},
gravatar(email, size) {
return gravatar.url(email, { s: size, d: 'mm' }).replace('//www.gravatar.com/avatar', syzoj.config.gravatar_url);
},
async parseTestdata(dir, submitAnswer) {
if (!await syzoj.utils.isDir(dir)) return null;
try {
// Get list of *files*
let list = await (await fs.readdirAsync(dir)).filterAsync(async x => await syzoj.utils.isFile(path.join(dir, x)));
let res = [];
if (!list.includes('data.yml')) {
res[0] = {};
res[0].cases = [];
for (let file of list) {
let parsedName = path.parse(file);
if (parsedName.ext === '.in') {
if (list.includes(`${parsedName.name}.out`)) {
let o = {
input: file,
output: `${parsedName.name}.out`
};
if (submitAnswer) o.answer = `${parsedName.name}.out`;
res[0].cases.push(o);
}
if (list.includes(`${parsedName.name}.ans`)) {
let o = {
input: file,
output: `${parsedName.name}.ans`
};
if (submitAnswer) o.answer = `${parsedName.name}.out`;
res[0].cases.push(o);
}
}
}
res[0].type = 'sum';
res[0].score = 100;
res[0].cases.forEach((e) => { e.key = (e.input.match(/\d+/g) || []).map((x) => parseInt(x)).concat(e.input); });
res[0].cases.sort((a, b) => {
for (let i = 0; i < Math.max(a.key.length, b.key.length); ++i) {
if (a.key[i] == undefined) return -1;
if (b.key[i] == undefined) return +1;
if (a.key[i] !== b.key[i]) return (a.key[i] < b.key[i] ? -1 : +1);
}
return 0;
});
res.spj = list.some(s => s.startsWith('spj_'));
} else {
let config = require('js-yaml').load((await fs.readFileAsync(dir + '/data.yml')));
let input = config.inputFile, output = config.outputFile, answer = config.userOutput;
res = config.subtasks.map(st => ({
score: st.score,
type: st.type,
cases: st.cases.map(c => {
function getFileName(template, id, mustExist) {
let s = template.split('#').join(String(id));
if (mustExist && !list.includes(s)) throw `找不到文件 ${s}`;
return s;
}
let o = {};
if (input) o.input = getFileName(input, c, true);
if (output) o.output = getFileName(output, c, true);
if (answer) o.answer = getFileName(answer, c, false);
return o;
})
}));
res = res.filter(x => x.cases && x.cases.length !== 0);
res.spj = !!config.specialJudge;
}
return res;
} catch (e) {
console.log(e);
return { error: e };
}
},
ansiToHTML(s) {
let Convert = require('ansi-to-html');
let convert = new Convert({ escapeXML: true });
return convert.toHtml(s);
},
paginate(count, currPage, perPage) {
currPage = parseInt(currPage);
if (!currPage || currPage < 1) currPage = 1;
let pageCnt = Math.ceil(count / perPage);
if (currPage > pageCnt) currPage = pageCnt;
return {
currPage: currPage,
perPage: perPage,
pageCnt: pageCnt,
toSQL: () => {
if (!pageCnt) return '';
else return ` LIMIT ${(currPage - 1) * perPage},${perPage}`
}
};
},
paginateFast(currPageTop, currPageBottom, perPage) {
function parseIntOrNull(x) {
if (typeof x === 'string') x = parseInt(x);
if (typeof x !== 'number' || isNaN(x)) return null;
return x;
}
return {
currPageTop: parseIntOrNull(currPageTop),
currPageBottom: parseIntOrNull(currPageBottom),
perPage
};
},
removeTitleTag(s) {
return s.replace(/「[\S\s]+?」/, '');
},
md5(data) {
let crypto = require('crypto');
let md5 = crypto.createHash('md5');
md5.update(data);
return md5.digest('hex');
},
isValidUsername(s) {
return RegExp(syzoj.config.username_regex).test(s);
},
isValidRealName(s) {
return !s.trim().length || /^[a-zA-Z \u4e00-\u9fa5]+$/.test(s);
},
locks: [],
lock(key, cb) {
let s = JSON.stringify(key);
if (!this.locks[s]) this.locks[s] = new AsyncLock();
return this.locks[s].acquire(s, cb);
},
encrypt(buffer, password) {
if (typeof buffer === 'string') buffer = Buffer.from(buffer);
let crypto = require('crypto');
let cipher = crypto.createCipher('aes-256-ctr', password);
return Buffer.concat([cipher.update(buffer), cipher.final()]);
},
decrypt(buffer, password) {
let crypto = require('crypto');
let decipher = crypto.createDecipher('aes-256-ctr', password);
return Buffer.concat([decipher.update(buffer), decipher.final()]);
},
async isFile(path) {
try {
return (await fs.statAsync(path)).isFile();
} catch (e) {
return false;
}
},
async isDir(path) {
try {
return (await fs.statAsync(path)).isDirectory();
} catch (e) {
return false;
}
},
async saveConfig() {
let fs = require('fs-extra');
fs.writeFileAsync(syzoj.configDir, JSON.stringify(syzoj.configInFile, null, 2));
},
withTimeoutRetry(func) {
let attemptCount = 0;
return new Promise((resolve, reject) => {
function attempt() {
if (attemptCount++) console.log(`syzoj.utils.withTimeout(): attemptCount = ${attemptCount}`);
Promise.method(func)().timeout(5000)
.then(resolve)
.catch(Promise.TimeoutError, attempt)
.catch(reject);
}
attempt();
});
},
getCurrentLocation(req, hostOnly) {
const currentProto = req.get("X-Forwarded-Proto") || req.protocol,
host = currentProto + '://' + req.get('host');
if (hostOnly) return host;
else return host + req.originalUrl;
}
};