From 280f75bfdb39b295a119962a10b96d18bf334abf Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Tue, 9 Apr 2024 14:17:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E8=AF=BB=E5=8F=96=20b?= =?UTF-8?q?lock=20=E6=96=87=E4=BB=B6=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/files/fileinfo.go | 24 +++++++++++++----------- backend/utils/files/utils.go | 4 ++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/backend/utils/files/fileinfo.go b/backend/utils/files/fileinfo.go index 94b1dbce3173..224e95296a7e 100644 --- a/backend/utils/files/fileinfo.go +++ b/backend/utils/files/fileinfo.go @@ -311,20 +311,22 @@ func (f *FileInfo) listChildren(option FileOption) error { } func (f *FileInfo) getContent() error { - if f.Size <= 10*1024*1024 { - afs := &afero.Afero{Fs: f.Fs} - cByte, err := afs.ReadFile(f.Path) - if err != nil { - return nil - } - if len(cByte) > 0 && DetectBinary(cByte) { - return buserr.New(constant.ErrFileCanNotRead) - } - f.Content = string(cByte) + if IsBlockDevice(f.FileMode) { + return buserr.New(constant.ErrFileCanNotRead) + } + if f.Size > 10*1024*1024 { + return buserr.New("ErrFileToLarge") + } + afs := &afero.Afero{Fs: f.Fs} + cByte, err := afs.ReadFile(f.Path) + if err != nil { return nil - } else { + } + if len(cByte) > 0 && DetectBinary(cByte) { return buserr.New(constant.ErrFileCanNotRead) } + f.Content = string(cByte) + return nil } func DetectBinary(buf []byte) bool { diff --git a/backend/utils/files/utils.go b/backend/utils/files/utils.go index c6c4aec53596..b77e2650ea57 100644 --- a/backend/utils/files/utils.go +++ b/backend/utils/files/utils.go @@ -16,6 +16,10 @@ func IsSymlink(mode os.FileMode) bool { return mode&os.ModeSymlink != 0 } +func IsBlockDevice(mode os.FileMode) bool { + return mode&os.ModeDevice != 0 && mode&os.ModeCharDevice == 0 +} + func GetMimeType(path string) string { file, err := os.Open(path) if err != nil {