/* 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 xml2js from 'xml2js'; import { ObjectID } from 'bson'; import { filter } from 'lodash'; import decodeHTML from 'decode-html'; import { LocalProblemConfig } from 'hydrooj'; import { Route, Handler, param, Types, } from 'hydrooj/dist/service/server'; import { streamToBuffer } from 'hydrooj/dist/utils'; import { ProblemAdd } from 'hydrooj/dist/lib/ui'; import * as file from 'hydrooj/dist/model/file'; import { PERM } from 'hydrooj/dist/model/builtin'; const processing = {}; class FpsProblemImportHandler extends Handler { @param('ufid', Types.ObjectID, true) async get(domainId: string, ufid?: ObjectID) { if (ufid) { if (processing[ufid.toHexString()]) { this.response.body = 'Processing'; return; } const stream = await file.get(ufid); processing[ufid.toHexString()] = true; try { const buf = await streamToBuffer(stream); let input = buf.toString(); try { await xml2js.parseStringPromise(input); } catch (e) { const zip = new AdmZip(buf); const entries = zip.getEntries(); for (const entry of entries) { if (entry.entryName.endsWith('.xml')) { input = entry.getData().toString(); break; } } } // @ts-ignore await this.post({ domainId, input }); await file.del(ufid); } catch (e) { processing[ufid.toHexString()] = false; throw e; } } else this.response.template = 'problem_import_fps.html'; } @param('input', Types.String) async post(domainId: string, input: string) { const result = await xml2js.parseStringPromise(input); const problem = global.Hydro.model.problem; for (const p of result.fps.item) { const testdata = new AdmZip(); const content = []; if (p.description) { content.push( `
${p.sample_input[0]}
`,
`${p.sample_output[0]}
`,
);
} else {
for (let i = 0; i < p.sample_input.length; i++) {
content.push(
`${p.sample_input[i]}
`,
`${p.sample_output[i]}
`,
);
}
}
}
if (p.hint) {
content.push(
`