🐛Fix: 排除部分文件读取异常
This commit is contained in:
@@ -4,9 +4,19 @@ import cn.hutool.http.HttpUtil;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class FileService {
|
public class FileService {
|
||||||
|
private static final Set<String> 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
|
// repo: http://47.121.181.112:3000/wonder/lark-webhook-sender
|
||||||
// prefix: /raw/branch/main/
|
// prefix: /raw/branch/main/
|
||||||
// raw: gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java
|
// 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) {
|
public String getRawContents(String repo, String raw) {
|
||||||
String url = getRawUrl(repo, raw);
|
String url = getRawUrl(repo, raw);
|
||||||
String fileName = raw.substring(raw.lastIndexOf("/")+1);
|
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);
|
String fileContent = HttpUtil.get(url);
|
||||||
return "[FileName]" + fileName + "\n" + "[FileContent]" + fileContent + "\n";
|
return "[FileName]" + fileName + "\n" + "[FileContent]" + fileContent + "\n";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user