Browse Source

fix: 解决读取 block 文件报错的问题 (#4439)

pull/4440/head
zhengkunwang 8 months ago committed by GitHub
parent
commit
db4973cfde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      backend/utils/files/fileinfo.go
  2. 4
      backend/utils/files/utils.go

24
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 {

4
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 {

Loading…
Cancel
Save