97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package util
|
|
|
|
import (
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// AllowedMIMEPrefixes 允许的文本类 MIME 类型前缀
|
|
AllowedMIMEPrefixes = []string{
|
|
"text/",
|
|
"application/json",
|
|
"application/xml",
|
|
"application/javascript",
|
|
"application/x-yaml",
|
|
"application/yaml",
|
|
"application/toml",
|
|
"application/x-httpd-php",
|
|
"application/x-sh",
|
|
"application/x-python",
|
|
"application/x-perl",
|
|
"application/x-ruby",
|
|
}
|
|
|
|
// BannedExtensions 禁止的文件扩展名
|
|
BannedExtensions = map[string]bool{
|
|
".png": true, ".jpg": true, ".jpeg": true, ".gif": true, ".bmp": true,
|
|
".webp": true, ".svg": true, ".ico": true, ".tiff": true, ".tif": true,
|
|
".mp3": true, ".wav": true, ".ogg": true, ".flac": true, ".aac": true,
|
|
".wma": true, ".m4a": true,
|
|
".mp4": true, ".avi": true, ".mkv": true, ".mov": true, ".wmv": true,
|
|
".flv": true, ".webm": true,
|
|
".tar": true, ".gz": true, ".rar": true, ".7z": true,
|
|
".exe": true, ".dll": true, ".so": true, ".bin": true, ".dat": true,
|
|
".class": true, ".pyc": true,
|
|
".pdf": true, ".doc": true, ".docx": true, ".xls": true, ".xlsx": true,
|
|
".ppt": true, ".pptx": true,
|
|
}
|
|
|
|
symbolCleaner = regexp.MustCompile(`[\x00-\x08\x0B\x0C\x0E-\x1F]`)
|
|
multiNewlines = regexp.MustCompile(`\n{3,}`)
|
|
)
|
|
|
|
// SanitizeURL 清洗 URL 字符串
|
|
func SanitizeURL(raw string) string {
|
|
s := strings.TrimSpace(raw)
|
|
s = strings.Trim(s, "`\"")
|
|
return s
|
|
}
|
|
|
|
// CleanSymbols 清洗文本中的控制字符和多余空行
|
|
func CleanSymbols(text string) string {
|
|
text = symbolCleaner.ReplaceAllString(text, "")
|
|
text = strings.ReplaceAll(text, "\r\n", "\n")
|
|
text = strings.ReplaceAll(text, "\r", "\n")
|
|
text = multiNewlines.ReplaceAllString(text, "\n\n")
|
|
return strings.TrimSpace(text)
|
|
}
|
|
|
|
// IsBannedExtension 判断是否为禁止的文件扩展名
|
|
func IsBannedExtension(url string) bool {
|
|
ext := extractExtension(url)
|
|
return BannedExtensions[ext]
|
|
}
|
|
|
|
// IsZipExtension 判断是否为 zip 文件
|
|
func IsZipExtension(url string) bool {
|
|
ext := extractExtension(url)
|
|
return ext == ".zip"
|
|
}
|
|
|
|
// IsReadableContentType 判断是否为可读的文本类型
|
|
func IsReadableContentType(contentType string) bool {
|
|
if contentType == "" {
|
|
return false
|
|
}
|
|
|
|
ct := strings.ToLower(contentType)
|
|
for _, prefix := range AllowedMIMEPrefixes {
|
|
if strings.HasPrefix(ct, prefix) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// extractExtension 提取文件扩展名并清理查询参数
|
|
func extractExtension(url string) string {
|
|
ext := strings.ToLower(filepath.Ext(url))
|
|
if idx := strings.Index(ext, "?"); idx != -1 {
|
|
ext = ext[:idx]
|
|
}
|
|
return ext
|
|
}
|