use of top.hcode.hoj.pojo.entity.contest.ContestPrint in project HOJ by HimitZH.
the class ContestFileController method downloadContestPrintText.
@GetMapping("/download-contest-print-text")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
public void downloadContestPrintText(@RequestParam("id") Long id, HttpServletResponse response) {
ContestPrint contestPrint = contestPrintService.getById(id);
String filename = contestPrint.getUsername() + "_Contest_Print.txt";
String filePath = Constants.File.CONTEST_TEXT_PRINT_FOLDER.getPath() + File.separator + id + File.separator + filename;
if (!FileUtil.exist(filePath)) {
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write(contestPrint.getContent());
}
FileReader zipFileReader = new FileReader(filePath);
// 放到缓冲流里面
BufferedInputStream bins = new BufferedInputStream(zipFileReader.getInputStream());
// 获取文件输出IO流
OutputStream outs = null;
BufferedOutputStream bouts = null;
try {
outs = response.getOutputStream();
bouts = new BufferedOutputStream(outs);
response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
int bytesRead = 0;
byte[] buffer = new byte[1024 * 10];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 1024 * 10)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
// 刷新缓存
bouts.flush();
} catch (IOException e) {
log.error("下载比赛打印文本文件异常------------>", e);
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, Object> map = new HashMap<>();
map.put("status", CommonResult.STATUS_ERROR);
map.put("msg", "下载文件失败,请重新尝试!");
map.put("data", null);
try {
response.getWriter().println(JSONUtil.toJsonStr(map));
} catch (IOException ioException) {
ioException.printStackTrace();
}
} finally {
try {
bins.close();
if (outs != null) {
outs.close();
}
if (bouts != null) {
bouts.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of top.hcode.hoj.pojo.entity.contest.ContestPrint in project HOJ by HimitZH.
the class ContestAdminManager method getContestPrint.
public IPage<ContestPrint> getContestPrint(Long cid, Integer currentPage, Integer limit) throws StatusForbiddenException {
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 获取本场比赛的状态
Contest contest = contestEntityService.getById(cid);
// 超级管理员或者该比赛的创建者,则为比赛管理者
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
if (!isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), contest.getGid()))) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
if (currentPage == null || currentPage < 1)
currentPage = 1;
if (limit == null || limit < 1)
limit = 30;
// 获取当前比赛的,未被确定的排在签名
IPage<ContestPrint> contestPrintIPage = new Page<>(currentPage, limit);
QueryWrapper<ContestPrint> contestPrintQueryWrapper = new QueryWrapper<>();
contestPrintQueryWrapper.select("id", "cid", "username", "realname", "status", "gmt_create").eq("cid", cid).orderByAsc("status").orderByDesc("gmt_create");
return contestPrintEntityService.page(contestPrintIPage, contestPrintQueryWrapper);
}
use of top.hcode.hoj.pojo.entity.contest.ContestPrint in project HOJ by HimitZH.
the class ContestFileManager method downloadContestPrintText.
public void downloadContestPrintText(Long id, HttpServletResponse response) throws StatusForbiddenException {
ContestPrint contestPrint = contestPrintEntityService.getById(id);
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
Long cid = contestPrint.getCid();
Contest contest = contestEntityService.getById(cid);
Long gid = contest.getGid();
if (!isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), gid))) {
throw new StatusForbiddenException("错误:您并非该比赛的管理员,无权下载打印代码!");
}
String filename = contestPrint.getUsername() + "_Contest_Print.txt";
String filePath = Constants.File.CONTEST_TEXT_PRINT_FOLDER.getPath() + File.separator + id + File.separator + filename;
if (!FileUtil.exist(filePath)) {
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write(contestPrint.getContent());
}
FileReader zipFileReader = new FileReader(filePath);
// 放到缓冲流里面
BufferedInputStream bins = new BufferedInputStream(zipFileReader.getInputStream());
// 获取文件输出IO流
OutputStream outs = null;
BufferedOutputStream bouts = null;
try {
outs = response.getOutputStream();
bouts = new BufferedOutputStream(outs);
response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
int bytesRead = 0;
byte[] buffer = new byte[1024 * 10];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 1024 * 10)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
// 刷新缓存
bouts.flush();
} catch (IOException e) {
log.error("下载比赛打印文本文件异常------------>", e);
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, Object> map = new HashMap<>();
map.put("status", ResultStatus.SYSTEM_ERROR);
map.put("msg", "下载文件失败,请重新尝试!");
map.put("data", null);
try {
response.getWriter().println(JSONUtil.toJsonStr(map));
} catch (IOException ioException) {
ioException.printStackTrace();
}
} finally {
try {
bins.close();
if (outs != null) {
outs.close();
}
if (bouts != null) {
bouts.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of top.hcode.hoj.pojo.entity.contest.ContestPrint in project HOJ by HimitZH.
the class ContestAdminController method getContestPrint.
@GetMapping("/get-contest-print")
@RequiresAuthentication
public CommonResult getContestPrint(@RequestParam("cid") Long cid, @RequestParam(value = "currentPage", required = false) Integer currentPage, @RequestParam(value = "limit", required = false) Integer limit, HttpServletRequest request) {
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 获取本场比赛的状态
Contest contest = contestService.getById(cid);
// 超级管理员或者该比赛的创建者,则为比赛管理者
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
if (!isRoot && !contest.getUid().equals(userRolesVo.getUid())) {
return CommonResult.errorResponse("对不起,你无权查看!", CommonResult.STATUS_FORBIDDEN);
}
if (currentPage == null || currentPage < 1)
currentPage = 1;
if (limit == null || limit < 1)
limit = 30;
// 获取当前比赛的,未被确定的排在签名
IPage<ContestPrint> contestPrintIPage = new Page<>(currentPage, limit);
QueryWrapper<ContestPrint> contestPrintQueryWrapper = new QueryWrapper<>();
contestPrintQueryWrapper.select("id", "cid", "username", "realname", "status", "gmt_create").eq("cid", cid).orderByAsc("status").orderByDesc("gmt_create");
IPage<ContestPrint> contestPrintList = contestPrintService.page(contestPrintIPage, contestPrintQueryWrapper);
return CommonResult.successResponse(contestPrintList, "查询成功");
}
use of top.hcode.hoj.pojo.entity.contest.ContestPrint in project HOJ by HimitZH.
the class ContestAdminController method checkContestStatus.
/**
* @param id
* @param cid
* @param request
* @MethodName checkContestStatus
* @Description 更新该打印为确定状态
* @Return
* @Since 2021/9/20
*/
@PutMapping("/check-contest-print-status")
@RequiresAuthentication
public CommonResult checkContestStatus(@RequestParam("id") Long id, @RequestParam("cid") Long cid, HttpServletRequest request) {
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 获取本场比赛的状态
Contest contest = contestService.getById(cid);
// 超级管理员或者该比赛的创建者,则为比赛管理者
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
if (!isRoot && !contest.getUid().equals(userRolesVo.getUid())) {
return CommonResult.errorResponse("对不起,你无权操作!", CommonResult.STATUS_FORBIDDEN);
}
boolean result = contestPrintService.updateById(new ContestPrint().setId(id).setStatus(1));
if (result) {
return CommonResult.successResponse(null, "确定成功!");
} else {
return CommonResult.errorResponse("确定失败!");
}
}
Aggregations