Search in sources :

Example 1 with ContestPrint

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();
        }
    }
}
Also used : ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) FileWriter(cn.hutool.core.io.file.FileWriter) FileReader(cn.hutool.core.io.file.FileReader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) GetMapping(org.springframework.web.bind.annotation.GetMapping) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Example 2 with ContestPrint

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);
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Session(org.apache.shiro.session.Session)

Example 3 with ContestPrint

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();
        }
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) FileWriter(cn.hutool.core.io.file.FileWriter) ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) FileReader(cn.hutool.core.io.file.FileReader) Contest(top.hcode.hoj.pojo.entity.contest.Contest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Session(org.apache.shiro.session.Session)

Example 4 with ContestPrint

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, "查询成功");
}
Also used : ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage) Contest(top.hcode.hoj.pojo.entity.contest.Contest) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 5 with ContestPrint

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("确定失败!");
    }
}
Also used : ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Contest(top.hcode.hoj.pojo.entity.contest.Contest) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Aggregations

ContestPrint (top.hcode.hoj.pojo.entity.contest.ContestPrint)6 Contest (top.hcode.hoj.pojo.entity.contest.Contest)5 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)5 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)3 Session (org.apache.shiro.session.Session)3 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)3 FileReader (cn.hutool.core.io.file.FileReader)2 FileWriter (cn.hutool.core.io.file.FileWriter)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 IPage (com.baomidou.mybatisplus.core.metadata.IPage)2 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 HttpSession (javax.servlet.http.HttpSession)2 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)1