Forráskód Böngészése

update NginxController

reghao 2 hónapja
szülő
commit
97747421eb

+ 17 - 11
web/src/main/java/cn/reghao/bnt/web/devops/srv/controller/NginxController.java

@@ -35,24 +35,30 @@ public class NginxController {
 
     @Operation(summary = "", description = "N")
     @GetMapping(value = "/tree", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String getFileTree(@RequestParam(value = "machineId") String machineId) {
-        List<FileTree> list = fileTreeService.getFileTree(baseDir);
-        return WebResult.success(list);
+    public String getFileTree(@RequestParam(value = "machineId") String machineId,
+                              @RequestParam(value = "path") String path) {
+        //List<FileTree> list = fileTreeService.getFileTree(path);
+        List<FileTree> list = fileTreeService.getFileTree1(path);
+        if (list != null) {
+            return WebResult.success(list);
+        } else {
+            return WebResult.failWithMsg(String.format("path %s not exist", path));
+        }
     }
 
     @Operation(summary = "", description = "N")
     @GetMapping(value = "/conf", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getNginxConf(@RequestParam(value = "machineId") String machineId,
                                @RequestParam(value = "path") String path) {
-        if (path.startsWith(baseDir)) {
-            String content = nginxService.getNginxConf(machineId, path);
-            if (content.isBlank()) {
-                return WebResult.failWithMsg("not text file");
-            } else {
-                return WebResult.success(content);
-            }
+        String content = nginxService.getNginxConf(machineId, path);
+        if (content == null) {
+            return WebResult.failWithMsg("file not exist");
+        }
+
+        if (content.isBlank()) {
+            return WebResult.failWithMsg("not text file");
         } else {
-            return WebResult.failWithMsg("path invalid");
+            return WebResult.success(content);
         }
     }
 

+ 28 - 1
web/src/main/java/cn/reghao/bnt/web/devops/srv/service/FileTreeService.java

@@ -68,9 +68,15 @@ public class FileTreeService {
         list.add(new FileTree(type, path));
     }
 
-    public List<FileTree> getFileTree(String baseDir) {
+    public List<FileTree> getFileTree(String dirPath) {
+        File dir = new File(dirPath);
+        if (!dir.exists() || !dir.isDirectory()) {
+            return null;
+        }
+
         list.clear();
         try {
+            String baseDir = dir.getAbsolutePath();
             walkDir(Path.of(baseDir), baseDir);
             Map<String, FileTree> map = list.stream()
                     .collect(Collectors.toMap(FileTree::getPath, v -> v));
@@ -131,4 +137,25 @@ public class FileTreeService {
 
         return Collections.emptyList();
     }
+
+    public List<FileTree> getFileTree1(String dirPath) {
+        File dir = new File(dirPath);
+        if (!dir.exists() || !dir.isDirectory()) {
+            return null;
+        }
+
+        File[] files = dir.listFiles();
+        int len = files.length;
+        List<FileTree> list = new ArrayList<>();
+        for (int i = 0; i < Math.min(len, 1000); i++) {
+            File file = files[i];
+            String path = file.getAbsolutePath();
+            String type = "file";
+            if (file.isDirectory()) {
+                type = "dir";
+            }
+            list.add(new FileTree(type, path));
+        }
+        return list;
+    }
 }

+ 9 - 4
web/src/main/java/cn/reghao/bnt/web/devops/srv/service/NginxService.java

@@ -22,11 +22,16 @@ public class NginxService {
     public String getNginxConf(String machineId, String confPath) {
         File file = new File(confPath);
         if (file.exists() && file.isFile()) {
-            String content = textFile.readFile(file.getAbsolutePath());
-            return content;
+            long len = file.length();
+            if (len > 10 * 1024 * 1024) { // 10MB
+                return "";
+            } else {
+                String content = textFile.readFile(file.getAbsolutePath());
+                return content;
+            }
         }
-        String filePath = "/etc/nginx/nginx.conf";
-        return "";
+
+        return null;
     }
 
     public Result updateNginxConf(NginxConf nginxConf) {