core&ui: support file renaming (#688)

Co-authored-by: undefined <i@undefined.moe>
This commit is contained in:
Milmonandundefined authored and GitHub committed 2023-11-19 21:46:15 +08:00
1 parent 746efef9bf
commit 01cb6944b7
19 files changed
+496 -144

No files matched your search

+41
View File
@@ -282,6 +282,22 @@ export class ProblemModel {
await bus.emit('problem/addTestdata', domainId, pid, name, payload);
}
static async renameTestdata(domainId: string, pid: number, file: string, newName: string, operator = 1) {
if (file === newName) return;
const [, sdoc] = await document.getSub(domainId, document.TYPE_PROBLEM, pid, 'data', newName);
if (sdoc) await ProblemModel.delTestdata(domainId, pid, newName);
const payload = { _id: newName, name: newName, lastModified: new Date() };
await Promise.all([
storage.rename(
`problem/${domainId}/${pid}/testdata/${file}`,
`problem/${domainId}/${pid}/testdata/${newName}`,
operator,
),
document.setSub(domainId, document.TYPE_PROBLEM, pid, 'data', file, payload),
]);
await bus.emit('problem/renameTestdata', domainId, pid, file, newName);
}
static async delTestdata(domainId: string, pid: number, name: string | string[], operator = 1) {
const names = (name instanceof Array) ? name : [name];
await Promise.all([
@@ -307,6 +323,22 @@ export class ProblemModel {
await bus.emit('problem/addAdditionalFile', domainId, pid, name, payload);
}
static async renameAdditionalFile(domainId: string, pid: number, file: string, newName: string, operator = 1) {
if (file === newName) return;
const [, sdoc] = await document.getSub(domainId, document.TYPE_PROBLEM, pid, 'additional_file', newName);
if (sdoc) await ProblemModel.delAdditionalFile(domainId, pid, newName);
const payload = { _id: newName, name: newName, lastModified: new Date() };
await Promise.all([
storage.rename(
`problem/${domainId}/${pid}/additional_file/${file}`,
`problem/${domainId}/${pid}/additional_file/${newName}`,
operator,
),
document.setSub(domainId, document.TYPE_PROBLEM, pid, 'additional_file', file, payload),
]);
await bus.emit('problem/renameAdditionalFile', domainId, pid, file, newName);
}
static async delAdditionalFile(domainId: string, pid: number, name: MaybeArray<string>, operator = 1) {
const names = (name instanceof Array) ? name : [name];
await Promise.all([
@@ -558,6 +590,15 @@ bus.on('problem/delTestdata', async (domainId, docId, names) => {
if (!names.includes('config.yaml')) return;
await ProblemModel.edit(domainId, docId, { config: '' });
});
bus.on('problem/renameTestdata', async (domainId, docId, file, newName) => {
if (['config.yaml', 'config.yml', 'Config.yaml', 'Config.yml'].includes(file)) {
await ProblemModel.edit(domainId, docId, { config: '' });
}
if (['config.yaml', 'config.yml', 'Config.yaml', 'Config.yml'].includes(newName)) {
const buf = await storage.get(`problem/${domainId}/${docId}/testdata/${newName}`);
await ProblemModel.edit(domainId, docId, { config: (await streamToBuffer(buf)).toString() });
}
});
global.Hydro.model.problem = ProblemModel;
export default ProblemModel;