core: fix testdata zip upload

This commit is contained in:
undefined committed 2021-03-29 23:02:59 +08:00
1 parent 777d648f63
commit b833efc8c9
4 files changed
+16 -2

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.20.46",
"version": "2.20.47",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",
+1
View File
@@ -384,6 +384,7 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
const zip = new AdmZip(this.request.files.file.path);
const entries = zip.getEntries();
for (const entry of entries) {
if (!entry.name) continue;
if (type === 'testdata') {
// eslint-disable-next-line no-await-in-loop
await problem.addTestdata(domainId, this.pdoc.docId, entry.name, entry.getData());
+3 -1
View File
@@ -8,7 +8,7 @@ import { buildProjection } from '../utils';
import type { ProblemStatusDoc, Pdict, Document } from '../interface';
import { Content } from '../loader';
import { ArrayKeys, NumberKeys, Projection } from '../typeutils';
import { ProblemNotFoundError } from '../error';
import { ProblemNotFoundError, ValidationError } from '../error';
import storage from '../service/storage';
import * as bus from '../service/bus';
@@ -134,11 +134,13 @@ export function del(domainId: string, docId: number) {
}
export async function addTestdata(domainId: string, pid: number, name: string, f: Readable | Buffer | string) {
if (!name) throw new ValidationError('name');
const [[, fileinfo]] = await Promise.all([
document.getSub(domainId, document.TYPE_PROBLEM, pid, 'data', name),
storage.put(`problem/${domainId}/${pid}/testdata/${name}`, f),
]);
const meta = await storage.getMeta(`problem/${domainId}/${pid}/testdata/${name}`);
if (!meta) throw new Error('Upload failed');
const payload = { name, ...pick(meta, ['size', 'lastModified', 'etag']) };
if (!fileinfo) await push(domainId, pid, 'data', { _id: name, ...payload });
else await document.setSub(domainId, document.TYPE_PROBLEM, pid, 'data', name, payload);
+11
View File
@@ -232,6 +232,17 @@ const scripts: UpgradeScript[] = [
await db.collection('domain.user').deleteMany({ uid: null });
await db.collection('domain.user').deleteMany({ uid: 0 });
},
async function _15_16() {
const _FRESH_INSTALL_IGNORE = 1;
await iterateAllProblem(['data', 'additional_file'], async (pdoc) => {
const $set: any = {};
const td = pdoc.data.filter((f) => f.name);
if (JSON.stringify(td) !== JSON.stringify(pdoc.data)) $set.data = td;
const af = pdoc.additional_file.filter((f) => f.name);
if (JSON.stringify(af) !== JSON.stringify(pdoc.additional_file)) $set.additional_file = af;
if (Object.keys($set).length) await problem.edit(pdoc.domainId, pdoc.docId, $set);
});
},
];
export = scripts;