1Panel/backend/utils/files/fileinfo.go

279 lines
6 KiB
Go
Raw Normal View History

2022-08-24 11:10:50 +08:00
package files
import (
"bufio"
2022-08-24 11:10:50 +08:00
"fmt"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
2023-02-06 16:58:45 +08:00
"io/fs"
2022-08-24 11:10:50 +08:00
"os"
"os/exec"
2022-08-24 11:10:50 +08:00
"path"
"path/filepath"
2023-02-06 16:58:45 +08:00
"strings"
"syscall"
2022-08-24 11:10:50 +08:00
"time"
"github.com/spf13/afero"
2022-08-24 11:10:50 +08:00
)
type FileInfo struct {
Fs afero.Fs `json:"-"`
Path string `json:"path"`
Name string `json:"name"`
User string `json:"user"`
Group string `json:"group"`
Extension string `json:"extension"`
Content string `json:"content"`
Size int64 `json:"size"`
IsDir bool `json:"isDir"`
IsSymlink bool `json:"isSymlink"`
2022-09-09 11:20:02 +08:00
IsHidden bool `json:"isHidden"`
LinkPath string `json:"linkPath"`
2022-08-24 11:10:50 +08:00
Type string `json:"type"`
Mode string `json:"mode"`
2022-08-30 17:59:59 +08:00
MimeType string `json:"mimeType"`
2022-08-24 11:10:50 +08:00
UpdateTime time.Time `json:"updateTime"`
ModTime time.Time `json:"modTime"`
FileMode os.FileMode `json:"-"`
Items []*FileInfo `json:"items"`
2022-09-15 10:44:43 +08:00
ItemTotal int `json:"itemTotal"`
2022-08-24 11:10:50 +08:00
}
type FileOption struct {
2022-09-09 11:20:02 +08:00
Path string `json:"path"`
Search string `json:"search"`
2023-02-06 16:58:45 +08:00
ContainSub bool `json:"containSub"`
2022-09-09 11:20:02 +08:00
Expand bool `json:"expand"`
Dir bool `json:"dir"`
ShowHidden bool `json:"showHidden"`
2022-09-15 10:44:43 +08:00
Page int `json:"page"`
PageSize int `json:"pageSize"`
2022-08-24 11:10:50 +08:00
}
2023-02-06 16:58:45 +08:00
type FileSearchInfo struct {
Path string `json:"path"`
fs.FileInfo
}
2022-08-24 11:10:50 +08:00
func NewFileInfo(op FileOption) (*FileInfo, error) {
var appFs = afero.NewOsFs()
info, err := appFs.Stat(op.Path)
if err != nil {
return nil, err
}
file := &FileInfo{
Fs: appFs,
Path: op.Path,
Name: info.Name(),
IsDir: info.IsDir(),
FileMode: info.Mode(),
ModTime: info.ModTime(),
Size: info.Size(),
IsSymlink: IsSymlink(info.Mode()),
Extension: filepath.Ext(info.Name()),
2022-09-09 11:20:02 +08:00
IsHidden: IsHidden(op.Path),
2022-08-24 11:10:50 +08:00
Mode: fmt.Sprintf("%04o", info.Mode().Perm()),
User: GetUsername(info.Sys().(*syscall.Stat_t).Uid),
Group: GetGroup(info.Sys().(*syscall.Stat_t).Gid),
2022-08-30 17:59:59 +08:00
MimeType: GetMimeType(op.Path),
2022-08-24 11:10:50 +08:00
}
if file.IsSymlink {
file.LinkPath = GetSymlink(op.Path)
}
2022-08-24 11:10:50 +08:00
if op.Expand {
if file.IsDir {
2023-02-06 16:58:45 +08:00
if err := file.listChildren(op.Dir, op.ShowHidden, op.ContainSub, op.Search, op.Page, op.PageSize); err != nil {
2022-08-24 11:10:50 +08:00
return nil, err
}
return file, nil
} else {
if err := file.getContent(); err != nil {
return nil, err
}
}
}
return file, nil
}
func (f *FileInfo) search(search string, count int) (files []FileSearchInfo, total int, err error) {
cmd := exec.Command("find", f.Path, "-name", search)
output, err := cmd.StdoutPipe()
if err != nil {
return
}
if err = cmd.Start(); err != nil {
return
}
defer cmd.Wait()
defer cmd.Process.Kill()
scanner := bufio.NewScanner(output)
for scanner.Scan() {
line := scanner.Text()
info, err := os.Stat(line)
if err != nil {
continue
2023-02-06 16:58:45 +08:00
}
total++
if total > count {
continue
}
files = append(files, FileSearchInfo{
Path: line,
FileInfo: info,
})
2023-02-06 16:58:45 +08:00
}
if err = scanner.Err(); err != nil {
return
}
return
2023-02-06 16:58:45 +08:00
}
func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, page, pageSize int) error {
2022-08-24 11:10:50 +08:00
afs := &afero.Afero{Fs: f.Fs}
2023-02-06 16:58:45 +08:00
var (
files []FileSearchInfo
err error
total int
2023-02-06 16:58:45 +08:00
)
if search != "" && containSub {
files, total, err = f.search(search, page*pageSize)
2023-02-06 16:58:45 +08:00
if err != nil {
return err
}
} else {
dirFiles, err := afs.ReadDir(f.Path)
if err != nil {
return err
}
for _, file := range dirFiles {
files = append(files, FileSearchInfo{
Path: f.Path,
FileInfo: file,
})
}
2022-08-24 11:10:50 +08:00
}
2023-02-06 16:58:45 +08:00
2022-08-24 11:10:50 +08:00
var items []*FileInfo
2022-09-06 10:35:35 +08:00
for _, df := range files {
if dir && !df.IsDir() {
continue
}
2022-08-24 11:10:50 +08:00
name := df.Name()
2023-02-06 16:58:45 +08:00
fPath := path.Join(df.Path, df.Name())
if search != "" {
if containSub {
fPath = df.Path
name = strings.TrimPrefix(strings.TrimPrefix(fPath, f.Path), "/")
} else {
lowerName := strings.ToLower(name)
lowerSearch := strings.ToLower(search)
if !strings.Contains(lowerName, lowerSearch) {
continue
}
}
}
2022-09-09 11:20:02 +08:00
if !showHidden && IsHidden(name) {
continue
}
f.ItemTotal++
2022-08-24 11:10:50 +08:00
isSymlink, isInvalidLink := false, false
if IsSymlink(df.Mode()) {
isSymlink = true
info, err := f.Fs.Stat(fPath)
if err == nil {
2023-02-06 16:58:45 +08:00
df.FileInfo = info
2022-08-24 11:10:50 +08:00
} else {
isInvalidLink = true
}
}
file := &FileInfo{
Fs: f.Fs,
Name: name,
Size: df.Size(),
ModTime: df.ModTime(),
FileMode: df.Mode(),
IsDir: df.IsDir(),
IsSymlink: isSymlink,
2022-09-09 11:20:02 +08:00
IsHidden: IsHidden(fPath),
2022-08-24 11:10:50 +08:00
Extension: filepath.Ext(name),
Path: fPath,
Mode: fmt.Sprintf("%04o", df.Mode().Perm()),
User: GetUsername(df.Sys().(*syscall.Stat_t).Uid),
Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid),
2022-08-24 11:10:50 +08:00
}
2023-02-06 16:58:45 +08:00
if isSymlink {
file.LinkPath = GetSymlink(fPath)
}
if df.Size() > 0 {
file.MimeType = GetMimeType(fPath)
}
2022-08-24 11:10:50 +08:00
if isInvalidLink {
file.Type = "invalid_link"
}
items = append(items, file)
}
if containSub {
f.ItemTotal = total
}
2022-09-15 10:44:43 +08:00
start := (page - 1) * pageSize
end := pageSize + start
var result []*FileInfo
if start < 0 || start > f.ItemTotal || end < 0 || start > end {
2022-09-15 10:44:43 +08:00
result = items
} else {
if end > f.ItemTotal {
result = items[start:]
} else {
result = items[start:end]
}
2022-09-15 10:44:43 +08:00
}
f.Items = result
2022-08-24 11:10:50 +08:00
return nil
}
func (f *FileInfo) getContent() error {
2022-09-01 19:02:33 +08:00
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)
}
2022-09-01 19:02:33 +08:00
f.Content = string(cByte)
return nil
} else {
return buserr.New(constant.ErrFileCanNotRead)
}
}
func detectBinary(buf []byte) bool {
2022-12-04 21:18:24 +08:00
whiteByte := 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
}
}
2022-12-04 21:18:24 +08:00
return whiteByte < 1
}
func min(x, y int) int {
if x < y {
return x
2022-09-01 19:02:33 +08:00
}
return y
2022-08-24 11:10:50 +08:00
}