diff --git a/packages/hydrooj/src/handler/contest.ts b/packages/hydrooj/src/handler/contest.ts index aa0683d97..89448a834 100644 --- a/packages/hydrooj/src/handler/contest.ts +++ b/packages/hydrooj/src/handler/contest.ts @@ -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`); } } diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index 82a2f0751..f20c95e78 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -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(