diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index ebaeb3905..c177bd5f9 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.13.39", + "version": "2.13.40", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", diff --git a/packages/import-qduoj/handler.ts b/packages/import-qduoj/handler.ts new file mode 100644 index 000000000..bd4ded6fb --- /dev/null +++ b/packages/import-qduoj/handler.ts @@ -0,0 +1,127 @@ +/* eslint-disable no-await-in-loop */ +import path from 'path'; +import os from 'os'; +import fs from 'fs-extra'; +import AdmZip from 'adm-zip'; +import yaml from 'js-yaml'; +import { ObjectID } from 'mongodb'; +import { LocalProblemConfig } from 'hydrooj'; +import { + Route, Handler, param, Types, +} from 'hydrooj/dist/service/server'; +import { BadRequestError } from 'hydrooj/dist/error'; +import { streamToBuffer } from 'hydrooj/dist/utils'; +import { ProblemAdd } from 'hydrooj/dist/lib/ui'; +import * as file from 'hydrooj/dist/model/file'; +import * as problem from 'hydrooj/dist/model/problem'; +import { PERM } from 'hydrooj/dist/model/builtin'; + +const processing = {}; +fs.ensureDirSync('/tmp/hydro/import-qduoj'); + +class ImportQduojHandler extends Handler { + async ImportFromFile(domainId: string, id: string, zipfile: Buffer) { + const zip = new AdmZip(zipfile); + const tmp = path.resolve(os.tmpdir(), 'hydro', 'import-qduoj', id); + await new Promise((resolve, reject) => { + zip.extractAllToAsync(tmp, true, (err) => (err ? resolve() : reject(err))); + }); + try { + const folders = await fs.readdir(tmp); + for (const folder of folders) { + const buf = await fs.readFile(path.join(tmp, folder, 'problem.json')); + const pdoc = JSON.parse(buf.toString()); + const content = [ + ...pdoc.description?.value + ? [ + '
`, + sample.input, + `
`, + sample.output, + '', + ].join('\n')), + ...pdoc.hint?.value + ? [ + '
{{ _('With this feature, you can import problems that you can view from a site to here. Their title, content, tags and categories will be imported.') }}
+