mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
测试数据上传时自动读取config
This commit is contained in:
1 parent
0d7d6ea12a
commit
4b3c10b4eb
14 files changed
+89
-42
No files matched your search
@@ -27,5 +27,6 @@ module.exports = {
|
|||||||
"global-require": "off",
|
"global-require": "off",
|
||||||
"no-nested-ternary": "off",
|
"no-nested-ternary": "off",
|
||||||
"no-multi-assign": "off",
|
"no-multi-assign": "off",
|
||||||
|
"no-return-await": "off",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -6,6 +6,7 @@ const builtinLib = [
|
|||||||
'axios', 'download', 'i18n', 'mail', 'markdown',
|
'axios', 'download', 'i18n', 'mail', 'markdown',
|
||||||
'md5', 'misc', 'paginate', 'hash.hydro', 'rank',
|
'md5', 'misc', 'paginate', 'hash.hydro', 'rank',
|
||||||
'template', 'validator', 'nav', 'sysinfo', 'testdata.convert.ini',
|
'template', 'validator', 'nav', 'sysinfo', 'testdata.convert.ini',
|
||||||
|
'readConfig',
|
||||||
];
|
];
|
||||||
|
|
||||||
const builtinModel = [
|
const builtinModel = [
|
||||||
|
|||||||
+11
-1
@@ -1,3 +1,6 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const os = require('os');
|
||||||
const problem = require('../model/problem');
|
const problem = require('../model/problem');
|
||||||
const { Route, Handler } = require('../service/server');
|
const { Route, Handler } = require('../service/server');
|
||||||
const { PERM_CREATE_PROBLEM } = require('../permission');
|
const { PERM_CREATE_PROBLEM } = require('../permission');
|
||||||
@@ -29,7 +32,14 @@ class ProblemImportHandler extends Handler {
|
|||||||
if (pid) pdoc.pid = pid;
|
if (pid) pdoc.pid = pid;
|
||||||
if (hidden) pdoc.hidden = true;
|
if (hidden) pdoc.hidden = true;
|
||||||
const docId = await problem.add(domainId, pdoc.title, pdoc.content, this.user._id, pdoc);
|
const docId = await problem.add(domainId, pdoc.title, pdoc.content, this.user._id, pdoc);
|
||||||
await problem.setTestdata(docId, testdata);
|
const file = path.resolve(os.tmpdir(), 'hydro', `import_${domainId}_${pid}.zip`);
|
||||||
|
const w = fs.createWriteStream(file);
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
w.on('finish', resolve);
|
||||||
|
w.on('error', reject);
|
||||||
|
testdata.pipe(w);
|
||||||
|
});
|
||||||
|
await problem.setTestdata(domainId, docId, file);
|
||||||
this.response.body = { pid: pid || docId };
|
this.response.body = { pid: pid || docId };
|
||||||
this.response.redirect = `/p/${pid || docId}/settings`;
|
this.response.redirect = `/p/${pid || docId}/settings`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,9 +79,15 @@ class ProblemHandler extends Handler {
|
|||||||
class ProblemCategoryHandler extends ProblemHandler {
|
class ProblemCategoryHandler extends ProblemHandler {
|
||||||
async get({ domainId, page = 1, category }) {
|
async get({ domainId, page = 1, category }) {
|
||||||
this.response.template = 'problem_main.html';
|
this.response.template = 'problem_main.html';
|
||||||
const q = {
|
const q = { $and: [] };
|
||||||
$or: [{ category }, { tag: category }],
|
for (const name of category) {
|
||||||
};
|
q.$and.push({
|
||||||
|
$or: [
|
||||||
|
{ category: { $elemMatch: { $eq: name } } },
|
||||||
|
{ tag: { $elemMatch: { $eq: name } } },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
let psdict = {};
|
let psdict = {};
|
||||||
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
|
if (!this.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) q.hidden = false;
|
||||||
const [pdocs, ppcount, pcount] = await paginate(
|
const [pdocs, ppcount, pcount] = await paginate(
|
||||||
@@ -97,10 +103,10 @@ class ProblemCategoryHandler extends ProblemHandler {
|
|||||||
const path = [
|
const path = [
|
||||||
['Hydro', 'homepage'],
|
['Hydro', 'homepage'],
|
||||||
['problem_main', 'problem_main'],
|
['problem_main', 'problem_main'],
|
||||||
[category, null, true],
|
[category, null, null, true],
|
||||||
];
|
];
|
||||||
this.response.body = {
|
this.response.body = {
|
||||||
path, page, pcount, ppcount, pdocs, psdict, category,
|
path, page, pcount, ppcount, pdocs, psdict, category: category.join('+'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -295,8 +301,7 @@ class ProblemDataUploadHandler extends ProblemManageHandler {
|
|||||||
|
|
||||||
async post({ domainId }) {
|
async post({ domainId }) {
|
||||||
if (!this.request.files.file) throw new BadRequestError();
|
if (!this.request.files.file) throw new BadRequestError();
|
||||||
const r = fs.createReadStream(this.request.files.file.path);
|
await problem.setTestdata(domainId, this.pdoc.docId, this.request.files.file.path);
|
||||||
await problem.setTestdata(domainId, this.pdoc.docId, r);
|
|
||||||
if (this.pdoc.data && typeof this.pdoc.data === 'object') {
|
if (this.pdoc.data && typeof this.pdoc.data === 'object') {
|
||||||
const f = await file.getMeta(this.pdoc.data);
|
const f = await file.getMeta(this.pdoc.data);
|
||||||
this.md5 = f.md5;
|
this.md5 = f.md5;
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
const Zip = require('adm-zip');
|
||||||
|
|
||||||
|
async function readConfig(filePath) {
|
||||||
|
const data = new Zip(filePath);
|
||||||
|
const entries = data.getEntries();
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (entry.name.toLowerCase() === 'config.yaml') {
|
||||||
|
return entry.getData().toString();
|
||||||
|
}
|
||||||
|
if (entry.name.toLowerCase() === 'config.ini') {
|
||||||
|
const ini = entry.getData().toString();
|
||||||
|
const conv = global.Hydro.lib['testdata.convert.ini'];
|
||||||
|
return conv(ini);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
global.Hydro.lib.readConfig = module.exports = readConfig;
|
||||||
+1
-1
@@ -45,7 +45,7 @@ async function load() {
|
|||||||
process.stdin.setEncoding('utf8');
|
process.stdin.setEncoding('utf8');
|
||||||
process.stdin.on('data', async (input) => {
|
process.stdin.on('data', async (input) => {
|
||||||
try {
|
try {
|
||||||
const t = eval(input.toString().trim()); // eslint-disable-line no-eval
|
const t = eval(input.toString().trim());
|
||||||
if (t instanceof Promise) console.log(await t);
|
if (t instanceof Promise) console.log(await t);
|
||||||
else console.log(t);
|
else console.log(t);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ const user = require('./user');
|
|||||||
const problem = require('./problem');
|
const problem = require('./problem');
|
||||||
const {
|
const {
|
||||||
ValidationError, ContestNotFoundError, ContestAlreadyAttendedError,
|
ValidationError, ContestNotFoundError, ContestAlreadyAttendedError,
|
||||||
ContestNotAttendedError, ProblemNotFoundError, ContestScoreboardHiddenError,
|
ContestNotAttendedError, ContestScoreboardHiddenError,
|
||||||
} = require('../error');
|
} = require('../error');
|
||||||
const { PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD } = require('../permission');
|
const { PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD } = require('../permission');
|
||||||
const validator = require('../lib/validator');
|
const validator = require('../lib/validator');
|
||||||
@@ -74,7 +74,9 @@ const acm = {
|
|||||||
const rows = [columns];
|
const rows = [columns];
|
||||||
for (const [rank, tsdoc] of rankedTsdocs) {
|
for (const [rank, tsdoc] of rankedTsdocs) {
|
||||||
const tsddict = {};
|
const tsddict = {};
|
||||||
if (tdoc.detail) { for (const item of tsdoc.journal) tsddict[item.pid] = item; }
|
if (tdoc.detail) {
|
||||||
|
for (const item of tsdoc.journal) tsddict[item.pid] = item;
|
||||||
|
}
|
||||||
const row = [];
|
const row = [];
|
||||||
row.push(
|
row.push(
|
||||||
{ type: 'string', value: rank },
|
{ type: 'string', value: rank },
|
||||||
@@ -346,13 +348,7 @@ const ContestHandlerMixin = (c) => class extends c {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async verifyProblems(domainId, pids) { // eslint-disable-line class-methods-use-this
|
async verifyProblems(domainId, pids) { // eslint-disable-line class-methods-use-this
|
||||||
const r = [];
|
return await problem.getList(domainId, pids, true);
|
||||||
for (const pid of pids) {
|
|
||||||
const res = await problem.get(domainId, pid); // eslint-disable-line no-await-in-loop
|
|
||||||
if (res) r.push(res.docId);
|
|
||||||
else throw new ProblemNotFoundError(pid);
|
|
||||||
}
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ function editReply(domainId, drid, content) {
|
|||||||
async function delReply(domainId, drid) {
|
async function delReply(domainId, drid) {
|
||||||
const drdoc = await getReply(domainId, drid);
|
const drdoc = await getReply(domainId, drid);
|
||||||
if (!drdoc) throw new DocumentNotFoundError(drid);
|
if (!drdoc) throw new DocumentNotFoundError(drid);
|
||||||
return await Promise.all([ // eslint-disable-line no-return-await
|
return await Promise.all([
|
||||||
document.deleteOne(domainId, document.TYPE_DISCUSSION_REPLY, drid),
|
document.deleteOne(domainId, document.TYPE_DISCUSSION_REPLY, drid),
|
||||||
document.inc(domainId, document.TYPE_DISCUSSION, drdoc.parentId, 'nReply', -1),
|
document.inc(domainId, document.TYPE_DISCUSSION, drdoc.parentId, 'nReply', -1),
|
||||||
]);
|
]);
|
||||||
|
|||||||
+17
-12
@@ -1,31 +1,36 @@
|
|||||||
|
const fs = require('fs');
|
||||||
const { ForbiddenError } = require('../error');
|
const { ForbiddenError } = require('../error');
|
||||||
const db = require('../service/db');
|
const db = require('../service/db');
|
||||||
const fs = require('../service/gridfs');
|
const gridfs = require('../service/gridfs');
|
||||||
const hash = require('../lib/hash.hydro');
|
const hash = require('../lib/hash.hydro');
|
||||||
|
|
||||||
const coll = db.collection('file');
|
const coll = db.collection('file');
|
||||||
|
const collFile = db.collection('fs.files');
|
||||||
|
const collChunk = db.collection('fs.chunks');
|
||||||
|
|
||||||
function _timestamp() {
|
function _timestamp() {
|
||||||
return Math.floor(Number(new Date()) / 1000000);
|
return Math.floor(Number(new Date()) / 1000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function add(stream, filename, meta = {}) {
|
async function add(streamOrPath, filename, meta = {}) {
|
||||||
const file = await coll.insertOne(meta);
|
if (typeof streamOrPath === 'string') streamOrPath = fs.createReadStream(streamOrPath);
|
||||||
const w = fs.openUploadStreamWithId(file.insertedId, filename);
|
const w = gridfs.openUploadStream(filename);
|
||||||
|
await coll.insertOne({ ...meta, _id: w.id });
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
stream.on('end', resolve);
|
w.on('error', reject);
|
||||||
stream.on('error', reject);
|
w.on('finish', resolve);
|
||||||
stream.pipe(w);
|
streamOrPath.pipe(w);
|
||||||
});
|
});
|
||||||
const c = await fs.find({ _id: file.insertedId }).toArray();
|
const c = await gridfs.find({ _id: w.id }).toArray();
|
||||||
await coll.updateOne({ _id: file.insertedId }, { $set: { md5: c[0].md5, size: c[0].size } });
|
await coll.updateOne({ _id: w.id }, { $set: { md5: c[0].md5, size: c[0].size } });
|
||||||
return file.insertedId;
|
return w.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
function del(_id) {
|
function del(_id) {
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
coll.deleteOne({ _id }),
|
coll.deleteOne({ _id }),
|
||||||
fs.delete(_id),
|
collFile.deleteOne({ _id }),
|
||||||
|
collChunk.deleteMany({ files_id: _id }),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +63,7 @@ async function get(_id, secret) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fs.openDownloadStream(_id);
|
return gridfs.openDownloadStream(_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMeta(_id) {
|
function getMeta(_id) {
|
||||||
|
|||||||
+13
-8
@@ -3,6 +3,7 @@ const file = require('./file');
|
|||||||
const document = require('./document');
|
const document = require('./document');
|
||||||
const domain = require('./domain');
|
const domain = require('./domain');
|
||||||
const { ProblemNotFoundError } = require('../error');
|
const { ProblemNotFoundError } = require('../error');
|
||||||
|
const readConfig = require('../lib/readConfig');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('../interface').Pdoc} Pdoc
|
* @typedef {import('../interface').Pdoc} Pdoc
|
||||||
@@ -30,9 +31,13 @@ async function add(domainId, title, content, owner, {
|
|||||||
}) {
|
}) {
|
||||||
const d = await domain.inc(domainId, 'pidCounter', 1);
|
const d = await domain.inc(domainId, 'pidCounter', 1);
|
||||||
if (!pid) pid = d.pidCounter.toString();
|
if (!pid) pid = d.pidCounter.toString();
|
||||||
return await document.add(domainId, content, owner, document.TYPE_PROBLEM, d.pidCounter, null, null, {
|
// eslint-disable-next-line no-return-await
|
||||||
pid, title, data, category, tag, hidden, nSubmit: 0, nAccept: 0,
|
return await document.add(
|
||||||
});
|
domainId, content, owner, document.TYPE_PROBLEM, d.pidCounter, null, null,
|
||||||
|
{
|
||||||
|
pid, title, data, category, tag, hidden, nSubmit: 0, nAccept: 0,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -148,12 +153,12 @@ async function updateStatus(domainId, pid, uid, rid, status) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setTestdata(domainId, _id, readStream) {
|
async function setTestdata(domainId, _id, filePath) {
|
||||||
// TODO read config in zipfile
|
|
||||||
const pdoc = await get(domainId, _id);
|
const pdoc = await get(domainId, _id);
|
||||||
const id = await file.add(readStream, 'data.zip');
|
const config = await readConfig(filePath);
|
||||||
if (pdoc.data && typeof pdoc.data === 'object') file.dec(this.pdoc.data);
|
const id = await file.add(filePath, 'data.zip');
|
||||||
return await edit(domainId, _id, { data: id }); // eslint-disable-line no-return-await
|
if (pdoc.data && typeof pdoc.data === 'object') file.dec(pdoc.data);
|
||||||
|
return await edit(domainId, _id, { data: id, config });
|
||||||
}
|
}
|
||||||
|
|
||||||
global.Hydro.model.problem = module.exports = {
|
global.Hydro.model.problem = module.exports = {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
const moment = require('moment-timezone');
|
const moment = require('moment-timezone');
|
||||||
const builtin = require('./builtin');
|
const builtin = require('./builtin');
|
||||||
const options = require('../options');
|
const options = require('../options');
|
||||||
const i18n = require('../lib/i18n');
|
|
||||||
|
|
||||||
const countries = moment.tz.countries();
|
const countries = moment.tz.countries();
|
||||||
const tzs = new Set();
|
const tzs = new Set();
|
||||||
|
|||||||
+1
-1
@@ -112,7 +112,7 @@ async function changePassword(uid, currentPassword, newPassword) {
|
|||||||
const udoc = await getById(uid);
|
const udoc = await getById(uid);
|
||||||
udoc.checkPassword(currentPassword);
|
udoc.checkPassword(currentPassword);
|
||||||
const salt = String.random();
|
const salt = String.random();
|
||||||
return await coll.findOneAndUpdate({ // eslint-disable-line no-return-await
|
return await coll.findOneAndUpdate({
|
||||||
_id: udoc._id,
|
_id: udoc._id,
|
||||||
}, {
|
}, {
|
||||||
$set: { salt, hash: pwhash.hash(newPassword, salt), hashType: 'hydro' },
|
$set: { salt, hash: pwhash.hash(newPassword, salt), hashType: 'hydro' },
|
||||||
|
|||||||
+2
-1
@@ -6,6 +6,7 @@
|
|||||||
"author": "masnn",
|
"author": "masnn",
|
||||||
"license": "GPL-3.0-only",
|
"license": "GPL-3.0-only",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"adm-zip": "^0.4.14",
|
||||||
"axios": "^0.19.0",
|
"axios": "^0.19.0",
|
||||||
"bson": "^4.0.2",
|
"bson": "^4.0.2",
|
||||||
"js-yaml": "^3.13.1",
|
"js-yaml": "^3.13.1",
|
||||||
@@ -49,4 +50,4 @@
|
|||||||
".build/module/**"
|
".build/module/**"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -234,6 +234,11 @@ acorn@^7.1.1:
|
|||||||
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.1.tgz#e35668de0b402f359de515c5482a1ab9f89a69bf"
|
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.1.tgz#e35668de0b402f359de515c5482a1ab9f89a69bf"
|
||||||
integrity sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==
|
integrity sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==
|
||||||
|
|
||||||
|
adm-zip@^0.4.14:
|
||||||
|
version "0.4.14"
|
||||||
|
resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.4.14.tgz#2cf312bcc9f8875df835b0f6040bd89be0a727a9"
|
||||||
|
integrity sha512-/9aQCnQHF+0IiCl0qhXoK7qs//SwYE7zX8lsr/DNk1BRAHYxeLZPL4pguwK29gUEqasYQjqPtEpDRSWEkdHn9g==
|
||||||
|
|
||||||
ajv-errors@^1.0.0:
|
ajv-errors@^1.0.0:
|
||||||
version "1.0.1"
|
version "1.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/ajv-errors/-/ajv-errors-1.0.1.tgz#f35986aceb91afadec4102fbd85014950cefa64d"
|
resolved "https://registry.yarnpkg.com/ajv-errors/-/ajv-errors-1.0.1.tgz#f35986aceb91afadec4102fbd85014950cefa64d"
|
||||||
|
|||||||
Reference in new issue
Block a user