新增系统设置页面,支持在线配置上传文件地址、日志文件地址等参数

This commit is contained in:
WIN-VSNMD38DUOC\Administrator
2024-04-30 11:13:42 +08:00
parent e2d5ad6d1a
commit e5872de15b
15 changed files with 304 additions and 21 deletions

View File

@@ -4,6 +4,7 @@ import cn.somkit.fmt.annotation.ApiOperate;
import cn.somkit.fmt.config.FmtConfig;
import cn.somkit.fmt.config.RocksDBConfig;
import cn.somkit.fmt.utils.MD5Utils;
import cn.somkit.fmt.utils.ParamUtils;
import cn.somkit.fmt.utils.PathUtils;
import cn.somkit.fmt.utils.RocksDBUtils;
import jakarta.servlet.http.HttpServletResponse;
@@ -31,10 +32,9 @@ import java.util.zip.ZipOutputStream;
public class DownloadAction {
@GetMapping("/index")
@ApiOperate(description = "下载页面跳转")
public ModelAndView index(String keyboard) throws Exception{
PathUtils.directory(FmtConfig.Upload_File_Path);
File folder = new File(FmtConfig.Upload_File_Path);
PathUtils.directory(ParamUtils.getUploadPath());
File folder = new File(ParamUtils.getUploadPath());
File[] listOfFiles = folder.listFiles();
List<Map<String, Object>> list = new ArrayList<>();
List<String> keys = RocksDBUtils.getAllKey(RocksDBConfig.RocksDB_Column_Family);
@@ -124,12 +124,12 @@ public class DownloadAction {
@ApiOperate(description = "批量文件打包下载")
public Map<String, Object> packZip(String filenames) throws Exception{
try {
PathUtils.directory(FmtConfig.Upload_File_Path);
PathUtils.directory(FmtConfig.Temp_File_Path);
PathUtils.directory(ParamUtils.getUploadPath());
PathUtils.directory(ParamUtils.getTempFilePath());
String zipName = String.valueOf(System.currentTimeMillis());
String zipDir = FmtConfig.Temp_File_Path + File.separator + zipName + ".zip";
String zipDir = ParamUtils.getTempFilePath() + File.separator + zipName + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipDir));
File folder = new File(FmtConfig.Upload_File_Path);
File folder = new File(ParamUtils.getUploadPath());
File[] listOfFiles = folder.listFiles();
assert listOfFiles != null;
for (File file : listOfFiles) {
@@ -171,8 +171,8 @@ public class DownloadAction {
@ResponseBody
@ApiOperate(description = "批量文件删除")
public String batchDel(String filenames) throws Exception{
PathUtils.directory(FmtConfig.Upload_File_Path);
File folder = new File(FmtConfig.Upload_File_Path);
PathUtils.directory(ParamUtils.getUploadPath());
File folder = new File(ParamUtils.getUploadPath());
File[] listOfFiles = folder.listFiles();
assert listOfFiles != null;
for (File file : listOfFiles) {

View File

@@ -9,7 +9,6 @@ import org.springframework.web.servlet.ModelAndView;
public class IndexAction {
@RequestMapping("/")
@ApiOperate(description = "默认跳转")
public ModelAndView index(){
return new ModelAndView("redirect:/download/index");
}

View File

@@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
public class LoggingAction {
@GetMapping("/index")
@ApiOperate(description = "在线日志页面跳转")
public String index() throws Exception{
return "logging";
}

View File

@@ -0,0 +1,53 @@
package cn.somkit.fmt.action;
import cn.somkit.fmt.annotation.ApiOperate;
import cn.somkit.fmt.config.FmtConfig;
import cn.somkit.fmt.utils.ParamUtils;
import cn.somkit.fmt.utils.PathUtils;
import cn.somkit.fmt.utils.RocksDBUtils;
import org.rocksdb.RocksDBException;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/system")
public class SystemAction {
@GetMapping("/index")
public ModelAndView index() throws Exception{
Map<String, String> map = new HashMap<>();
map.put("Upload_File_Path", ParamUtils.getUploadPath());
map.put("Temp_File_Path", ParamUtils.getTempFilePath());
map.put("Log_File_Path", ParamUtils.getLogFilePath());
map.put("Max_Read_Length", ParamUtils.getMaxReadLength());
map.put("Read_Interval", ParamUtils.getReadInterval());
ModelAndView mv = new ModelAndView();
mv.setViewName("system");
mv.addObject(FmtConfig.System_Cf_Name, map);
return mv;
}
@PostMapping("/save")
@ResponseBody
@ApiOperate(description = "保存系统设置")
public Map<String, Object> save(@RequestParam Map<String, String> map) throws Exception {
if(map != null && !map.isEmpty()){
map.keySet().forEach(key -> {
try {
if(StringUtils.hasText(map.get(key))){
String value = map.get(key).startsWith("./") ? PathUtils.resolve(map.get(key)) : map.get(key);
RocksDBUtils.put(FmtConfig.System_Cf_Name, key, value);
}
} catch (RocksDBException e) {
throw new RuntimeException(e);
}
});
}
return Map.of("code", 200, "message", "保存成功");
}
}

View File

@@ -4,6 +4,7 @@ import cn.somkit.fmt.annotation.ApiOperate;
import cn.somkit.fmt.config.FmtConfig;
import cn.somkit.fmt.config.RocksDBConfig;
import cn.somkit.fmt.utils.MD5Utils;
import cn.somkit.fmt.utils.ParamUtils;
import cn.somkit.fmt.utils.PathUtils;
import cn.somkit.fmt.utils.RocksDBUtils;
import jakarta.servlet.http.HttpServletRequest;
@@ -24,7 +25,6 @@ import java.util.*;
public class UploadAction {
@GetMapping("/index")
@ApiOperate(description = "上传页面跳转")
public String index() throws Exception{
return "upload";
}
@@ -35,7 +35,7 @@ public class UploadAction {
public Map<String, Object> execute(HttpServletRequest request) throws Exception{
//多个文件上传 就只是简单的多文件上传保存在本地的磁盘
if (request instanceof MultipartHttpServletRequest mrequest) {
PathUtils.directory(FmtConfig.Upload_File_Path);
PathUtils.directory(ParamUtils.getUploadPath());
List<MultipartFile> files = mrequest.getFiles("file");
// 取出每一个上传文件
for (MultipartFile file : files) {
@@ -64,7 +64,7 @@ public class UploadAction {
if(StringUtils.hasText(fileName)){
String hash = MD5Utils.md5HashCode(file.getInputStream());
RocksDBUtils.put(RocksDBConfig.RocksDB_Column_Family, fileName, hash);
filePath = FmtConfig.Upload_File_Path + File.separator + fileName;
filePath = ParamUtils.getUploadPath() + File.separator + fileName;
File saveFile = new File(filePath) ;
file.transferTo(saveFile); // 文件保存
}

View File

@@ -1,5 +1,6 @@
package cn.somkit.fmt.config;
import cn.somkit.fmt.utils.PathUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@@ -16,6 +17,10 @@ public class LogSocketConfig {
@Value("${somkit.logging.socket.log-file-path}")
public void setLog_File_Path(String log_File_Path) {
if(StringUtils.hasText(log_File_Path)){
//如果使用相对路径,转换为绝对路径
if(log_File_Path.startsWith("./")){
log_File_Path = PathUtils.resolve(log_File_Path);
}
Log_File_Path = log_File_Path;
}
}

View File

@@ -1,5 +1,6 @@
package cn.somkit.fmt.config;
import cn.somkit.fmt.utils.PathUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@@ -14,6 +15,10 @@ public class RocksDBConfig {
@Value("${somkit.cache.rocks-db.path}")
public void setRocksDB_Path(String rocksDB_Path) {
if(StringUtils.hasText(rocksDB_Path)){
//如果使用相对路径,转换为绝对路径
if(rocksDB_Path.startsWith("./")){
rocksDB_Path = PathUtils.resolve(rocksDB_Path);
}
RocksDB_Path = rocksDB_Path;
}
}

View File

@@ -27,7 +27,7 @@ public class LogSocketUtils {
BufferedReader reader = null;
try {
//日志文件路径
String filePath = LogSocketConfig.Log_File_Path;
String filePath = ParamUtils.getLogFilePath();
//字符流
reader = new BufferedReader(new FileReader(filePath));
Object[] lines = reader.lines().toArray();
@@ -61,7 +61,7 @@ public class LogSocketUtils {
WebSocketOnline.logLengthMap.put(socketSessionId, lines.length);
//第一次如果太大截取最新的200行就够了避免传输的数据太大
int maxLength = Integer.parseInt(LogSocketConfig.Max_Read_Length);
int maxLength = Integer.parseInt(ParamUtils.getMaxReadLength());
if(first && copyOfRange.length > maxLength){
copyOfRange = Arrays.copyOfRange(copyOfRange, copyOfRange.length - maxLength, copyOfRange.length);
first = false;
@@ -74,7 +74,7 @@ public class LogSocketUtils {
WebSocketOnline.sendMessage(socketSessionId, JSONUtil.toJsonStr(entity));
//休眠一秒
Thread.sleep(Long.parseLong(LogSocketConfig.Read_Interval));
Thread.sleep(Long.parseLong(ParamUtils.getReadInterval()));
} catch (Exception e) {
logger.error("读取日志异常:{}", ErrorUtil.errorInfoToString(e));
} finally {

View File

@@ -0,0 +1,63 @@
package cn.somkit.fmt.utils;
import cn.somkit.fmt.config.FmtConfig;
import cn.somkit.fmt.config.LogSocketConfig;
import org.springframework.util.StringUtils;
public class ParamUtils {
public static String getUploadPath() throws Exception {
String path = RocksDBUtils.get(FmtConfig.System_Cf_Name, "Upload_File_Path");
if(StringUtils.hasText(path)){
return path;
} else {
path = FmtConfig.Upload_File_Path;
RocksDBUtils.put(FmtConfig.System_Cf_Name, "Upload_File_Path", path);
return path;
}
}
public static String getTempFilePath() throws Exception {
String path = RocksDBUtils.get(FmtConfig.System_Cf_Name, "Temp_File_Path");
if(StringUtils.hasText(path)){
return path;
} else {
path = FmtConfig.Temp_File_Path;
RocksDBUtils.put(FmtConfig.System_Cf_Name, "Temp_File_Path", path);
return path;
}
}
public static String getLogFilePath() throws Exception {
String path = RocksDBUtils.get(FmtConfig.System_Cf_Name, "Log_File_Path");
if(StringUtils.hasText(path)){
return path;
} else {
path = LogSocketConfig.Log_File_Path;
RocksDBUtils.put(FmtConfig.System_Cf_Name, "Log_File_Path", path);
return path;
}
}
public static String getMaxReadLength() throws Exception {
String path = RocksDBUtils.get(FmtConfig.System_Cf_Name, "Max_Read_Length");
if(StringUtils.hasText(path)){
return path;
} else {
path = LogSocketConfig.Max_Read_Length;
RocksDBUtils.put(FmtConfig.System_Cf_Name, "Max_Read_Length", path);
return path;
}
}
public static String getReadInterval() throws Exception {
String path = RocksDBUtils.get(FmtConfig.System_Cf_Name, "Read_Interval");
if(StringUtils.hasText(path)){
return path;
} else {
path = LogSocketConfig.Read_Interval;
RocksDBUtils.put(FmtConfig.System_Cf_Name, "Read_Interval", path);
return path;
}
}
}

View File

@@ -23,10 +23,6 @@ public class RocksDBUtils {
static {
try {
String rocksDBPath = RocksDBConfig.RocksDB_Path; //RocksDB文件目录
//如果使用相对路径,转换为绝对路径
if(rocksDBPath != null && rocksDBPath.startsWith("./")){
rocksDBPath = PathUtils.resolve(rocksDBPath);
}
PathUtils.directory(rocksDBPath); // 创建RocksDB文件目录
RocksDB.loadLibrary();
Options options = new Options();

View File

@@ -43,6 +43,10 @@
<a th:href="@{/logging/index}" class="ax-text">在线日志</a>
<span class="ax-line"></span>
</div>
<div class="ax-item">
<a th:href="@{/system/index}" class="ax-text">系统设置</a>
<span class="ax-line"></span>
</div>
</div>
</div>
</header>

View File

@@ -36,6 +36,10 @@
<a th:href="@{/logging/index}" class="ax-text">在线日志</a>
<span class="ax-line"></span>
</div>
<div class="ax-item">
<a th:href="@{/system/index}" class="ax-text">系统设置</a>
<span class="ax-line"></span>
</div>
</div>
</div>
</header>

View File

@@ -0,0 +1,150 @@
<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="https://www.thymeleaf.org">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="apple-touch-fullscreen" content="yes"/>
<meta name="format-detection" content="email=no" />
<meta name="wap-font-scale" content="no" />
<meta name="viewport" content="user-scalable=no, width=device-width" />
<meta content="telephone=no" name="format-detection" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系统设置</title>
<link th:href="@{/axui-v2.1.1/css/ax.css}" rel="stylesheet" type="text/css" >
<link th:href="@{/axui-v2.1.1/css/ax-response.css}" rel="stylesheet" type="text/css" >
</head>
<body>
<header class="ax-header">
<div class="ax-row">
<div class="ax-col">
<a th:href="@{/download/index}" class="ax-logo">
<img th:src="@{/common/images/logo.png}" alt="File Management"/>
</a>
</div>
<div class="ax-nav">
<div class="ax-item">
<a th:href="@{/download/index}" class="ax-text">文件列表</a>
<span class="ax-line"></span>
</div>
<div class="ax-item">
<a th:href="@{/upload/index}" class="ax-text">文件上传</a>
<span class="ax-line"></span>
</div>
<div class="ax-item">
<a th:href="@{/logging/index}" class="ax-text">在线日志</a>
<span class="ax-line"></span>
</div>
<div class="ax-item ax-selected">
<a th:href="@{/system/index}" class="ax-text">系统设置</a>
<span class="ax-line"></span>
</div>
</div>
</div>
</header>
<div class="ax-space-header"></div>
<div id="app" class="ax-border ax-margin">
<div class="ax-break-md"></div>
<div class="ax-form-group">
<div class="ax-flex-row">
<div class="ax-form-label">上传文件存放地址:</div>
<div class="ax-form-con">
<div class="ax-form-input">
<input name="Upload_File_Path" th:value="${system.Upload_File_Path}" type="text">
</div>
</div>
<span class="ax-form-txt ax-color-ignore">相对路径以“./”开头,绝对路径填写完整目录</span>
</div>
</div>
<div class="ax-break-md"></div>
<div class="ax-form-group">
<div class="ax-flex-row">
<div class="ax-form-label">临时文件存放地址:</div>
<div class="ax-form-con">
<div class="ax-form-input">
<input name="Temp_File_Path" th:value="${system.Temp_File_Path}" type="text">
</div>
</div>
<span class="ax-form-txt ax-color-ignore">相对路径以“./”开头,绝对路径填写完整目录</span>
</div>
</div>
<div class="ax-break-md"></div>
<div class="ax-form-group">
<div class="ax-flex-row">
<div class="ax-form-label">日志文件地址:</div>
<div class="ax-form-con">
<div class="ax-form-input">
<input name="Log_File_Path" th:value="${system.Log_File_Path}" type="text">
</div>
</div>
<span class="ax-form-txt ax-color-ignore">相对路径以“./”开头,绝对路径填写完整目录</span>
</div>
</div>
<div class="ax-break-md"></div>
<div class="ax-form-group">
<div class="ax-flex-row">
<div class="ax-form-label">最大读取展示行数:</div>
<div class="ax-form-con">
<div class="ax-form-input">
<div class="ax-amount" axAmount>
<input name="Max_Read_Length" th:value="${system.Max_Read_Length}" type="text">
<a href="#" decrease><i class="ax-iconfont ax-icon-minus"></i></a>
<a href="#" increase><i class="ax-iconfont ax-icon-plus"></i></a>
</div>
</div>
</div>
<span class="ax-form-txt ax-color-ignore">在线日志文件最大读取行数,建议设置较大值以提高性能</span>
</div>
</div>
<div class="ax-break-md"></div>
<div class="ax-form-group">
<div class="ax-flex-row">
<div class="ax-form-label">读取间隔时间(毫秒)</div>
<div class="ax-form-con">
<div class="ax-form-input">
<div class="ax-amount" axAmount>
<input name="Read_Interval" th:value="${system.Read_Interval}" type="text">
<a href="#" decrease><i class="ax-iconfont ax-icon-minus"></i></a>
<a href="#" increase><i class="ax-iconfont ax-icon-plus"></i></a>
</div>
</div>
</div>
<span class="ax-form-txt ax-color-ignore">在线日志文件读取间隔时间,建议设置较小值以提高性能</span>
</div>
</div>
<div class="ax-break-md"></div>
<div class="ax-form-group">
<div class="ax-flex-row">
<div class="ax-form-label"></div>
<div class="ax-flex-block">
<div class="ax-form-input">
<button type="button" id="saveSystemSetting" class="ax-btn ax-primary ax-full">保存系统设置</button>
</div>
</div>
</div>
</div>
<div class="ax-break-md"></div>
</div>
<script th:src="@{/axui-v2.1.1/js/ax.js}" type="text/javascript" charset="utf-8"></script>
<script th:src="@{/common/js/basic.js}" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript" charset="utf-8">
window.onload = () => {
document.getElementById("saveSystemSetting").onclick = () => {
const options = {
url: Fmt.ctx() + '/system/save',
data: Fmt.utils.serialize('#app', 'json'),
method: 'post'
};
Fmt.axios(options).then((result) => {
console.log(result);
new axMessage({content: result.message, result: 'success', iconShow: true}).show();
}).catch((err) => {
console.log(err);
new axMessage({content: err, result: 'error', iconShow: true}).show();
});
}
}
</script>
</body>
</html>

View File

@@ -36,6 +36,10 @@
<a th:href="@{/logging/index}" class="ax-text">在线日志</a>
<span class="ax-line"></span>
</div>
<div class="ax-item">
<a th:href="@{/system/index}" class="ax-text">系统设置</a>
<span class="ax-line"></span>
</div>
</div>
</div>
</header>

View File

@@ -22,4 +22,5 @@
修改RocksDB文件目录配置使其支持使用相对路径
修改上传文件目录配置,使其支持使用相对路径
新增临时文件存放地址配置,支持相对路径
新增系统设置页面,支持在线配置上传文件地址、日志文件地址等参数
```