|
|
|
@ -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
|
|
|
|
@ -125,7 +121,6 @@ public class AdminComponentCodeController {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("updateAdminData")
|
|
|
|
|
@ApiOperation("编辑")
|
|
|
|
|
@AnonymousAccess
|
|
|
|
@ -156,8 +151,6 @@ public class AdminComponentCodeController {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("getAdminData")
|
|
|
|
|
@ApiOperation("展示")
|
|
|
|
|
@AnonymousAccess
|
|
|
|
@ -244,39 +237,46 @@ 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<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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 文件不存在
|
|
|
|
|
System.out.println("未找到文件: " + name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//没有就创建
|
|
|
|
|
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不存在!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -336,15 +336,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 +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, "已经存在,不用重复创建");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|