core: logger upd

This commit is contained in:
undefined committed 2020-12-21 03:06:41 +08:00
1 parent d4088f429a
commit 4d031eb9ab
9 files changed
+141 -131

No files matched your search

+28 -44
View File
@@ -4,25 +4,20 @@ import os from 'os';
import fs from 'fs-extra';
import AdmZip from 'adm-zip';
import yaml from 'js-yaml';
import { ObjectID } from 'mongodb';
import { ContentNode, LocalProblemConfig } from 'hydrooj';
import {
Route, Handler, param, Types,
} from 'hydrooj/dist/service/server';
import { BadRequestError } from 'hydrooj/dist/error';
import { streamToBuffer } from 'hydrooj/dist/utils';
import { Route, Handler } from 'hydrooj/dist/service/server';
import * as storage from 'hydrooj/dist/service/storage';
import { BadRequestError, ValidationError } from 'hydrooj/dist/error';
import { ProblemAdd } from 'hydrooj/dist/lib/ui';
import * as file from 'hydrooj/dist/model/file';
import * as problem from 'hydrooj/dist/model/problem';
import { PERM } from 'hydrooj/dist/model/builtin';
const processing = {};
fs.ensureDirSync('/tmp/hydro/import-qduoj');
class ImportQduojHandler extends Handler {
async ImportFromFile(domainId: string, id: string, zipfile: Buffer) {
async ImportFromFile(domainId: string, zipfile: string) {
const zip = new AdmZip(zipfile);
const tmp = path.resolve(os.tmpdir(), 'hydro', 'import-qduoj', id);
const tmp = path.resolve(os.tmpdir(), 'hydro', 'import-qduoj', String.random(32));
await new Promise((resolve, reject) => {
zip.extractAllToAsync(tmp, true, (err) => (err ? resolve() : reject(err)));
});
@@ -79,15 +74,20 @@ class ImportQduojHandler extends Handler {
});
}
const pid = await problem.add(domainId, pdoc.display_id, pdoc.title, content, this.user._id, pdoc.tags);
const testdata = new AdmZip();
const config: LocalProblemConfig = {
time: `${pdoc.time_limit}ms`,
memory: `${pdoc.memory_limit}m`,
subtasks: [],
};
for (const tc of pdoc.test_case_score) {
testdata.addLocalFile(path.join(tmp, 'testcase', tc.input_name));
testdata.addLocalFile(path.join(tmp, 'testcase', tc.output_name));
await storage.put(
`problem/${domainId}/${pid}/testdata/${tc.input_name}`,
path.join(tmp, 'testcase', tc.input_name),
);
await storage.put(
`problem/${domainId}/${pid}/testdata/${tc.output_name}`,
path.join(tmp, 'testcase', tc.output_name),
);
config.subtasks.push({
score: tc.score,
cases: [{
@@ -96,43 +96,27 @@ class ImportQduojHandler extends Handler {
}],
});
}
testdata.addFile('config.yaml', Buffer.from(yaml.safeDump(config)));
const f = path.resolve(os.tmpdir(), 'hydro', `${Math.random()}.zip`);
await new Promise((resolve, reject) => {
testdata.writeZip(f, (err) => (err ? resolve() : reject(err)));
});
try {
await problem.setTestdata(domainId, pid, f);
await problem.edit(domainId, pid, { html: true });
} finally {
await fs.unlink(f);
}
await storage.put(
`problem/${domainId}/${pid}/testdata/config.yaml`,
Buffer.from(yaml.safeDump(config)),
);
await problem.edit(domainId, pid, { html: true });
}
} finally {
await fs.remove(tmp);
}
}
@param('ufid', Types.ObjectID, true)
async get(domainId: string, ufid?: ObjectID) {
if (ufid) {
if (processing[ufid.toHexString()]) {
this.response.body = 'Processing';
return;
}
const stat = await file.getMeta(ufid);
if (stat.size > 128 * 1024 * 1024) throw new BadRequestError('File too large');
const stream = await file.get(ufid);
processing[ufid.toHexString()] = true;
try {
const buf = await streamToBuffer(stream);
await this.ImportFromFile(domainId, ufid.toHexString(), buf);
await file.del(ufid);
} catch (e) {
processing[ufid.toHexString()] = false;
throw e;
}
} else this.response.template = 'problem_import_qduoj.html';
async get() {
this.response.template = 'problem_import.html';
}
async post({ domainId }) {
if (!this.request.files.file) throw new ValidationError('file');
const stat = await fs.stat(this.request.files.file.path);
if (stat.size > 128 * 1024 * 1024) throw new BadRequestError('File too large');
await this.ImportFromFile(domainId, this.request.files.file.path);
this.response.redirect = this.url('problem_main');
}
}