diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 14f361a85..56bab0040 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -45,7 +45,9 @@ var ( // app var ( - ErrPortInUsed = "ErrPortInUsed" - ErrAppLimit = "ErrAppLimit" - ErrAppRequired = "ErrAppRequired" + ErrPortInUsed = "ErrPortInUsed" + ErrAppLimit = "ErrAppLimit" + ErrAppRequired = "ErrAppRequired" + ErrFileCanNotRead = "ErrFileCanNotRead" + ErrFileToLarge = "ErrFileToLarge" ) diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 3588aa761..cb6ff858a 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -20,4 +20,9 @@ ErrNameIsExist: "Name is already exist" #app ErrPortInUsed: "{{ .detail }} port already in use" ErrAppLimit: "App exceeds install limit" -ErrAppRequired: "{{ .detail }} app is required" \ No newline at end of file +ErrAppRequired: "{{ .detail }} app is required" + + +#file +ErrFileCanNotRead: "File can not read" +ErrFileToLarge: "file is too large" \ No newline at end of file diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index 539a4fcf2..29be89cf2 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -20,4 +20,9 @@ ErrNameIsExist: "名称已存在" #app ErrPortInUsed: "{{ .detail }} 端口已被占用!" ErrAppLimit: "应用超出安装数量限制" -ErrAppRequired: "请先安装 {{ .detail }} 应用" \ No newline at end of file +ErrAppRequired: "请先安装 {{ .detail }} 应用" + + +#file +ErrFileCanNotRead: "文件不可读" +ErrFileToLarge: "文件超过10M,无法打开" \ No newline at end of file diff --git a/backend/utils/files/fileinfo.go b/backend/utils/files/fileinfo.go index 05f4e079f..7938622c4 100644 --- a/backend/utils/files/fileinfo.go +++ b/backend/utils/files/fileinfo.go @@ -2,13 +2,14 @@ package files import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/buserr" + "github.com/1Panel-dev/1Panel/backend/constant" "os" "path" "path/filepath" "syscall" "time" - "github.com/pkg/errors" "github.com/spf13/afero" ) @@ -170,9 +171,36 @@ func (f *FileInfo) getContent() error { if err != nil { return nil } + if detectBinary(cByte) { + return buserr.New(constant.ErrFileCanNotRead, "", nil) + } f.Content = string(cByte) return nil } else { - return errors.New("file is too large!") + return buserr.New(constant.ErrFileToLarge, "", nil) } } + +func detectBinary(buf []byte) bool { + var whiteByte int = 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 + } + } + + if whiteByte >= 1 { + return false + } + return true +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} diff --git a/frontend/src/views/host/file-management/index.vue b/frontend/src/views/host/file-management/index.vue index b9bb2b807..79278099f 100644 --- a/frontend/src/views/host/file-management/index.vue +++ b/frontend/src/views/host/file-management/index.vue @@ -96,7 +96,7 @@ {{ row.mode }} - +