From c9c2d29f7c3ba0d6f15a1ccdfd5625770f35a2d3 Mon Sep 17 00:00:00 2001 From: Menci Date: Sun, 25 Jun 2017 09:29:21 +0800 Subject: [PATCH] Don't update zip after each upload of single file; Add delete file in testdata page --- models/problem.js | 30 ++++++++++--- modules/problem.js | 25 +++++++++++ views/problem_data.ejs | 89 ++++++++++++++++++++++++++++++++++--- views/problem_testcases.ejs | 4 +- 4 files changed, 134 insertions(+), 14 deletions(-) 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 %> +
@@ -49,22 +54,60 @@ function getIcon(filename) { 文件名 文件大小 - 操作 + 下载 + <% if (problem.allowedEdit) { %> + 删除 + <% } %> <% if (testdata.zip) { %> 完整数据包 - <%= syzoj.utils.formatSize(testdata.zip.size) %> + <%- !testdata.zip.size ? '' : syzoj.utils.formatSize(testdata.zip.size) %> + <% if (problem.allowedEdit) { %> + + <% } %> <% } %> + <% let i = 0; %> <% if (testdata.files) for (let file of testdata.files) { %> + <% i++; %> <%= file.filename %> <%= syzoj.utils.formatSize(file.size) %> - + + + + + + <% if (problem.allowedEdit) { %> + + + + + + + <% } %> <% } %> @@ -73,18 +116,52 @@ function getIcon(filename) {

无测试数据

<% } %> <% if (problem.allowedEdit) { %> -
+
- +
- +
提交
返回题目
+ + <% } %>
diff --git a/views/problem_testcases.ejs b/views/problem_testcases.ejs index f3b0b53..fddf6fd 100644 --- a/views/problem_testcases.ejs +++ b/views/problem_testcases.ejs @@ -30,8 +30,8 @@ let subtaskType = { <% } %> <% for (let testcase of subtask.cases) { %> - <%= testcase.input %> - <%= testcase.output %> + <%= testcase.input %> + <%= testcase.output %> <% } %> <% } %>