use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.
the class ProjectFileControl method getFileList.
@RequestMapping(value = "getFileList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getFileList(String id, String path) {
// 查询项目路径
NodeProjectInfoModel pim = projectInfoService.getItem(id);
Assert.notNull(pim, "查询失败:项目不存在");
String lib = pim.allLib();
File fileDir = FileUtil.file(lib, StrUtil.emptyToDefault(path, FileUtil.FILE_SEPARATOR));
boolean exist = FileUtil.exist(fileDir);
if (!exist) {
return JsonMessage.getString(200, "查询成功", new JSONArray());
}
//
File[] filesAll = fileDir.listFiles();
if (ArrayUtil.isEmpty(filesAll)) {
return JsonMessage.getString(200, "查询成功", new JSONArray());
}
JSONArray arrayFile = FileUtils.parseInfo(filesAll, false, lib);
AgentWhitelist whitelist = whitelistDirectoryService.getWhitelist();
for (Object o : arrayFile) {
JSONObject jsonObject = (JSONObject) o;
String filename = jsonObject.getString("filename");
jsonObject.put("textFileEdit", AgentWhitelist.checkSilentFileSuffix(whitelist.getAllowEditSuffix(), filename));
}
return JsonMessage.getString(200, "查询成功", arrayFile);
}
use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.
the class WhitelistDirectoryController method whiteList.
/**
* get whiteList data
* 白名单数据接口
*
* @return json
* @author Hotstrip
*/
@RequestMapping(value = "white-list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@SystemPermission
public String whiteList() {
AgentWhitelist agentWhitelist = whitelistDirectoryService.getData(getNode());
Map<String, String> map = new HashMap<>(8);
if (agentWhitelist != null) {
/**
* put key and value into map
* 赋值给 map 对象返回
*/
Field[] fields = ReflectUtil.getFields(AgentWhitelist.class);
for (Field field : fields) {
Collection<String> fieldValue = (Collection<String>) ReflectUtil.getFieldValue(agentWhitelist, field);
map.put(field.getName(), AgentWhitelist.convertToLine(fieldValue));
}
}
return JsonMessage.getString(200, "ok", map);
}
use of io.jpom.model.data.AgentWhitelist 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.AgentWhitelist in project Jpom by dromara.
the class ProjectFileControl method updateConfigFile.
/**
* 保存文件内容 (只能处理文本文件)
*
* @param filePath 相对项目文件的文件夹
* @param filename 读取的文件名
* @param fileText 文件内容
* @return json
*/
@PostMapping(value = "update_config_file", produces = MediaType.APPLICATION_JSON_VALUE)
public String updateConfigFile(String filePath, String filename, String fileText) {
NodeProjectInfoModel pim = getProjectInfoModel();
filePath = StrUtil.emptyToDefault(filePath, File.separator);
// 判断文件后缀
AgentWhitelist whitelist = whitelistDirectoryService.getWhitelist();
Charset charset = AgentWhitelist.checkFileSuffix(whitelist.getAllowEditSuffix(), filename);
FileUtil.writeString(fileText, FileUtil.file(pim.allLib(), filePath, filename), charset);
return JsonMessage.getString(200, "文件写入成功");
}
use of io.jpom.model.data.AgentWhitelist in project Jpom by dromara.
the class NginxService method list.
public List<JSONObject> list(String whitePath, String fileName, String showAll) {
AgentWhitelist agentWhitelist = whitelistDirectoryService.getWhitelist();
if (agentWhitelist == null) {
return null;
}
List<String> ngxDirectory = agentWhitelist.getNginx();
if (ngxDirectory == null) {
return null;
}
File normalize = FileUtil.file(whitePath, fileName);
File[] files = FileUtil.ls(normalize.getAbsolutePath());
if (files == null || files.length <= 0) {
return null;
}
String[] suffixes = Convert.toBool(showAll, false) ? new String[] { ".conf", ".conf_back" } : new String[] { ".conf" };
return Arrays.stream(files).map(file -> {
String name = file.getName();
if (file.isDirectory()) {
return null;
}
if (!StrUtil.endWithAnyIgnoreCase(name, suffixes)) {
return null;
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("path", whitePath);
long time = file.lastModified();
jsonObject.put("time", DateUtil.date(time).toString());
jsonObject.put("name", name);
jsonObject.put("relativePath", FileUtil.normalize(fileName + StrUtil.SLASH + name));
try {
NgxConfig config = NgxConfig.read(file.getPath());
List<NgxEntry> server = config.findAll(NgxBlock.class, "server");
JSONObject data = findSeverName(server);
if (data != null) {
jsonObject.putAll(data);
}
} catch (IOException e) {
DefaultSystemLog.getLog().error(e.getMessage(), e);
}
return jsonObject;
}).filter(Objects::nonNull).collect(Collectors.toList());
}
Aggregations