组件代码学生端动态展示、超管端支持更新修改(jupyter)

master
@t2652009480 8 months ago
parent 99aca08074
commit fbec9a24af

@ -33,13 +33,11 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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
@ -130,19 +128,15 @@ public class AdminComponentCodeController {
@ApiOperation("编辑")
@AnonymousAccess
@Transactional
public ResultEntity<String> updateAdminData(@ApiParam("案例ID") @RequestParam Integer id,
@ApiParam("代码内容")@RequestParam(required = false) String codeContent,
@ApiParam("参数释义")@RequestParam(required = false) String parameterContent,
@ApiParam("步骤名称(一级标签)")@RequestParam(required = false) String stepName,
@ApiParam("案例名称(二级标签)")@RequestParam(required = false) String caseName) {
public ResultEntity<String> updateAdminData(@RequestBody UpdateAdminDataDTO dto) {
//编辑
try {
AdminComponentCodeWithBLOBs adminComponentCode = new AdminComponentCodeWithBLOBs();
adminComponentCode.setId(id);
adminComponentCode.setCourseName(stepName);
adminComponentCode.setChapterName(caseName);
adminComponentCode.setItem(codeContent);
adminComponentCode.setDefinition(parameterContent);
adminComponentCode.setId(dto.getId());
adminComponentCode.setCourseName(dto.getStepName());
adminComponentCode.setChapterName(dto.getCaseName());
adminComponentCode.setItem(dto.getCodeContent());
adminComponentCode.setDefinition(dto.getParameterContent());
adminComponentCode.setStatus(0); //默认下架,发布后上架
adminComponentCode.setCreateTime(new Date()); //修改时间
@ -240,43 +234,48 @@ public class AdminComponentCodeController {
}
@ApiOperation("后端根据名字创建notebook")
@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<File> 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<File> 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());
}
}
}
//没有就创建
AdminJupyterTokenExample tokenExample = new AdminJupyterTokenExample();
tokenExample.createCriteria().andTypeEqualTo(1);
List<AdminJupyterToken> adminJupyterTokenList = adminJupyterTokenMapper.selectByExample(tokenExample);
if (adminJupyterTokenList.isEmpty())
{
if (adminJupyterTokenList.isEmpty()) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "Token不存在");
}
@ -302,6 +301,14 @@ public class AdminComponentCodeController {
// + "\"nbformat_minor\": 2"
// + "}";
if (codeContent.startsWith("```python")) {
codeContent = codeContent.replaceFirst("```python", "");
}
if (codeContent.endsWith("```")) {
codeContent = codeContent.substring(0, codeContent.length() - 3);
}
String notebookContent = "{"
+ "\"cells\": ["
+ "{"
@ -336,15 +343,8 @@ public class AdminComponentCodeController {
+ "}";
// 在根目录下创建新的笔记本
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 +354,36 @@ 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);
// 等待进程执行完成
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, "已经存在,不用重复创建");
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

Loading…
Cancel
Save