mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-02-24 14:54:43 +08:00
fix: 限制非文本文件读取,限制大文件读取
This commit is contained in:
parent
2add7312e6
commit
05ef2373ef
5 changed files with 49 additions and 9 deletions
|
@ -45,7 +45,9 @@ var (
|
|||
|
||||
// app
|
||||
var (
|
||||
ErrPortInUsed = "ErrPortInUsed"
|
||||
ErrAppLimit = "ErrAppLimit"
|
||||
ErrAppRequired = "ErrAppRequired"
|
||||
ErrPortInUsed = "ErrPortInUsed"
|
||||
ErrAppLimit = "ErrAppLimit"
|
||||
ErrAppRequired = "ErrAppRequired"
|
||||
ErrFileCanNotRead = "ErrFileCanNotRead"
|
||||
ErrFileToLarge = "ErrFileToLarge"
|
||||
)
|
||||
|
|
|
@ -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"
|
||||
ErrAppRequired: "{{ .detail }} app is required"
|
||||
|
||||
|
||||
#file
|
||||
ErrFileCanNotRead: "File can not read"
|
||||
ErrFileToLarge: "file is too large"
|
|
@ -20,4 +20,9 @@ ErrNameIsExist: "名称已存在"
|
|||
#app
|
||||
ErrPortInUsed: "{{ .detail }} 端口已被占用!"
|
||||
ErrAppLimit: "应用超出安装数量限制"
|
||||
ErrAppRequired: "请先安装 {{ .detail }} 应用"
|
||||
ErrAppRequired: "请先安装 {{ .detail }} 应用"
|
||||
|
||||
|
||||
#file
|
||||
ErrFileCanNotRead: "文件不可读"
|
||||
ErrFileToLarge: "文件超过10M,无法打开"
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -96,7 +96,7 @@
|
|||
<el-link :underline="false" @click="openMode(row)">{{ row.mode }}</el-link>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column :label="$t('file.user')" prop="user"></el-table-column>
|
||||
<el-table-column :label="$t('file.user')" prop="user" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column :label="$t('file.group')" prop="group"></el-table-column>
|
||||
<el-table-column :label="$t('file.size')" prop="size">
|
||||
<template #default="{ row }">
|
||||
|
@ -425,8 +425,8 @@ const openCodeEditor = (row: File.File) => {
|
|||
codeReq.expand = true;
|
||||
GetFileContent(codeReq).then((res) => {
|
||||
editorPage.content = res.data.content;
|
||||
editorPage.open = true;
|
||||
});
|
||||
editorPage.open = true;
|
||||
};
|
||||
|
||||
const closeCodeEditor = () => {
|
||||
|
|
Loading…
Reference in a new issue