From 7358d427e0c1a485d0a0a9c3a6304b633b4f4175 Mon Sep 17 00:00:00 2001 From: undefined Date: Mon, 18 Mar 2024 05:21:09 +0800 Subject: [PATCH] fps-importer: allow setting --- packages/fps-importer/index.ts | 25 ++++++++++++++++--- packages/fps-importer/package.json | 2 +- .../template/problem_import_fps.html | 3 +++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/fps-importer/index.ts b/packages/fps-importer/index.ts index fbde0523e..3c5f8914c 100644 --- a/packages/fps-importer/index.ts +++ b/packages/fps-importer/index.ts @@ -2,8 +2,8 @@ import decodeHTML from 'decode-html'; import xml2js from 'xml2js'; import { - _, AdmZip, BadRequestError, buildContent, ContentNode, Context, FileTooLargeError, fs, - Handler, PERM, ProblemConfigFile, ProblemModel, ProblemType, SolutionModel, ValidationError, yaml, + _, AdmZip, BadRequestError, buildContent, ContentNode, Context, FileTooLargeError, fs, Handler, + PERM, ProblemConfigFile, ProblemModel, ProblemType, SettingModel, SolutionModel, SystemModel, ValidationError, yaml, } from 'hydrooj'; const knownRemoteMapping = { @@ -111,7 +111,7 @@ class FpsProblemImportHandler extends Handler { const tasks = []; try { const file = await fs.stat(this.request.files.file.filepath); - if (file.size > 64 * 1024 * 1024) throw new FileTooLargeError('64m'); + if (file.size > SystemModel.get('import-fps.limit')) throw new FileTooLargeError(); const content = fs.readFileSync(this.request.files.file.filepath, 'utf-8'); const result = await xml2js.parseStringPromise(content); tasks.push(result); @@ -127,7 +127,7 @@ class FpsProblemImportHandler extends Handler { for (const entry of zip.getEntries()) { try { const buf = entry.getData(); - if (buf.byteLength > 64 * 1024 * 1024) throw new FileTooLargeError('64m'); + if (buf.byteLength > SystemModel.get('import-fps.limit')) throw new FileTooLargeError(); const content = buf.toString(); const result = await xml2js.parseStringPromise(content); tasks.push(result); @@ -145,5 +145,22 @@ export async function apply(ctx: Context) { ctx.injectUI('ProblemAdd', 'problem_import_fps', { icon: 'copy', text: 'From FPS File' }); ctx.i18n.load('zh', { 'From FPS File': '从 FPS 文件导入', + 'problem.import.fps.hint1': '我们推荐的最大导入大小为 64MiB,若文件超出此大小,强烈建议您在本机使用 EasyFPSViewer 等工具将其拆分或是移除测试数据后单独上传。', + 'problem.import.fps.hint2': '由于 xml 格式无法随机读写,解析需要消耗大量内存,在内存过小的机器上导入大型题目包很可能导致崩溃或死机。', + 'problem.import.fps.hint3': '若您确有需要,此限制可在系统设置中更改。我们建议您使用 Hydro 自带的 zip 格式存储或是交换题目。', + }); + ctx.i18n.load('en', { + 'From FPS File': 'Import from FPS File', + 'problem.import.fps.hint1': 'We recommend that the maximum import size is 64MiB. If the file exceeds this size, \ +we strongly recommend that you use tools such as EasyFPSViewer to split it or remove the testdata and upload it separately on your local machine.', + 'problem.import.fps.hint2': 'Since the xml format cannot be read randomly, parsing requires a large amount of memory. \ +Importing a large problem set on a machine with insufficient memory may cause a crash or freeze.', + 'problem.import.fps.hint3': 'If you really need it, this limit can be changed in the system settings. \ +We strongly recommend that you use the zip format to store or exchange problemsets.', + }); + ctx.inject(['setting'], (c) => { + c.setting.SystemSetting( + SettingModel.Setting('setting_limits', 'import-fps.limit', 64 * 1024 * 1024, 'text', 'Maximum file size for FPS problemset import'), + ); }); } diff --git a/packages/fps-importer/package.json b/packages/fps-importer/package.json index 541cd301e..967064117 100644 --- a/packages/fps-importer/package.json +++ b/packages/fps-importer/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/fps-importer", - "version": "1.5.10", + "version": "1.5.11", "description": "Import FPS problems", "main": "index.ts", "repository": "https://github.com/hydro-dev/Hydro.git", diff --git a/packages/fps-importer/template/problem_import_fps.html b/packages/fps-importer/template/problem_import_fps.html index 75bacaef7..4ace15a7c 100644 --- a/packages/fps-importer/template/problem_import_fps.html +++ b/packages/fps-importer/template/problem_import_fps.html @@ -9,6 +9,9 @@

+

{{ _('problem.import.fps.hint1') }}

+

{{ _('problem.import.fps.hint2') }}

+

{{ _('problem.import.fps.hint3') }}