From 3324068400a6fe6226b8b5470989fb450546e176 Mon Sep 17 00:00:00 2001 From: Menci Date: Fri, 7 Jul 2017 08:26:56 +0800 Subject: [PATCH] Fix can't download additional file in contest --- modules/contest.js | 40 ++++++++++++++++++++++++++++++++++++++++ views/problem.ejs | 7 ++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/modules/contest.js b/modules/contest.js index 9c7b042..18e5659 100644 --- a/modules/contest.js +++ b/modules/contest.js @@ -329,6 +329,7 @@ app.get('/contest/:id/:pid', async (req, res) => { try { let contest_id = parseInt(req.params.id); let contest = await Contest.fromID(contest_id); + if (!contest) throw new ErrorMessage('无此比赛。'); let problems_id = await contest.getProblems(); @@ -353,6 +354,8 @@ app.get('/contest/:id/:pid', async (req, res) => { let state = await problem.getJudgeState(res.locals.user, false); let testcases = await syzoj.utils.parseTestdata(problem.getTestdataPath(), problem.type === 'submit-answer'); + await problem.loadRelationships(); + res.render('problem', { pid: pid, contest: contest, @@ -368,3 +371,40 @@ app.get('/contest/:id/:pid', async (req, res) => { }); } }); + +app.get('/contest/:id/:pid/download/additional_file', async (req, res) => { + try { + let id = parseInt(req.params.id); + let contest = await Contest.fromID(id); + if (!contest) throw new ErrorMessage('无此比赛。'); + + let problems_id = await contest.getProblems(); + + let pid = parseInt(req.params.pid); + if (!pid || pid < 1 || pid > problems_id.length) throw new ErrorMessage('无此题目。'); + + let problem_id = problems_id[pid - 1]; + let problem = await Problem.fromID(problem_id); + + contest.ended = await contest.isEnded(); + if (!(await contest.isRunning() || contest.ended)) { + if (await problem.isAllowedUseBy(res.locals.user)) { + return res.redirect(syzoj.utils.makeUrl(['problem', problem_id, 'download', 'additional_file'])); + } + throw new ErrorMessage('比赛尚未开始。'); + } + + await problem.loadRelationships(); + + if (!problem.additional_file) throw new ErrorMessage('无附加文件。'); + + console.log(`additional_file_${id}_${pid}.zip`); + res.download(problem.additional_file.getPath(), `additional_file_${id}_${pid}.zip`); + } catch (e) { + syzoj.log(e); + res.status(404); + res.render('error', { + err: e + }); + } +}); diff --git a/views/problem.ejs b/views/problem.ejs index c85bd62..c93cca1 100644 --- a/views/problem.ejs +++ b/views/problem.ejs @@ -82,6 +82,9 @@ div[class*=ace_br] { 转到题库 <% } %> 提交记录 + <% if (problem.additional_file) { %> + 附加文件 + <% } %> 返回比赛 <% } else { %> <% if (testcases && !testcases.error) { %> @@ -90,8 +93,10 @@ div[class*=ace_br] { 提交记录 统计 测试数据 + <% if (problem.additional_file) { %> + 附加文件 + <% } %> <% } %> - <% if (problem.additional_file) { %>附加文件<% } %> <% if (!contest) { %>