use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.
the class BaseAgentController method getProjectInfoModel.
/**
* 获取拦截器中缓存的项目信息
*
* @return NodeProjectInfoModel
*/
protected NodeProjectInfoModel getProjectInfoModel() {
NodeProjectInfoModel nodeProjectInfoModel = tryGetProjectInfoModel();
Objects.requireNonNull(nodeProjectInfoModel, "获取项目信息失败");
return nodeProjectInfoModel;
}
use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.
the class BaseAgentController method getProjectInfoModel.
/**
* 根据 项目ID 获取项目信息
*
* @return NodeProjectInfoModel
*/
protected NodeProjectInfoModel getProjectInfoModel(String id) {
NodeProjectInfoModel nodeProjectInfoModel = tryGetProjectInfoModel(id);
Objects.requireNonNull(nodeProjectInfoModel, "获取项目信息失败");
return nodeProjectInfoModel;
}
use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.
the class ProjectFileControl method upload.
@RequestMapping(value = "upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String upload() throws Exception {
NodeProjectInfoModel pim = getProjectInfoModel();
MultipartFileBuilder multipartFileBuilder = createMultipart().addFieldName("file").setUseOriginalFilename(true);
// 压缩文件
String type = getParameter("type");
// 是否清空
String clearType = getParameter("clearType");
String levelName = getParameter("levelName");
File lib;
if (StrUtil.isEmpty(levelName)) {
lib = new File(pim.allLib());
} else {
lib = FileUtil.file(pim.allLib(), levelName);
}
// 判断是否需要清空
if ("clear".equalsIgnoreCase(clearType)) {
if (!FileUtil.clean(lib)) {
FileUtil.del(lib.toPath());
// return JsonMessage.getString(500, "清除旧lib失败");
}
}
if ("unzip".equals(type)) {
multipartFileBuilder.setFileExt(StringUtil.PACKAGE_EXT);
multipartFileBuilder.setSavePath(AgentConfigBean.getInstance().getTempPathName());
String path = multipartFileBuilder.save();
// 解压
File file = new File(path);
try {
CompressionFileUtil.unCompress(file, lib);
} finally {
if (!FileUtil.del(file)) {
DefaultSystemLog.getLog().error("删除文件失败:" + file.getPath());
}
}
} else {
multipartFileBuilder.setSavePath(FileUtil.getAbsolutePath(lib));
// 保存
multipartFileBuilder.save();
}
// 修改使用状态
pim.setUseLibDesc("upload");
projectInfoService.updateItem(pim);
//
String after = getParameter("after");
if (StrUtil.isNotEmpty(after)) {
//
List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = pim.getJavaCopyItemList();
//
AfterOpt afterOpt = BaseEnum.getEnum(AfterOpt.class, Convert.toInt(after, AfterOpt.No.getCode()));
if ("restart".equalsIgnoreCase(after) || afterOpt == AfterOpt.Restart) {
String result = consoleService.execCommand(ConsoleCommandOp.restart, pim, null);
// 自动处理副本集
if (javaCopyItemList != null) {
ThreadUtil.execute(() -> javaCopyItemList.forEach(javaCopyItem -> {
try {
consoleService.execCommand(ConsoleCommandOp.restart, pim, javaCopyItem);
} catch (Exception e) {
DefaultSystemLog.getLog().error("重启副本集失败", e);
}
}));
}
return JsonMessage.getString(200, "上传成功并重启:" + result);
}
if (afterOpt == AfterOpt.Order_Restart || afterOpt == AfterOpt.Order_Must_Restart) {
boolean restart = this.restart(pim, null, afterOpt);
if (javaCopyItemList != null) {
ThreadUtil.execute(() -> {
// 副本
for (NodeProjectInfoModel.JavaCopyItem javaCopyItem : javaCopyItemList) {
if (!this.restart(pim, javaCopyItem, afterOpt)) {
return;
}
// 休眠30秒 等待之前项目正常启动
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException ignored) {
}
}
});
}
}
}
return JsonMessage.getString(200, "上传成功");
}
use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.
the class ProjectFileControl method remoteDownload.
/**
* 下载远程文件
*
* @param id 项目id
* @param url 远程 url 地址
* @param levelName 保存的文件夹
* @param unzip 是否为压缩包、true 将自动解压
* @return json
*/
@PostMapping(value = "remote_download", produces = MediaType.APPLICATION_JSON_VALUE)
public String remoteDownload(String id, String url, String levelName, String unzip) {
Assert.hasText(url, "请输入正确的远程地址");
AgentWhitelist whitelist = whitelistDirectoryService.getWhitelist();
Set<String> allowRemoteDownloadHost = whitelist.getAllowRemoteDownloadHost();
Assert.state(CollUtil.isNotEmpty(allowRemoteDownloadHost), "还没有配置运行的远程地址");
List<String> collect = allowRemoteDownloadHost.stream().filter(s -> StrUtil.startWith(url, s)).collect(Collectors.toList());
Assert.state(CollUtil.isNotEmpty(collect), "不允许下载当前地址的文件");
try {
NodeProjectInfoModel pim = projectInfoService.getItem(id);
File file = FileUtil.file(pim.allLib(), StrUtil.emptyToDefault(levelName, FileUtil.FILE_SEPARATOR));
File downloadFile = HttpUtil.downloadFileFromUrl(url, file);
if (BooleanUtil.toBoolean(unzip)) {
// 需要解压文件
try {
CompressionFileUtil.unCompress(file, downloadFile);
} finally {
if (!FileUtil.del(downloadFile)) {
DefaultSystemLog.getLog().error("删除文件失败:" + file.getPath());
}
}
}
return JsonMessage.getString(200, "下载成功文件大小:" + FileUtil.readableFileSize(downloadFile));
} catch (Exception e) {
DefaultSystemLog.getLog().error("下载远程文件异常", e);
return JsonMessage.getString(500, "下载远程文件失败:" + e.getMessage());
}
}
use of io.jpom.model.data.NodeProjectInfoModel in project Jpom by dromara.
the class ProjectFileControl method download.
/**
* 将执行文件下载到客户端 本地
*
* @param id 项目id
* @param filename 文件名
* @param levelName 文件夹名
* @return 正常情况返回文件流,非正在返回 text plan
*/
@GetMapping(value = "download", produces = MediaType.APPLICATION_JSON_VALUE)
public String download(String id, String filename, String levelName) {
Assert.hasText(filename, "请选择文件");
// }
try {
NodeProjectInfoModel pim = projectInfoService.getItem(id);
File file = FileUtil.file(pim.allLib(), StrUtil.emptyToDefault(levelName, FileUtil.FILE_SEPARATOR), filename);
if (file.isDirectory()) {
return "暂不支持下载文件夹";
}
ServletUtil.write(getResponse(), file);
} catch (Exception e) {
DefaultSystemLog.getLog().error("下载文件异常", e);
}
return "下载失败。请刷新页面后重试";
}
Aggregations