mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 17:46:28 +08:00
core: catch unzip error
This commit is contained in:
1 file changed
+7
-4
@@ -9,6 +9,7 @@ import { escapeRegExp, pick } from 'lodash';
|
|||||||
import { Filter, ObjectId } from 'mongodb';
|
import { Filter, ObjectId } from 'mongodb';
|
||||||
import type { Readable } from 'stream';
|
import type { Readable } from 'stream';
|
||||||
import { Logger, size, streamToBuffer } from '@hydrooj/utils/lib/utils';
|
import { Logger, size, streamToBuffer } from '@hydrooj/utils/lib/utils';
|
||||||
|
import { Context } from '../context';
|
||||||
import { FileUploadError, ProblemNotFoundError, ValidationError } from '../error';
|
import { FileUploadError, ProblemNotFoundError, ValidationError } from '../error';
|
||||||
import type {
|
import type {
|
||||||
Document, ProblemDict, ProblemStatusDoc, User,
|
Document, ProblemDict, ProblemStatusDoc, User,
|
||||||
@@ -446,6 +447,7 @@ export class ProblemModel {
|
|||||||
static async import(domainId: string, filepath: string, operator = 1, preferredPrefix?: string) {
|
static async import(domainId: string, filepath: string, operator = 1, preferredPrefix?: string) {
|
||||||
let tmpdir = '';
|
let tmpdir = '';
|
||||||
let del = false;
|
let del = false;
|
||||||
|
try {
|
||||||
if (filepath.endsWith('.zip')) {
|
if (filepath.endsWith('.zip')) {
|
||||||
tmpdir = path.join(os.tmpdir(), 'hydro', `${Math.random()}.import`);
|
tmpdir = path.join(os.tmpdir(), 'hydro', `${Math.random()}.import`);
|
||||||
let zip: AdmZip;
|
let zip: AdmZip;
|
||||||
@@ -466,7 +468,6 @@ export class ProblemModel {
|
|||||||
} else {
|
} else {
|
||||||
throw new ValidationError('file', null, 'Invalid file');
|
throw new ValidationError('file', null, 'Invalid file');
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
const problems = await fs.readdir(tmpdir, { withFileTypes: true });
|
const problems = await fs.readdir(tmpdir, { withFileTypes: true });
|
||||||
for (const p of problems) {
|
for (const p of problems) {
|
||||||
if (process.env.HYDRO_CLI) logger.info(`Importing problem ${p.name}`);
|
if (process.env.HYDRO_CLI) logger.info(`Importing problem ${p.name}`);
|
||||||
@@ -592,16 +593,17 @@ export class ProblemModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bus.on('problem/addTestdata', async (domainId, docId, name) => {
|
export function apply(ctx: Context) {
|
||||||
|
ctx.on('problem/addTestdata', async (domainId, docId, name) => {
|
||||||
if (!['config.yaml', 'config.yml', 'Config.yaml', 'Config.yml'].includes(name)) return;
|
if (!['config.yaml', 'config.yml', 'Config.yaml', 'Config.yml'].includes(name)) return;
|
||||||
const buf = await storage.get(`problem/${domainId}/${docId}/testdata/${name}`);
|
const buf = await storage.get(`problem/${domainId}/${docId}/testdata/${name}`);
|
||||||
await ProblemModel.edit(domainId, docId, { config: (await streamToBuffer(buf)).toString() });
|
await ProblemModel.edit(domainId, docId, { config: (await streamToBuffer(buf)).toString() });
|
||||||
});
|
});
|
||||||
bus.on('problem/delTestdata', async (domainId, docId, names) => {
|
ctx.on('problem/delTestdata', async (domainId, docId, names) => {
|
||||||
if (!names.includes('config.yaml')) return;
|
if (!names.includes('config.yaml')) return;
|
||||||
await ProblemModel.edit(domainId, docId, { config: '' });
|
await ProblemModel.edit(domainId, docId, { config: '' });
|
||||||
});
|
});
|
||||||
bus.on('problem/renameTestdata', async (domainId, docId, file, newName) => {
|
ctx.on('problem/renameTestdata', async (domainId, docId, file, newName) => {
|
||||||
if (['config.yaml', 'config.yml', 'Config.yaml', 'Config.yml'].includes(file)) {
|
if (['config.yaml', 'config.yml', 'Config.yaml', 'Config.yml'].includes(file)) {
|
||||||
await ProblemModel.edit(domainId, docId, { config: '' });
|
await ProblemModel.edit(domainId, docId, { config: '' });
|
||||||
}
|
}
|
||||||
@@ -610,6 +612,7 @@ bus.on('problem/renameTestdata', async (domainId, docId, file, newName) => {
|
|||||||
await ProblemModel.edit(domainId, docId, { config: (await streamToBuffer(buf)).toString() });
|
await ProblemModel.edit(domainId, docId, { config: (await streamToBuffer(buf)).toString() });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
global.Hydro.model.problem = ProblemModel;
|
global.Hydro.model.problem = ProblemModel;
|
||||||
export default ProblemModel;
|
export default ProblemModel;
|
||||||
Reference in new issue
Block a user