use of top.hcode.hoj.pojo.dto.ProblemDto in project HOJ by HimitZH.
the class ImportFpsController method parseFps.
private List<ProblemDto> parseFps(InputStream inputStream, String username) throws IOException {
Document document = XmlUtil.readXML(inputStream);
Element rootElement = XmlUtil.getRootElement(document);
String version = rootElement.getAttribute("version");
List<ProblemDto> problemDtoList = new ArrayList<>();
String fileDirId = IdUtil.simpleUUID();
String fileDir = Constants.File.TESTCASE_TMP_FOLDER.getPath() + File.separator + fileDirId;
int index = 1;
for (Element item : XmlUtil.getElements(rootElement, "item")) {
Problem problem = new Problem();
problem.setAuthor(username).setType(0).setIsUploadCase(true).setDifficulty(1).setIsRemoveEndBlank(true).setOpenCaseResult(true).setCodeShare(false).setIsRemote(false).setAuth(1).setProblemId(String.valueOf(System.currentTimeMillis()));
Element title = XmlUtil.getElement(item, "title");
// 标题
problem.setTitle(title.getTextContent());
HashMap<String, String> srcMapUrl = new HashMap<>();
List<Element> images = XmlUtil.getElements(item, "img");
for (Element img : images) {
Element srcElement = XmlUtil.getElement(img, "src");
if (srcElement == null) {
continue;
}
String src = srcElement.getTextContent();
String base64 = XmlUtil.getElement(img, "base64").getTextContent();
String[] split = src.split("\\.");
byte[] decode = Base64.getDecoder().decode(base64);
String fileName = IdUtil.fastSimpleUUID() + "." + split[split.length - 1];
FileUtil.writeBytes(decode, Constants.File.MARKDOWN_FILE_FOLDER.getPath() + File.separator + fileName);
srcMapUrl.put(src, Constants.File.IMG_API.getPath() + fileName);
}
Element descriptionElement = XmlUtil.getElement(item, "description");
String description = descriptionElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
description = description.replaceAll(entry.getKey(), entry.getValue());
}
// 题目描述
problem.setDescription(description);
Element inputElement = XmlUtil.getElement(item, "input");
String input = inputElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
input = input.replaceAll(entry.getKey(), entry.getValue());
}
// 输入描述
problem.setInput(input);
Element outputElement = XmlUtil.getElement(item, "output");
String output = outputElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
output = output.replaceAll(entry.getKey(), entry.getValue());
}
// 输出描述
problem.setOutput(output);
// 提示
Element hintElement = XmlUtil.getElement(item, "hint");
String hint = hintElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
hint = hint.replaceAll(entry.getKey(), entry.getValue());
}
problem.setHint(hint);
// 来源
Element sourceElement = XmlUtil.getElement(item, "source");
String source = sourceElement.getTextContent();
problem.setSource(source);
// ms
Integer timeLimit = getTimeLimit(version, item);
problem.setTimeLimit(timeLimit);
// mb
Integer memoryLimit = getMemoryLimit(item);
problem.setMemoryLimit(memoryLimit);
// 题面用例
List<Element> sampleInputs = XmlUtil.getElements(item, "sample_input");
List<Element> sampleOutputs = XmlUtil.getElements(item, "sample_output");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sampleInputs.size(); i++) {
sb.append("<input>").append(sampleInputs.get(i).getTextContent()).append("</input>");
sb.append("<output>").append(sampleOutputs.get(i).getTextContent()).append("</output>");
}
problem.setExamples(sb.toString());
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", "ME");
List<Language> languageList = languageService.list(languageQueryWrapper);
HashMap<String, Long> languageMap = new HashMap<>();
for (Language language : languageList) {
languageMap.put(language.getName(), language.getId());
}
// 题目模板
List<Element> templateNodes = XmlUtil.getElements(item, "template");
List<CodeTemplate> codeTemplates = new ArrayList<>();
for (Element templateNode : templateNodes) {
String templateLanguage = templateNode.getAttribute("language");
String templateCode = templateNode.getTextContent();
if (templateLanguage == null || templateCode == null) {
continue;
}
String lang = fpsMapHOJ.get(templateLanguage);
if (lang != null) {
codeTemplates.add(new CodeTemplate().setCode(templateCode).setLid(languageMap.get(lang)));
}
}
// spj
Element spjNode = XmlUtil.getElement(item, "spj");
if (spjNode != null) {
String spjLanguage = spjNode.getAttribute("language");
String spjCode = spjNode.getTextContent();
if (("C".equals(spjLanguage) || "C++".equals(spjLanguage)) && !StringUtils.isEmpty(spjCode)) {
problem.setSpjLanguage(spjLanguage).setSpjCode(spjCode);
}
}
// 题目评测数据
List<Element> testInputs = XmlUtil.getElements(item, "test_input");
List<Element> testOutputs = XmlUtil.getElements(item, "test_output");
List<ProblemCase> problemSamples = new LinkedList<>();
String problemTestCaseDir = fileDir + File.separator + index;
for (int i = 0; i < testInputs.size(); i++) {
String infileName = (i + 1) + ".in";
String outfileName = (i + 1) + ".out";
FileWriter infileWriter = new FileWriter(problemTestCaseDir + File.separator + infileName);
FileWriter outfileWriter = new FileWriter(problemTestCaseDir + File.separator + outfileName);
infileWriter.write(testInputs.get(i).getTextContent());
outfileWriter.write(testOutputs.get(i).getTextContent());
problemSamples.add(new ProblemCase().setInput(infileName).setOutput(outfileName));
}
String mode = Constants.JudgeMode.DEFAULT.getMode();
if (problem.getSpjLanguage() != null) {
mode = Constants.JudgeMode.SPJ.getMode();
}
ProblemDto problemDto = new ProblemDto();
problemDto.setSamples(problemSamples).setIsUploadTestCase(true).setUploadTestcaseDir(problemTestCaseDir).setLanguages(languageList).setTags(null).setJudgeMode(mode).setProblem(problem).setCodeTemplates(codeTemplates);
problemDtoList.add(problemDto);
index++;
}
return problemDtoList;
}
use of top.hcode.hoj.pojo.dto.ProblemDto in project HOJ by HimitZH.
the class ImportQDUOJController method importQDOJProblem.
/**
* @param file
* @MethodName importQDOJProblem
* @Description zip文件导入题目 仅超级管理员可操作
* @Return
* @Since 2021/5/27
*/
@RequiresRoles("root")
@RequiresAuthentication
@ResponseBody
@Transactional(rollbackFor = Exception.class)
@PostMapping("/import-qdoj-problem")
public CommonResult importQDOJProblem(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
if (!"zip".toUpperCase().contains(suffix.toUpperCase())) {
return CommonResult.errorResponse("请上传zip格式的题目文件压缩包!");
}
String fileDirId = IdUtil.simpleUUID();
String fileDir = Constants.File.TESTCASE_TMP_FOLDER.getPath() + File.separator + fileDirId;
String filePath = fileDir + File.separator + file.getOriginalFilename();
// 文件夹不存在就新建
FileUtil.mkdir(fileDir);
try {
file.transferTo(new File(filePath));
} catch (IOException e) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("服务器异常:qduoj题目上传失败!");
}
// 将压缩包压缩到指定文件夹
ZipUtil.unzip(filePath, fileDir);
// 删除zip文件
FileUtil.del(filePath);
// 检查文件是否存在
File testCaseFileList = new File(fileDir);
File[] files = testCaseFileList.listFiles();
if (files == null || files.length == 0) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("评测数据压缩包里文件不能为空!");
}
HashMap<String, File> problemInfo = new HashMap<>();
for (File tmp : files) {
if (tmp.isDirectory()) {
File[] problemAndTestcase = tmp.listFiles();
if (problemAndTestcase == null || problemAndTestcase.length == 0) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("编号为:" + tmp.getName() + "的文件夹为空!");
}
for (File problemFile : problemAndTestcase) {
if (problemFile.isFile()) {
// 检查文件是否时json文件
if (!problemFile.getName().endsWith("json")) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("编号为:" + tmp.getName() + "的文件夹里面的题目数据格式错误,请使用json文件!");
}
problemInfo.put(tmp.getName(), problemFile);
}
}
}
}
// 读取json文件生成对象
HashMap<String, QDOJProblemDto> problemVoMap = new HashMap<>();
for (String key : problemInfo.keySet()) {
try {
FileReader fileReader = new FileReader(problemInfo.get(key));
JSONObject problemJson = JSONUtil.parseObj(fileReader.readString());
QDOJProblemDto qdojProblemDto = QDOJProblemToProblemVo(problemJson);
problemVoMap.put(key, qdojProblemDto);
} catch (Exception e) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("请检查编号为:" + key + "的题目json文件的格式:" + e.getLocalizedMessage());
}
}
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", "ME");
List<Language> languageList = languageService.list(languageQueryWrapper);
HashMap<String, Long> languageMap = new HashMap<>();
for (Language language : languageList) {
languageMap.put(language.getName(), language.getId());
}
// 获取当前登录的用户
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
List<Tag> tagList = tagService.list(new QueryWrapper<Tag>().eq("oj", "ME"));
HashMap<String, Tag> tagMap = new HashMap<>();
for (Tag tag : tagList) {
tagMap.put(tag.getName().toUpperCase(), tag);
}
List<ProblemDto> problemDtos = new LinkedList<>();
for (String key : problemInfo.keySet()) {
QDOJProblemDto qdojProblemDto = problemVoMap.get(key);
// 格式化题目语言
List<Language> languages = new LinkedList<>();
for (String lang : qdojProblemDto.getLanguages()) {
Long lid = languageMap.getOrDefault(lang, null);
languages.add(new Language().setId(lid).setName(lang));
}
// 格式化标签
List<Tag> tags = new LinkedList<>();
for (String tagStr : qdojProblemDto.getTags()) {
Tag tag = tagMap.getOrDefault(tagStr.toUpperCase(), null);
if (tag == null) {
tags.add(new Tag().setName(tagStr).setOj("ME"));
} else {
tags.add(tag);
}
}
Problem problem = qdojProblemDto.getProblem();
if (problem.getAuthor() == null) {
problem.setAuthor(userRolesVo.getUsername());
}
ProblemDto problemDto = new ProblemDto();
String mode = Constants.JudgeMode.DEFAULT.getMode();
if (qdojProblemDto.getIsSpj()) {
mode = Constants.JudgeMode.SPJ.getMode();
}
problemDto.setJudgeMode(mode).setProblem(problem).setCodeTemplates(qdojProblemDto.getCodeTemplates()).setTags(tags).setLanguages(languages).setUploadTestcaseDir(fileDir + File.separator + key + File.separator + "testcase").setIsUploadTestCase(true).setSamples(qdojProblemDto.getSamples());
problemDtos.add(problemDto);
}
for (ProblemDto problemDto : problemDtos) {
problemService.adminAddProblem(problemDto);
}
return CommonResult.successResponse(null, "导入题目成功");
}
use of top.hcode.hoj.pojo.dto.ProblemDto in project HOJ by HimitZH.
the class ImportFpsProblemManager method importFPSProblem.
/**
* @param file
* @MethodName importFpsProblem
* @Description zip文件导入题目 仅超级管理员可操作
* @Return
* @Since 2021/10/06
*/
@Transactional(rollbackFor = Exception.class)
public void importFPSProblem(MultipartFile file) throws IOException, StatusFailException {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
if (!"xml".toUpperCase().contains(suffix.toUpperCase())) {
throw new StatusFailException("请上传xml后缀格式的fps题目文件!");
}
// 获取当前登录的用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
List<ProblemDto> problemDtoList = parseFps(file.getInputStream(), userRolesVo.getUsername());
if (problemDtoList.size() == 0) {
throw new StatusFailException("警告:未成功导入一道以上的题目,请检查文件格式是否正确!");
} else {
for (ProblemDto problemDto : problemDtoList) {
problemEntityService.adminAddProblem(problemDto);
}
}
}
use of top.hcode.hoj.pojo.dto.ProblemDto in project HOJ by HimitZH.
the class ImportAndExportProblemController method importProblem.
/**
* @param file
* @MethodName importProblem
* @Description zip文件导入题目 仅超级管理员可操作
* @Return
* @Since 2021/5/27
*/
@RequiresRoles("root")
@RequiresAuthentication
@ResponseBody
@Transactional(rollbackFor = Exception.class)
@PostMapping("/import-problem")
public CommonResult importProblem(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
if (!"zip".toUpperCase().contains(suffix.toUpperCase())) {
return CommonResult.errorResponse("请上传zip格式的题目文件压缩包!");
}
String fileDirId = IdUtil.simpleUUID();
String fileDir = Constants.File.TESTCASE_TMP_FOLDER.getPath() + File.separator + fileDirId;
String filePath = fileDir + File.separator + file.getOriginalFilename();
// 文件夹不存在就新建
FileUtil.mkdir(fileDir);
try {
file.transferTo(new File(filePath));
} catch (IOException e) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("服务器异常:评测数据上传失败!");
}
// 将压缩包压缩到指定文件夹
ZipUtil.unzip(filePath, fileDir);
// 删除zip文件
FileUtil.del(filePath);
// 检查文件是否存在
File testCaseFileList = new File(fileDir);
File[] files = testCaseFileList.listFiles();
if (files == null || files.length == 0) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("评测数据压缩包里文件不能为空!");
}
HashMap<String, File> problemInfo = new HashMap<>();
HashMap<String, File> testcaseInfo = new HashMap<>();
for (File tmp : files) {
if (tmp.isFile()) {
// 检查文件是否时json文件
if (!tmp.getName().endsWith("json")) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("编号为:" + tmp.getName() + "的文件格式错误,请使用json文件!");
}
String tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf("."));
problemInfo.put(tmpPreName, tmp);
}
if (tmp.isDirectory()) {
testcaseInfo.put(tmp.getName(), tmp);
}
}
// 读取json文件生成对象
HashMap<String, ImportProblemVo> problemVoMap = new HashMap<>();
for (String key : problemInfo.keySet()) {
// 若有名字不对应,直接返回失败
if (testcaseInfo.getOrDefault(key, null) == null) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("请检查编号为:" + key + "的题目数据文件与测试数据文件夹是否一一对应!");
}
try {
FileReader fileReader = new FileReader(problemInfo.get(key));
ImportProblemVo importProblemVo = JSONUtil.toBean(fileReader.readString(), ImportProblemVo.class);
problemVoMap.put(key, importProblemVo);
} catch (Exception e) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("请检查编号为:" + key + "的题目json文件的格式:" + e.getLocalizedMessage());
}
}
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", "ME");
List<Language> languageList = languageService.list(languageQueryWrapper);
HashMap<String, Long> languageMap = new HashMap<>();
for (Language language : languageList) {
languageMap.put(language.getName(), language.getId());
}
// 获取当前登录的用户
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
List<ProblemDto> problemDtos = new LinkedList<>();
List<Tag> tagList = tagService.list(new QueryWrapper<Tag>().eq("oj", "ME"));
HashMap<String, Tag> tagMap = new HashMap<>();
for (Tag tag : tagList) {
tagMap.put(tag.getName().toUpperCase(), tag);
}
for (String key : problemInfo.keySet()) {
ImportProblemVo importProblemVo = problemVoMap.get(key);
// 格式化题目语言
List<Language> languages = new LinkedList<>();
for (String lang : importProblemVo.getLanguages()) {
Long lid = languageMap.getOrDefault(lang, null);
if (lid == null) {
return CommonResult.errorResponse("请检查编号为:" + key + "的题目的代码语言是否有错,不要添加不支持的语言!");
}
languages.add(new Language().setId(lid).setName(lang));
}
// 格式化题目代码模板
List<CodeTemplate> codeTemplates = new LinkedList<>();
for (Map<String, String> tmp : importProblemVo.getCodeTemplates()) {
String language = tmp.getOrDefault("language", null);
String code = tmp.getOrDefault("code", null);
Long lid = languageMap.getOrDefault(language, null);
if (language == null || code == null || lid == null) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("请检查编号为:" + key + "的题目的代码模板列表是否有错,不要添加不支持的语言!");
}
codeTemplates.add(new CodeTemplate().setCode(code).setStatus(true).setLid(lid));
}
// 格式化标签
List<Tag> tags = new LinkedList<>();
for (String tagStr : importProblemVo.getTags()) {
Tag tag = tagMap.getOrDefault(tagStr.toUpperCase(), null);
if (tag == null) {
tags.add(new Tag().setName(tagStr).setOj("ME"));
} else {
tags.add(tag);
}
}
Problem problem = BeanUtil.mapToBean(importProblemVo.getProblem(), Problem.class, true);
if (problem.getAuthor() == null) {
problem.setAuthor(userRolesVo.getUsername());
}
List<ProblemCase> problemCaseList = new LinkedList<>();
for (Map<String, Object> tmp : importProblemVo.getSamples()) {
problemCaseList.add(BeanUtil.mapToBean(tmp, ProblemCase.class, true));
}
// 格式化用户额外文件和判题额外文件
if (importProblemVo.getUserExtraFile() != null) {
JSONObject userExtraFileJson = JSONUtil.parseObj(importProblemVo.getUserExtraFile());
problem.setUserExtraFile(userExtraFileJson.toString());
}
if (importProblemVo.getJudgeExtraFile() != null) {
JSONObject judgeExtraFileJson = JSONUtil.parseObj(importProblemVo.getJudgeExtraFile());
problem.setJudgeExtraFile(judgeExtraFileJson.toString());
}
ProblemDto problemDto = new ProblemDto();
problemDto.setJudgeMode(importProblemVo.getJudgeMode()).setProblem(problem).setCodeTemplates(codeTemplates).setTags(tags).setLanguages(languages).setUploadTestcaseDir(fileDir + File.separator + key).setIsUploadTestCase(true).setSamples(problemCaseList);
problemDtos.add(problemDto);
}
for (ProblemDto problemDto : problemDtos) {
problemService.adminAddProblem(problemDto);
}
return CommonResult.successResponse(null, "导入题目成功");
}
use of top.hcode.hoj.pojo.dto.ProblemDto in project HOJ by HimitZH.
the class ImportFpsController method importFPSProblem.
/**
* @param file
* @MethodName importFpsProblem
* @Description zip文件导入题目 仅超级管理员可操作
* @Return
* @Since 2021/10/06
*/
@RequiresRoles("root")
@RequiresAuthentication
@ResponseBody
@Transactional(rollbackFor = Exception.class)
@PostMapping("/import-fps-problem")
public CommonResult importFPSProblem(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
if (!"xml".toUpperCase().contains(suffix.toUpperCase())) {
return CommonResult.errorResponse("请上传xml后缀格式的fps题目文件!");
}
// 获取当前登录的用户
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
List<ProblemDto> problemDtoList = parseFps(file.getInputStream(), userRolesVo.getUsername());
for (ProblemDto problemDto : problemDtoList) {
problemService.adminAddProblem(problemDto);
}
return CommonResult.successResponse(null, "导入题目成功");
}
Aggregations