Files
nms_cxy/features/file/model.go
2024-03-12 10:58:33 +08:00

86 lines
1.9 KiB
Go

package file
import (
"bufio"
"fmt"
"io/fs"
"os"
"os/exec"
"time"
"github.com/spf13/afero"
)
type FileOption struct {
Path string `json:"path"`
Search string `json:"search"`
ContainSub bool `json:"containSub"`
Expand bool `json:"expand"`
Dir bool `json:"dir"`
ShowHidden bool `json:"showHidden"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
type FileInfo struct {
Fs afero.Fs `json:"-"`
Path string `json:"path"`
Name string `json:"name"`
Extension string `json:"extension"`
Content string `json:"content"`
Size int64 `json:"size"`
IsDir bool `json:"isDir"`
IsSymlink bool `json:"isSymlink"`
IsHidden bool `json:"isHidden"`
LinkPath string `json:"linkPath"`
Type string `json:"type"`
Mode string `json:"mode"`
MimeType string `json:"mimeType"`
UpdateTime time.Time `json:"updateTime"`
ModTime time.Time `json:"modTime"`
FileMode os.FileMode `json:"-"`
Items []*FileInfo `json:"items"`
ItemTotal int `json:"itemTotal"`
}
func (f *FileInfo) search(search string, count int) (files []FileSearchInfo, total int, err error) {
cmd := exec.Command("find", f.Path, "-name", fmt.Sprintf("*%s*", search))
output, err := cmd.StdoutPipe()
if err != nil {
return
}
if err = cmd.Start(); err != nil {
return
}
defer func() {
_ = cmd.Wait()
_ = cmd.Process.Kill()
}()
scanner := bufio.NewScanner(output)
for scanner.Scan() {
line := scanner.Text()
info, err := os.Stat(line)
if err != nil {
continue
}
total++
if total > count {
continue
}
files = append(files, FileSearchInfo{
Path: line,
FileInfo: info,
})
}
if err = scanner.Err(); err != nil {
return
}
return
}
type FileSearchInfo struct {
Path string `json:"path"`
fs.FileInfo
}