You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/handler/misc.js

85 lines
2.6 KiB
JavaScript

4 years ago
const { PERM_JUDGE, PERM_LOGGEDIN } = require('../permission');
4 years ago
const file = require('../model/file');
const user = require('../model/user');
4 years ago
const markdown = require('../lib/markdown');
const db = require('../service/db');
4 years ago
const { Route, Handler } = require('../service/server');
const coll = db.collection('status');
4 years ago
class FileDownloadHandler extends Handler {
async get({ id, secret, name }) {
if (name) name = Buffer.from(name, 'base64').toString();
this.response.attachment(name || id);
this.response.body = await file.get(id, secret);
}
}
class StatusHandler extends Handler {
async get() {
const stats = await coll.find().sort({ type: 1, updateAt: -1 }).toArray();
for (const i in stats) {
let desc = '';
const online = new Date(stats[i].updateAt).getTime() > new Date().getTime() - 300000;
if (!online) desc = 'Offline';
desc = desc || 'Online';
4 years ago
stats[i].isOnline = online;
stats[i].status = desc;
}
4 years ago
const path = [
['Hydro', '/'],
];
this.response.body = { stats, path };
this.response.template = 'status.html';
}
}
class StatusUpdateHandler extends Handler {
async post(args) {
this.checkPerm(PERM_JUDGE);
4 years ago
args.type = 'judger';
return coll.updateOne(
{ mid: args.mid, type: 'judger' },
{ $set: args },
{ upsert: true },
);
}
}
class SwitchLanguageHandler extends Handler {
async get({ lang }) {
if (this.user.hasPerm(PERM_LOGGEDIN)) await user.setById(this.user._id, { viewLang: lang });
else this.session.viewLang = lang;
this.back();
}
}
4 years ago
class MarkdownHandler extends Handler {
async post({ text, safe = true }) {
this.response.body = safe
? markdown.safe.render(text)
: markdown.unsafe.render(text);
}
}
async function apply() {
Route('file_download', '/fs/:id/:secret', FileDownloadHandler);
Route('file_download_with_name', '/fs/:id/:name/:secret', FileDownloadHandler);
Route('status', '/status', StatusHandler);
Route('status_update', '/status/update', StatusUpdateHandler);
Route('switch_language', '/language/:lang', SwitchLanguageHandler);
4 years ago
Route('markdown', '/markdown', MarkdownHandler);
}
4 years ago
apply.updateStatus = function updateStatus(args) {
args.type = 'judger';
return coll.updateOne(
{ mid: args.mid, type: 'judger' },
{ $set: args },
{ upsert: true },
);
4 years ago
};
4 years ago
global.Hydro.handler.misc = module.exports = apply;