mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
37 lines
1.6 KiB
TypeScript
37 lines
1.6 KiB
TypeScript
import { sleep } from '@hydrooj/utils';
|
|
import { Context } from '../context';
|
|
import { ValidationError } from '../error';
|
|
import { PERM, PRIV } from '../model/builtin';
|
|
import MessageModel from '../model/message';
|
|
import problem from '../model/problem';
|
|
import { Handler, param, Types } from '../service/server';
|
|
|
|
class ProblemImportHydroHandler extends Handler {
|
|
async get() {
|
|
this.response.template = 'problem_import.html';
|
|
}
|
|
|
|
@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 (!this.request.files.file) throw new ValidationError('file');
|
|
if (preferredPrefix && !/^[a-zA-Z]+$/.test(preferredPrefix)) throw new ValidationError('preferredPrefix');
|
|
const promise = problem.import(
|
|
domainId, this.request.files.file.filepath, keepUser ? null : this.user._id,
|
|
preferredPrefix, this.progress.bind(this),
|
|
).catch((e) => MessageModel.send(1, this.user._id, `Import failed: ${e.message}\n${e.stack}`));
|
|
let resolved = false;
|
|
await Promise.race([
|
|
promise.then(() => { resolved = true; }),
|
|
sleep(5000),
|
|
]);
|
|
this.response.redirect = this.url('problem_main', resolved ? {} : { query: { showImport: 1 } });
|
|
}
|
|
}
|
|
|
|
export async function apply(ctx: Context) {
|
|
ctx.Route('problem_import_hydro', '/problem/import/hydro', ProblemImportHydroHandler, PERM.PERM_CREATE_PROBLEM);
|
|
ctx.injectUI('ProblemAdd', 'problem_import_hydro', { icon: 'copy', text: 'Import From Hydro' });
|
|
}
|