From f4495572813e1552a02b6c7dc18899dc337dccf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= <81747598+lan-yonghui@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:41:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=A4=E6=96=AD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E4=B8=BA=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E5=BF=BD=E7=95=A5=E6=96=87=E6=9C=AC=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=96=87=E4=BB=B6=20(#5730)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #5722 --- backend/utils/files/fileinfo.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/backend/utils/files/fileinfo.go b/backend/utils/files/fileinfo.go index ebe027d63..3d3e7be81 100644 --- a/backend/utils/files/fileinfo.go +++ b/backend/utils/files/fileinfo.go @@ -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,17 +336,21 @@ func (f *FileInfo) getContent() error { } func DetectBinary(buf []byte) bool { - whiteByte := 0 - n := min(1024, len(buf)) - for i := 0; i < n; i++ { - if (buf[i] >= 0x20) || buf[i] == 9 || buf[i] == 10 || buf[i] == 13 { - whiteByte++ - } else if buf[i] <= 6 || (buf[i] >= 14 && buf[i] <= 31) { - return true + mimeType := http.DetectContentType(buf) + if !strings.HasPrefix(mimeType, "text/") { + whiteByte := 0 + n := min(1024, len(buf)) + for i := 0; i < n; i++ { + if (buf[i] >= 0x20) || buf[i] == 9 || buf[i] == 10 || buf[i] == 13 { + whiteByte++ + } else if buf[i] <= 6 || (buf[i] >= 14 && buf[i] <= 31) { + return true + } } + return whiteByte < 1 } + return false - return whiteByte < 1 } func min(x, y int) int {