1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 14:08:06 +08:00

fix: 判断文件是否为二进制文件,忽略文本类型文件 (#5730)

Fixes #5722
This commit is contained in:
2024-07-09 16:41:24 +08:00 committed by GitHub
parent d01e34aa40
commit f449557281
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"io/fs"
"net/http"
"os"
"os/exec"
"path"
@ -335,6 +336,8 @@ func (f *FileInfo) getContent() error {
}
func DetectBinary(buf []byte) bool {
mimeType := http.DetectContentType(buf)
if !strings.HasPrefix(mimeType, "text/") {
whiteByte := 0
n := min(1024, len(buf))
for i := 0; i < n; i++ {
@ -344,8 +347,10 @@ func DetectBinary(buf []byte) bool {
return true
}
}
return whiteByte < 1
}
return false
}
func min(x, y int) int {