Skip to content

Commit

Permalink
fix: 解决读取 block 文件报错的问题 (1Panel-dev#4439)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 authored Apr 9, 2024
1 parent 6b25842 commit d66717d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
24 changes: 13 additions & 11 deletions backend/utils/files/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions backend/utils/files/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d66717d

Please sign in to comment.