core: model.problem.add/del AdditionalFile

This commit is contained in:
undefined committed 2021-01-24 23:38:37 +08:00
1 parent 25e7e00518
commit 9e92891fb2
5 files changed
+34 -43

No files matched your search

+4 -1
View File
@@ -75,7 +75,10 @@ export async function load(call: Entry) {
logger.info('Upgrading database: from %d to %d', dbVer, expected); logger.info('Upgrading database: from %d to %d', dbVer, expected);
if (isFresh) { if (isFresh) {
const func = scripts[dbVer].toString(); const func = scripts[dbVer].toString();
if (func.includes('_FRESH_INSTALL_IGNORE')) continue; if (func.includes('_FRESH_INSTALL_IGNORE')) {
dbVer++;
continue;
}
} }
const result = await scripts[dbVer](); const result = await scripts[dbVer]();
if (!result) break; if (!result) break;
+3 -2
View File
@@ -92,7 +92,7 @@ export interface User extends Dictionary<any> {
export type Udict = NumericDictionary<User>; export type Udict = NumericDictionary<User>;
export interface ProblemData { export interface FileInfo {
name: string, name: string,
size: number, size: number,
} }
@@ -172,7 +172,8 @@ declare module './model/problem' {
nAccept: number, nAccept: number,
tag: string[], tag: string[],
category: string[], category: string[],
data: ProblemData[], data: FileInfo[],
additional_file: FileInfo[],
hidden: boolean, hidden: boolean,
acMsg?: string, acMsg?: string,
html?: boolean, html?: boolean,
+14
View File
@@ -158,6 +158,18 @@ export async function delTestdata(domainId: string, pid: number, name: string |
await pull(domainId, pid, 'data', names); await pull(domainId, pid, 'data', names);
} }
export async function addAdditionalFile(domainId: string, pid: number, name: string, f: Readable | Buffer | string) {
await storage.put(`problem/${domainId}/${pid}/additional_file/${name}`, f);
const meta = await storage.getMeta(`problem/${domainId}/${pid}/additional_file/${name}`);
return await push(domainId, pid, 'additional_file', meta);
}
export async function delAdditionalFile(domainId: string, pid: number, name: string | string[]) {
const names = name instanceof Array ? name : [name];
await storage.del(names.map((t) => `problem/${domainId}/${pid}/additional_file/${t}`));
await pull(domainId, pid, 'additional_file', names);
}
export async function random(domainId: string, query: FilterQuery<Pdoc>): Promise<string | null> { export async function random(domainId: string, query: FilterQuery<Pdoc>): Promise<string | null> {
const cursor = document.getMulti(domainId, document.TYPE_PROBLEM, query); const cursor = document.getMulti(domainId, document.TYPE_PROBLEM, query);
const pcount = await cursor.count(); const pcount = await cursor.count();
@@ -239,6 +251,8 @@ global.Hydro.model.problem = {
random, random,
addTestdata, addTestdata,
delTestdata, delTestdata,
addAdditionalFile,
delAdditionalFile,
getMulti, getMulti,
getList, getList,
getListStatus, getListStatus,
+1 -28
View File
@@ -119,33 +119,6 @@ const scripts: UpgradeScript[] = [
} }
domainProgress.stop(); domainProgress.stop();
} }
const udocs = await user.getMulti().project({ _id: 1, uname: 1 }).toArray();
const userfileProgress = Progress.create({
items: udocs.length, title: 'Users', y: 3,
});
for (const udoc of udocs) {
userfileProgress.startItem(`${udoc._id}: ${udoc.uname}`);
const ufdocs = await coll.find({ owner: udoc._id }).toArray();
const currentUser = Progress.create({
items: ufdocs.length, title: `${udoc._id}: ${udoc.uname}`, y: 4, eta: true,
});
const namelist = [];
for (const ufdoc of ufdocs) {
currentUser.startItem(`${ufdoc._id}: ${ufdoc.filename || ''}`);
ufdoc.filename = ufdoc.filename || ufdoc._id.toHexString();
const file = await streamToBuffer(gridfs.openDownloadStream(ufdoc._id));
if (!namelist.includes(ufdoc.filename)) {
await storage.put(`user/${udoc._id}/${ufdoc.filename}`, file);
} else {
await storage.put(`user/${udoc._id}/${ufdoc._id}`, file);
}
namelist.push(ufdoc.filename);
currentUser.itemDone(`${ufdoc._id}: ${ufdoc.filename || ''}`);
}
currentUser.stop();
userfileProgress.itemDone(`${udoc._id}: ${udoc.uname}`);
}
userfileProgress.stop();
logger.success('Files copied successfully. You can now remove collection `file` `fs.files` `fs.chunks` in the database.'); logger.success('Files copied successfully. You can now remove collection `file` `fs.files` `fs.chunks` in the database.');
return true; return true;
}, },
@@ -176,7 +149,7 @@ const scripts: UpgradeScript[] = [
await storage.get(dst) await storage.get(dst)
.catch(() => { .catch(() => {
const cfg = yaml.dump(pdoc.config); const cfg = yaml.dump(pdoc.config);
return storage.put(dst, Buffer.from(cfg)); return problem.addTestdata(pdoc.domainId, pdoc.docId, 'config.yaml', Buffer.from(cfg));
}); });
}); });
return true; return true;
+12 -12
View File
@@ -82,14 +82,16 @@ class ImportQduojHandler extends Handler {
subtasks: [], subtasks: [],
}; };
for (const tc of pdoc.test_case_score) { for (const tc of pdoc.test_case_score) {
await storage.put( await Promise.all([
`problem/${domainId}/${pid}/testdata/${tc.input_name}`, problem.addTestdata(
domainId, pid, tc.input_name,
path.join(tmp, folder, 'testcase', tc.input_name), path.join(tmp, folder, 'testcase', tc.input_name),
); ),
await storage.put( problem.addTestdata(
`problem/${domainId}/${pid}/testdata/${tc.output_name}`, domainId, pid, tc.output_name,
path.join(tmp, folder, 'testcase', tc.output_name), path.join(tmp, folder, 'testcase', tc.output_name),
); ),
]);
config.subtasks.push({ config.subtasks.push({
score: tc.score, score: tc.score,
cases: [{ cases: [{
@@ -98,12 +100,10 @@ class ImportQduojHandler extends Handler {
}], }],
}); });
} }
await storage.put( await Promise.all([
`problem/${domainId}/${pid}/testdata/config.yaml`, problem.addTestdata(domainId, pid, 'config.yaml', Buffer.from(yaml.dump(config))),
Buffer.from(yaml.dump(config)), problem.edit(domainId, pid, { html: true }),
); ]);
const data = await storage.list(`problem/${domainId}/${pid}/testdata/`, true);
await problem.edit(domainId, pid, { html: true, data });
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);