From eddc58dd68f87eff63aed7be0aaa1612811cc2d9 Mon Sep 17 00:00:00 2001 From: zywang Date: Mon, 30 Dec 2024 10:51:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=96=87=E4=BB=B6=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/pkg/FileSystemController.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 proxy/src/main/java/io/mpms/controller/pkg/FileSystemController.java diff --git a/proxy/src/main/java/io/mpms/controller/pkg/FileSystemController.java b/proxy/src/main/java/io/mpms/controller/pkg/FileSystemController.java new file mode 100644 index 0000000..728e54e --- /dev/null +++ b/proxy/src/main/java/io/mpms/controller/pkg/FileSystemController.java @@ -0,0 +1,112 @@ +package io.jpom.controller; + +import cn.jiangzeyin.common.JsonMessage; +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import io.jpom.api.req.FilePathReq; +import io.jpom.api.resp.FileResp; +import io.jpom.model.NorMalPathTreeEntity; +import io.jpom.util.FileOperatorUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.tika.Tika; +import org.apache.tika.metadata.Metadata; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.File; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.List; + +@RestController +@Slf4j +@RequestMapping("/fs") +public class FileSystemController { + private static Tika tika = new Tika(); + + + + public JsonMessage ls(@RequestBody FilePathReq filePathReq) { + String filterName = ""; + int pageNum = filePathReq.getPageNum(); + int pageSize = filePathReq.getPageSize(); + Page page = PageHelper.startPage(pageNum, pageSize); + int startRow = (int) page.getStartRow(); + int endRow = (int) page.getEndRow(); + ArrayList fileResps = new ArrayList<>(); + String path = filePathReq.getPath(); + File file = new File(path); + if (!file.exists() && !file.getParentFile().exists()) { + return new JsonMessage(500, "目录或文件不存在", fileResps); + } + if (!file.exists() && file.getParentFile().exists()) { + filterName = file.getName(); + file = file.getParentFile(); + } + if (file.isFile()) { + filterName = file.getName(); + file = file.getParentFile(); + } + String finalFilterName = filterName; + File[] files = file.listFiles((dir, name) -> name.equals(finalFilterName) || name.startsWith(finalFilterName)); + int index = 0; + + assert files != null; + for (File file1 : files) { + if (index < startRow) { + index++; + continue; + } + FileResp fileResp = new FileResp(); + fileResp.setName(file1.getName()); + if (file1.isDirectory()) { + fileResp.setType("directory"); + fileResp.setIsRead(false); + } else { + if (file.getPath().equals("/dev")) { + try (FileInputStream fileInputStream = new FileInputStream(file1)) { + if (file1.canRead() && fileInputStream.available() > 0) { + String type = tika.detect(file1); + fileResp.setType(type); + fileResp.setIsRead(isFileRead(type)); + } else { + fileResp.setType("不可读文件[设备文件]"); + fileResp.setIsRead(false); + } + } catch (Exception e) { + log.error("解析文件类型异常\\n{}", ExceptionUtils.getStackTrace(e)); + } + } else { + try { + String type = tika.detect(file1); + fileResp.setType(type); + fileResp.setIsRead(isFileRead(type)); + } catch (Exception e) { + log.error("解析文件类型异常\\n{}", ExceptionUtils.getStackTrace(e)); + } + } + } + + if (index >= startRow && index < endRow) { + fileResps.add(fileResp); + } + if (index >= endRow) { + break; + } + index++; + + } + page.addAll(fileResps); + page.setTotal(files.length); + return new JsonMessage(200, "success", new PageInfo<>(page)); + } + + + + + + +} -- Gitee