Browse Source

Fix can't download additional file in contest

master
Menci 8 years ago
parent
commit
3324068400
  1. 40
      modules/contest.js
  2. 7
      views/problem.ejs

40
modules/contest.js

@ -329,6 +329,7 @@ app.get('/contest/:id/:pid', async (req, res) => {
try { try {
let contest_id = parseInt(req.params.id); let contest_id = parseInt(req.params.id);
let contest = await Contest.fromID(contest_id); let contest = await Contest.fromID(contest_id);
if (!contest) throw new ErrorMessage('无此比赛。');
let problems_id = await contest.getProblems(); 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 state = await problem.getJudgeState(res.locals.user, false);
let testcases = await syzoj.utils.parseTestdata(problem.getTestdataPath(), problem.type === 'submit-answer'); let testcases = await syzoj.utils.parseTestdata(problem.getTestdataPath(), problem.type === 'submit-answer');
await problem.loadRelationships();
res.render('problem', { res.render('problem', {
pid: pid, pid: pid,
contest: contest, 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
});
}
});

7
views/problem.ejs

@ -82,6 +82,9 @@ div[class*=ace_br] {
<a class="small ui primary button" href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>">转到题库</a> <a class="small ui primary button" href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>">转到题库</a>
<% } %> <% } %>
<a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['contest', contest.id, 'submissions'], { problem_id: pid }) %>">提交记录</a> <a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['contest', contest.id, 'submissions'], { problem_id: pid }) %>">提交记录</a>
<% if (problem.additional_file) { %>
<a class="small ui teal button" href="<%= syzoj.utils.makeUrl(['contest', contest.id, pid, 'download', 'additional_file']) %>">附加文件</a>
<% } %>
<a href="<%= syzoj.utils.makeUrl(['contest', contest.id]) %>" class="ui orange button">返回比赛</a> <a href="<%= syzoj.utils.makeUrl(['contest', contest.id]) %>" class="ui orange button">返回比赛</a>
<% } else { %> <% } else { %>
<% if (testcases && !testcases.error) { %> <% if (testcases && !testcases.error) { %>
@ -90,8 +93,10 @@ div[class*=ace_br] {
<a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['submissions'], { problem_id: problem.id }) %>">提交记录</a> <a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['submissions'], { problem_id: problem.id }) %>">提交记录</a>
<a class="small ui orange button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'statistics', problem.type === 'submit-answer' ? 'min' : 'fastest']) %>">统计</a> <a class="small ui orange button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'statistics', problem.type === 'submit-answer' ? 'min' : 'fastest']) %>">统计</a>
<a class="small ui yellow button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'testdata']) %>">测试数据</a> <a class="small ui yellow button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'testdata']) %>">测试数据</a>
<% if (problem.additional_file) { %>
<a class="small ui teal button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'download', 'additional_file']) %>">附加文件</a>
<% } %>
<% } %> <% } %>
<% if (problem.additional_file) { %><a class="small ui teal button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'download', 'additional_file']) %>">附加文件</a><% } %>
</div> </div>
<% if (!contest) { %> <% if (!contest) { %>
<div class="ui buttons right floated"> <div class="ui buttons right floated">

Loading…
Cancel
Save