diff --git a/src/main/java/com/sztzjy/resource_center/controller/new_module/admin/AdminComponentCodeController.java b/src/main/java/com/sztzjy/resource_center/controller/new_module/admin/AdminComponentCodeController.java index e450083..3aff635 100644 --- a/src/main/java/com/sztzjy/resource_center/controller/new_module/admin/AdminComponentCodeController.java +++ b/src/main/java/com/sztzjy/resource_center/controller/new_module/admin/AdminComponentCodeController.java @@ -33,13 +33,9 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.io.*; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; +import java.util.*; /** * @author tz @@ -70,7 +66,7 @@ public class AdminComponentCodeController { @AnonymousAccess @Transactional public ResultEntity add(@ApiParam("代码内容") String codeContent, - @ApiParam("参数释义")@RequestParam(required = false) String parameterContent, + @ApiParam("参数释义") @RequestParam(required = false) String parameterContent, @ApiParam("步骤名称(一级标签)") String stepName, @ApiParam("案例名称(二级标签)") String caseName, @RequestParam(required = false) List dataFile) { @@ -125,16 +121,15 @@ public class AdminComponentCodeController { } - @PostMapping("updateAdminData") @ApiOperation("编辑") @AnonymousAccess @Transactional public ResultEntity updateAdminData(@ApiParam("案例ID") @RequestBody Integer id, - @ApiParam("代码内容")@RequestBody(required = false) String codeContent, - @ApiParam("参数释义")@RequestBody(required = false) String parameterContent, - @ApiParam("步骤名称(一级标签)")@RequestBody(required = false) String stepName, - @ApiParam("案例名称(二级标签)")@RequestBody(required = false) String caseName) { + @ApiParam("代码内容") @RequestBody(required = false) String codeContent, + @ApiParam("参数释义") @RequestBody(required = false) String parameterContent, + @ApiParam("步骤名称(一级标签)") @RequestBody(required = false) String stepName, + @ApiParam("案例名称(二级标签)") @RequestBody(required = false) String caseName) { //编辑 try { AdminComponentCodeWithBLOBs adminComponentCode = new AdminComponentCodeWithBLOBs(); @@ -156,19 +151,17 @@ public class AdminComponentCodeController { } - - @PostMapping("getAdminData") @ApiOperation("展示") @AnonymousAccess @Transactional public ResultEntity getAdminData(@RequestParam Integer index, @RequestParam Integer size, - @RequestParam(required = false) String caseName ) { + @RequestParam(required = false) String caseName) { - AdminComponentCodeExample codeExample=new AdminComponentCodeExample(); - if(!caseName.isEmpty()){ - codeExample.createCriteria().andChapterNameLike("%"+caseName+"%"); + AdminComponentCodeExample codeExample = new AdminComponentCodeExample(); + if (!caseName.isEmpty()) { + codeExample.createCriteria().andChapterNameLike("%" + caseName + "%"); } List adminComponentCodeWithBLOBs = adminComponentCodeMapper.selectByExampleWithBLOBs(codeExample); @@ -176,7 +169,7 @@ public class AdminComponentCodeController { PageInfo pageInfo = PageUtil.pageHelper(adminComponentCodeWithBLOBs, index, size); - return new ResultEntity<>(HttpStatus.OK, "成功!",pageInfo); + return new ResultEntity<>(HttpStatus.OK, "成功!", pageInfo); } @PostMapping("addFile") @@ -244,40 +237,47 @@ public class AdminComponentCodeController { @GetMapping("/createNoteBook") @AnonymousAccess public ResultEntity createNoteBook(@ApiParam("参数为:案例名") String name, - @ApiParam("代码内容") String codeContent){ + @ApiParam("代码内容") String codeContent) throws IOException { //判断有无这个案例 -// -// String path = "/usr/local/tianzeProject/jupyter/tch"; -// -// // 创建表示该目录的 File 对象 -// File directory = new File(path); -// -// // 获取目录中的所有文件和子目录 -// File[] filesList = directory.listFiles(); -// -// -// if (filesList != null) { -// // 使用流查找目标文件 -// Optional targetFile = Arrays.stream(filesList) -// .filter(file -> file.getName().equals(name)) -// .findFirst(); -// -// if (targetFile.isPresent()) { -// return new ResultEntity<>(HttpStatus.OK,"文件已存在"); -// } -// } + String path = "/usr/local/tianzeProject/jupyter/tch/"; - //没有就创建 + // 创建表示该目录的 File 对象 + File directory = new File(path); + + // 获取目录中的所有文件和子目录 + File[] filesList = directory.listFiles(); + + + if (filesList != null) { + // 使用流查找目标文件 + Optional targetFile = Arrays.stream(filesList) + .filter(file -> file.getName().equals(name)) + .findFirst(); + if (targetFile.isPresent()) { + // 找到目标文件 + File file = targetFile.get(); + // 删除文件 + boolean deleted = file.delete(); + if (deleted) { + System.out.println("文件已删除: " + file.getAbsolutePath()); + } else { + System.out.println("删除文件失败: " + file.getAbsolutePath()); + } + } + } else { + // 文件不存在 + System.out.println("未找到文件: " + name); + } + //没有就创建 AdminJupyterTokenExample tokenExample = new AdminJupyterTokenExample(); tokenExample.createCriteria().andTypeEqualTo(1); List adminJupyterTokenList = adminJupyterTokenMapper.selectByExample(tokenExample); - if (adminJupyterTokenList.isEmpty()) - { - return new ResultEntity<>(HttpStatus.BAD_REQUEST,"Token不存在!"); + if (adminJupyterTokenList.isEmpty()) { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "Token不存在!"); } try (CloseableHttpClient client = HttpClients.createDefault()) { @@ -336,15 +336,8 @@ public class AdminComponentCodeController { + "}"; - - - - // 在根目录下创建新的笔记本 - HttpPut createNotebookRequest = new HttpPut(JUPYTER_URL + "/api/contents/"+name+".ipynb"); - - - + HttpPut createNotebookRequest = new HttpPut(JUPYTER_URL + "/api/contents/" + name + ".ipynb"); createNotebookRequest.addHeader("Authorization", "token " + adminJupyterTokenList.get(0).getToken()); createNotebookRequest.addHeader("Content-Type", "application/json charset=UTF-8"); //createNotebookRequest.setEntity(new StringEntity("{\"type\": \"notebook\", \"content\": " + notebookContent + "}")); @@ -354,17 +347,40 @@ public class AdminComponentCodeController { int statusCode = createNotebookResponse.getStatusLine().getStatusCode(); if (statusCode == 201) { System.out.println("Notebook created successfully!"); - return new ResultEntity<>(HttpStatus.OK,"创建成功!"); + + + String toPath = "/usr/local/tianzeProject/jupyter/stu/" + name + ".ipynb"; + + String[] commandMove = {"cp", "-f", path + name + ".ipynb", toPath}; + + + Process process = Runtime.getRuntime().exec(commandMove); + // 获取进程的输入流 + BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream())); + + // 获取进程的输出流 + BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); + + // 等待进程执行完成 + int exitCodeInfo = process.waitFor(); + System.out.println(exitCodeInfo); + if (exitCodeInfo == 0) { + return new ResultEntity<>(HttpStatus.OK, "创建成功", true); + } else { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "创建失败!请联系管理员", false); + } } else { String responseBody = EntityUtils.toString(createNotebookResponse.getEntity()); System.out.println("Failed to create notebook: " + responseBody); - return new ResultEntity<>(HttpStatus.OK,"已经存在,不用重复创建"); + return new ResultEntity<>(HttpStatus.OK, "已经存在,不用重复创建"); } + } catch (IOException e) { + throw new RuntimeException(e); + } catch (InterruptedException e) { + throw new RuntimeException(e); } - } catch (IOException e) { - throw new RuntimeException(e); - } + } } }