diff --git a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/FileService.java b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/FileService.java index 75832cf..ec0219d 100644 --- a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/FileService.java +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/FileService.java @@ -4,9 +4,19 @@ import cn.hutool.http.HttpUtil; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Set; @Service public class FileService { + private static final Set UNSUPPORTED_FILE_TYPES = Set.of( + ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".ico", // 图片 + ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", // 文档 + ".zip", ".rar", ".7z", ".tar", ".gz", // 压缩文件 + ".exe", ".dll", ".so", ".class", // 可执行文件 + ".jar", ".war", ".ear", // Java归档 + ".gitignore", ".gitattributes", ".gitmodules" // Git配置文件 + ); + // repo: http://47.121.181.112:3000/wonder/lark-webhook-sender // prefix: /raw/branch/main/ // raw: gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java @@ -19,6 +29,13 @@ public class FileService { public String getRawContents(String repo, String raw) { String url = getRawUrl(repo, raw); String fileName = raw.substring(raw.lastIndexOf("/")+1); + + // 检查文件拓展名 + String fileExtension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase(); + if (UNSUPPORTED_FILE_TYPES.contains(fileExtension)) { + return "[FileName]" + fileName + "\n[FileContent]*不支持显示的文件类型,请忽略\n"; + } + String fileContent = HttpUtil.get(url); return "[FileName]" + fileName + "\n" + "[FileContent]" + fileContent + "\n"; }