确认删除「 <%= file.filename %> 」吗?
+diff --git a/models/problem.js b/models/problem.js index dbd74b2..5517fe9 100644 --- a/models/problem.js +++ b/models/problem.js @@ -349,12 +349,25 @@ class Problem extends Model { if (oldSize + size > syzoj.config.limit.testdata) throw new ErrorMessage('数据包太大。'); await fs.moveAsync(filepath, path.join(dir, filename), { overwrite: true }); + await fs.removeAsync(dir + '.zip'); + } + + async deleteTestdataSingleFile(filename) { + let dir = this.getTestdataPath(); + let fs = Promise.promisifyAll(require('fs-extra')), path = require('path'); + await fs.removeAsync(path.join(dir, filename)); + await fs.removeAsync(dir + '.zip'); + } + + async makeTestdataZip() { + let dir = this.getTestdataPath(); + if (!await syzoj.utils.isDir(dir)) throw new ErrorMessage('无测试数据。'); let AdmZip = require('adm-zip'); let zip = new AdmZip(); - list = await this.listTestdata(); - for (let file of list.files) zip.addLocalFile(path.join(dir, file.filename), '', file.filename); + let list = await this.listTestdata(); + for (let file of list.files) zip.addLocalFile(require('path').join(dir, file.filename), '', file.filename); zip.writeZip(dir + '.zip'); } @@ -380,7 +393,7 @@ class Problem extends Model { return { filename: x, size: stat.size - } + }; }); list = list.filter(x => x); @@ -395,13 +408,18 @@ class Problem extends Model { if (stat.isFile()) { res.zip = { size: stat.size - } + }; } - } catch (e) {} + } catch (e) { + if (list) { + res.zip = { + size: null + }; + } + } return res; } catch (e) { - console.log(e); return null; } } diff --git a/modules/problem.js b/modules/problem.js index 50bdf26..4e46988 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -676,6 +676,25 @@ app.post('/problem/:id/testdata/upload', app.multer.array('file'), async (req, r } }); +app.get('/problem/:id/testdata/delete/:filename', async (req, res) => { + try { + let id = parseInt(req.params.id); + let problem = await Problem.fromID(id); + + if (!problem) throw new ErrorMessage('无此题目。'); + if (!await problem.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); + + await problem.deleteTestdataSingleFile(req.params.filename); + + res.redirect(syzoj.utils.makeUrl(['problem', id, 'testdata'])); + } catch (e) { + syzoj.log(e); + res.render('error', { + err: e + }); + } +}); + app.get('/problem/:id/testdata/download/:filename?', async (req, res) => { try { let id = parseInt(req.params.id); @@ -684,6 +703,12 @@ app.get('/problem/:id/testdata/download/:filename?', async (req, res) => { if (!problem) throw new ErrorMessage('无此题目。'); if (!await problem.isAllowedUseBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); + if (!req.params.filename) { + if (!await syzoj.utils.isFile(problem.getTestdataPath() + '.zip')) { + await problem.makeTestdataZip(); + } + } + let path = require('path'); let filename = req.params.filename ? path.join(problem.getTestdataPath(), req.params.filename) : (problem.getTestdataPath() + '.zip'); if (!await syzoj.utils.isFile(filename)) throw new ErrorMessage('文件不存在。'); diff --git a/views/problem_data.ejs b/views/problem_data.ejs index 37c9245..c5f4c98 100644 --- a/views/problem_data.ejs +++ b/views/problem_data.ejs @@ -35,6 +35,11 @@ function getIcon(filename) { } %> <% include header %> +