mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: add preferredPrefix option for problem_import
This commit is contained in:
2 files changed
+19
-6
No files matched your search
@@ -2,17 +2,20 @@ import { Context } from '../context';
|
|||||||
import { ValidationError } from '../error';
|
import { ValidationError } from '../error';
|
||||||
import { PERM, PRIV } from '../model/builtin';
|
import { PERM, PRIV } from '../model/builtin';
|
||||||
import problem from '../model/problem';
|
import problem from '../model/problem';
|
||||||
import { Handler } from '../service/server';
|
import { Handler, param, Types } from '../service/server';
|
||||||
|
|
||||||
class ProblemImportHydroHandler extends Handler {
|
class ProblemImportHydroHandler extends Handler {
|
||||||
async get() {
|
async get() {
|
||||||
this.response.template = 'problem_import.html';
|
this.response.template = 'problem_import.html';
|
||||||
}
|
}
|
||||||
|
|
||||||
async post({ domainId, keepUser }) {
|
@param('keepUser', Types.Boolean)
|
||||||
|
@param('preferredPrefix', Types.String, true)
|
||||||
|
async post(domainId: string, keepUser: boolean, preferredPrefix?: string) {
|
||||||
if (keepUser) this.checkPriv(PRIV.PRIV_EDIT_SYSTEM);
|
if (keepUser) this.checkPriv(PRIV.PRIV_EDIT_SYSTEM);
|
||||||
if (!this.request.files.file) throw new ValidationError('file');
|
if (!this.request.files.file) throw new ValidationError('file');
|
||||||
await problem.import(domainId, this.request.files.file.filepath, keepUser ? null : this.user._id);
|
if (preferredPrefix && !/^[a-zA-Z]+$/.test(preferredPrefix)) throw new ValidationError('preferredPrefix');
|
||||||
|
await problem.import(domainId, this.request.files.file.filepath, keepUser ? null : this.user._id, preferredPrefix);
|
||||||
this.response.redirect = this.url('problem_main');
|
this.response.redirect = this.url('problem_main');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -407,7 +407,7 @@ export class ProblemModel {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async import(domainId: string, filepath: string, operator = 1) {
|
static async import(domainId: string, filepath: string, operator = 1, preferredPrefix?: string) {
|
||||||
let tmpdir = '';
|
let tmpdir = '';
|
||||||
let del = false;
|
let del = false;
|
||||||
if (filepath.endsWith('.zip')) {
|
if (filepath.endsWith('.zip')) {
|
||||||
@@ -441,9 +441,19 @@ export class ProblemModel {
|
|||||||
const pdoc: ProblemDoc = yaml.load(content) as any;
|
const pdoc: ProblemDoc = yaml.load(content) as any;
|
||||||
if (!pdoc) continue;
|
if (!pdoc) continue;
|
||||||
let pid = pdoc.pid;
|
let pid = pdoc.pid;
|
||||||
|
|
||||||
|
const isValidPid = async (id: string) => {
|
||||||
|
if (!(/^[A-Za-z]+[0-9A-Za-z]*$/.test(id))) return false;
|
||||||
|
if (await ProblemModel.get(domainId, id)) return false;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
if (pid) {
|
if (pid) {
|
||||||
if (!(/^[A-Za-z]+[0-9A-Za-z]*$/.test(pid))) pid = undefined;
|
if (preferredPrefix) {
|
||||||
else if (await ProblemModel.get(domainId, pid)) pid = undefined;
|
const newPid = pid.replace(/^[A-Za-z]+/, preferredPrefix);
|
||||||
|
if (isValidPid(newPid)) pid = newPid;
|
||||||
|
}
|
||||||
|
if (!isValidPid(pid)) pid = undefined;
|
||||||
}
|
}
|
||||||
const overrideContent = findOverrideContent(path.join(tmpdir, i));
|
const overrideContent = findOverrideContent(path.join(tmpdir, i));
|
||||||
const docId = await ProblemModel.add(
|
const docId = await ProblemModel.add(
|
||||||
|
|||||||
Reference in new issue
Block a user