core: contest: fix export_code for submit_answer

This commit is contained in:
undefined committed 2022-12-16 15:48:33 +08:00
1 parent 4958655ac9
commit 58a5cc0f9e
2 files changed
+19 -8

No files matched your search

+15 -4
View File
@@ -3,7 +3,9 @@ import { statSync } from 'fs-extra';
import { pick } from 'lodash';
import moment from 'moment-timezone';
import { ObjectID } from 'mongodb';
import { Counter, sortFiles, Time } from '@hydrooj/utils/lib/utils';
import {
Counter, sortFiles, streamToBuffer, Time,
} from '@hydrooj/utils/lib/utils';
import {
BadRequestError, ContestNotEndedError, ContestNotFoundError, ContestNotLiveError,
ContestScoreboardHiddenError, FileLimitExceededError, FileUploadError, InvalidTokenError,
@@ -442,9 +444,18 @@ export class ContestCodeHandler extends Handler {
const rdocs = await record.getMulti(domainId, {
_id: { $in: Array.from(Object.keys(rnames)).map((id) => new ObjectID(id)) },
}).toArray();
for (const rdoc of rdocs) {
zip.addFile(`${rnames[rdoc._id.toHexString()]}.${rdoc.lang}`, Buffer.from(rdoc.code));
}
await Promise.all(rdocs.map(async (rdoc) => {
if (rdoc.files?.code) {
const [id, filename] = rdoc.files?.code?.split('#') || [];
if (!id) return;
zip.addFile(
`${rnames[rdoc._id.toHexString()]}.${filename || 'txt'}`,
await streamToBuffer(storage.get(id)),
);
} else if (rdoc.code) {
zip.addFile(`${rnames[rdoc._id.toHexString()]}.${rdoc.lang}`, Buffer.from(rdoc.code));
}
}));
this.binary(zip.toBuffer(), `${tdoc.title}.zip`);
}
}
+4 -4
View File
@@ -527,13 +527,13 @@ export class ProblemSubmitHandler extends ProblemDetailHandler {
if (!file || file.size === 0) throw new ValidationError('code');
const sizeLimit = config.type === 'submit_answer' ? 128 * 1024 * 1024 : 65535;
if (file.size > sizeLimit) throw new ValidationError('file');
if (file.size > 65535 || await isBinaryFile(file.filepath, file.size)) {
if (config.type !== 'submit_answer' || (file.size < 65535 && !await isBinaryFile(file.filepath, file.size))) {
// TODO auto detect & convert encoding
code = await readFile(file.filepath, 'utf-8');
} else {
const id = nanoid();
await storage.put(`submission/${this.user._id}/${id}`, file.filepath, this.user._id);
files.code = `${this.user._id}/${id}#${file.originalFilename}`;
} else {
// TODO auto detect & convert encoding
code = await readFile(file.filepath, 'utf-8');
}
}
const rid = await record.add(