core: import: show progress

This commit is contained in:
undefined committed 2024-08-03 00:09:15 +08:00
1 parent 24e3905643
commit f266542dec
4 files changed
+23 -9

No files matched your search

+12 -2
View File
@@ -1,6 +1,8 @@
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';
@@ -15,8 +17,16 @@ class ProblemImportHydroHandler extends Handler {
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');
await problem.import(domainId, this.request.files.file.filepath, keepUser ? null : this.user._id, preferredPrefix);
this.response.redirect = this.url('problem_main');
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 } });
}
}
+6 -2
View File
@@ -445,7 +445,7 @@ export class ProblemModel {
return true;
}
static async import(domainId: string, filepath: string, operator = 1, preferredPrefix?: string) {
static async import(domainId: string, filepath: string, operator = 1, preferredPrefix?: string, progress = null) {
let tmpdir = '';
let del = false;
try {
@@ -527,7 +527,11 @@ export class ProblemModel {
if (count > 5) continue;
await RecordModel.add(domainId, docId, operator, f.name.split('.')[1], await fs.readFile(loc, 'utf-8'), true);
}
if (process.env.HYDRO_CLI) logger.info(`Imported problem ${pdoc.pid} (${pdoc.title})`);
if (process.env.HYDRO_CLI) {
logger.info(`Imported problem ${pdoc.pid} (${pdoc.title})`);
} else {
progress?.(`Imported problem ${pdoc.pid} (${pdoc.title})`);
}
}
} finally {
if (del) await fs.remove(tmpdir);
+1 -1
View File
@@ -249,7 +249,7 @@ export async function apply(ctx: Context) {
checkPriv(...args: number[]) {
if (!this.user.hasPriv(...args)) throw new PrivilegeError(...args);
},
progress(message: string, params: any[]) {
progress(message: string, params: any[] = []) {
Hydro.model.message.sendInfo(this.user._id, JSON.stringify({ message, params }));
},
async limitRate(