fix some bugs, bump ver(2.15.0), update install

This commit is contained in:
undefined committed 2020-12-22 13:40:47 +08:00
1 parent 4d031eb9ab
commit bd8ff05387
29 files changed
+226 -162

No files matched your search

+39 -59
View File
@@ -1,66 +1,28 @@
/* eslint-disable no-await-in-loop */
import path from 'path';
import os from 'os';
import fs from 'fs-extra';
import AdmZip from 'adm-zip';
import xml2js from 'xml2js';
import { ObjectID } from 'mongodb';
import fs from 'fs-extra';
import { filter } from 'lodash';
import decodeHTML from 'decode-html';
import { LocalProblemConfig } from 'hydrooj';
import {
Route, Handler, param, Types,
} from 'hydrooj/dist/service/server';
import { streamToBuffer } from 'hydrooj/dist/utils';
import * as storage from 'hydrooj/dist/service/storage';
import { buildContent } from 'hydrooj/dist/lib/content';
import { ProblemAdd } from 'hydrooj/dist/lib/ui';
import * as file from 'hydrooj/dist/model/file';
import * as solution from 'hydrooj/dist/model/solution';
import { PERM } from 'hydrooj/dist/model/builtin';
const processing = {};
import { ValidationError } from 'hydrooj/dist/error';
class FpsProblemImportHandler extends Handler {
@param('ufid', Types.ObjectID, true)
async get(domainId: string, ufid?: ObjectID) {
if (ufid) {
if (processing[ufid.toHexString()]) {
this.response.body = 'Processing';
return;
}
const stream = await file.get(ufid);
processing[ufid.toHexString()] = true;
try {
const buf = await streamToBuffer(stream);
let input = buf.toString();
try {
await xml2js.parseStringPromise(input);
} catch (e) {
const zip = new AdmZip(buf);
const entries = zip.getEntries();
for (const entry of entries) {
if (entry.entryName.endsWith('.xml')) {
input = entry.getData().toString();
break;
}
}
}
// @ts-ignore
await this.post({ domainId, input });
await file.del(ufid);
} catch (e) {
processing[ufid.toHexString()] = false;
throw e;
}
} else this.response.template = 'problem_import_fps.html';
async get() {
this.response.template = 'problem_import.html';
}
@param('input', Types.String)
async post(domainId: string, input: string) {
const result = await xml2js.parseStringPromise(input);
async run(domainId: string, result: any) {
const problem = global.Hydro.model.problem;
for (const p of result.fps.item) {
const testdata = new AdmZip();
const content = buildContent({
description: p.description,
input: p.input,
@@ -77,26 +39,18 @@ class FpsProblemImportHandler extends Handler {
const title = decodeHTML(p.title.join(' '));
const tags = filter(p.source, (i: string) => i.trim());
const pid = await problem.add(domainId, null, title, content, this.user._id, tags, []);
testdata.addFile('config.yaml', Buffer.from(`time: ${config.time}\nmemory: ${config.memory}`));
await storage.put(`problem/${domainId}/${pid}/testdata/config.yaml`, Buffer.from(`time: ${config.time}\nmemory: ${config.memory}`));
if (p.test_output) {
for (let i = 0; i < p.test_input.length; i++) {
testdata.addFile(`${i + 1}.in`, Buffer.from(p.test_input[i]));
testdata.addFile(`${i + 1}.out`, Buffer.from(p.test_output[i]));
await storage.put(`problem/${domainId}/${pid}/testdata/${i + 1}.in`, Buffer.from(p.test_input[i]));
await storage.put(`problem/${domainId}/${pid}/testdata/${i + 1}.out`, Buffer.from(p.test_output[i]));
}
} else if (p.test_input) {
for (let i = 0; i < p.test_input.length / 2; i++) {
testdata.addFile(`${i + 1}.in`, Buffer.from(p.test_input[2 * i]));
testdata.addFile(`${i + 1}.out`, Buffer.from(p.test_input[2 * i + 1]));
await storage.put(`problem/${domainId}/${pid}/testdata/${i + 1}.in`, Buffer.from(p.test_input[2 * i]));
await storage.put(`problem/${domainId}/${pid}/testdata/${i + 1}.out`, Buffer.from(p.test_input[2 * i + 1]));
}
}
const f = path.resolve(os.tmpdir(), 'hydro', `${Math.random()}.zip`);
await new Promise((resolve, reject) => {
testdata.writeZip(f, (err) => {
if (err) reject(err);
resolve();
});
});
await problem.setTestdata(domainId, pid, f);
await problem.edit(domainId, pid, { html: true });
if (p.solution) {
let s = '';
@@ -105,9 +59,35 @@ class FpsProblemImportHandler extends Handler {
}
await solution.add(domainId, pid, this.user._id, s);
}
await fs.unlink(f);
}
this.response.body = { count: result.fps.item.length };
}
@param('input', Types.String, true)
async post(domainId: string, input: string) {
if (!input && !this.request.files.file) throw new ValidationError('file');
const tasks = [];
if (this.request.files.file) {
try {
const content = fs.readFile(this.request.files.file.path).toString();
const result = await xml2js.parseStringPromise(content);
tasks.push(result);
} catch (e) {
const zip = new AdmZip(this.request.files.file.path);
for (const entry of zip.getEntries()) {
try {
const content = entry.getData().toString();
const result = await xml2js.parseStringPromise(content);
tasks.push(result);
} catch { } // eslint-disable-line no-empty
}
}
} else {
tasks.push(await xml2js.parseStringPromise(input));
}
if (!tasks.length) throw new ValidationError('file');
for (const task of tasks) {
await this.run(domainId, task);
}
this.response.redirect = this.url('problem_main');
}
}