From ebe0f98209b2f2e3fcff1b357e49a602ef97e797 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 27 Apr 2023 21:31:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E4=BB=B6=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=90=9C=E7=B4=A2=E5=AD=90=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20(#819)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/files/fileinfo.go | 72 ++++++++++--------- backend/utils/files/utils.go | 14 +++- .../src/views/host/file-management/index.vue | 12 ++-- go.mod | 1 - go.sum | 2 - 5 files changed, 56 insertions(+), 45 deletions(-) diff --git a/backend/utils/files/fileinfo.go b/backend/utils/files/fileinfo.go index 668b5fcd2..5d108b1bd 100644 --- a/backend/utils/files/fileinfo.go +++ b/backend/utils/files/fileinfo.go @@ -1,11 +1,13 @@ package files import ( + "bufio" "fmt" "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/constant" "io/fs" "os" + "os/exec" "path" "path/filepath" "strings" @@ -96,35 +98,39 @@ func NewFileInfo(op FileOption) (*FileInfo, error) { return file, nil } -func (f *FileInfo) search(dir, showHidden bool, af afero.Afero, search string, count int) ([]FileSearchInfo, error) { - var files []FileSearchInfo - if err := afero.Walk(af, f.Path, func(path string, info fs.FileInfo, err error) error { - if info != nil { - - if dir && !info.IsDir() { - return nil - } - if !showHidden && IsHidden(info.Name()) { - return 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() - lowerName := strings.ToLower(info.Name()) - lowerSearch := strings.ToLower(search) - if strings.Contains(lowerName, lowerSearch) { - files = append(files, FileSearchInfo{ - Path: path, - FileInfo: info, - }) - if len(files) > count { - return nil - } - } + scanner := bufio.NewScanner(output) + for scanner.Scan() { + line := scanner.Text() + fmt.Println(line) + info, err := os.Stat(line) + if err != nil { + continue } - return nil - }); err != nil { - return nil, err + total++ + if total > count { + continue + } + files = append(files, FileSearchInfo{ + Path: line, + FileInfo: info, + }) } - return files, nil + if err = scanner.Err(); err != nil { + return + } + return } func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, page, pageSize int) error { @@ -132,10 +138,11 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, var ( files []FileSearchInfo err error + total int ) if search != "" && containSub { - files, err = f.search(dir, showHidden, *afs, search, page*pageSize) + files, total, err = f.search(search, page*pageSize) if err != nil { return err } @@ -159,10 +166,8 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, if dir && !df.IsDir() { continue } - name := df.Name() fPath := path.Join(df.Path, df.Name()) - if search != "" { if containSub { fPath = df.Path @@ -175,12 +180,10 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, } } } - if !showHidden && IsHidden(name) { continue } f.ItemTotal++ - isSymlink, isInvalidLink := false, false if IsSymlink(df.Mode()) { isSymlink = true @@ -204,7 +207,6 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, Extension: filepath.Ext(name), Path: fPath, Mode: fmt.Sprintf("%04o", df.Mode().Perm()), - MimeType: GetMimeType(fPath), User: GetUsername(df.Sys().(*syscall.Stat_t).Uid), Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid), } @@ -212,13 +214,15 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string, if isSymlink { file.LinkPath = GetSymlink(fPath) } - + if df.Size() > 0 { + file.MimeType = GetMimeType(fPath) + } if isInvalidLink { file.Type = "invalid_link" } items = append(items, file) } - + f.ItemTotal = total start := (page - 1) * pageSize end := pageSize + start var result []*FileInfo diff --git a/backend/utils/files/utils.go b/backend/utils/files/utils.go index c75ba980c..6dc4d84de 100644 --- a/backend/utils/files/utils.go +++ b/backend/utils/files/utils.go @@ -1,8 +1,8 @@ package files import ( - "github.com/gabriel-vasile/mimetype" "github.com/spf13/afero" + "net/http" "os" "os/user" "path/filepath" @@ -15,11 +15,19 @@ func IsSymlink(mode os.FileMode) bool { } func GetMimeType(path string) string { - mime, err := mimetype.DetectFile(path) + file, err := os.Open(path) if err != nil { return "" } - return mime.String() + defer file.Close() + + buffer := make([]byte, 512) + _, err = file.Read(buffer) + if err != nil { + return "" + } + mimeType := http.DetectContentType(buffer) + return mimeType } func GetSymlink(path string) string { diff --git a/frontend/src/views/host/file-management/index.vue b/frontend/src/views/host/file-management/index.vue index 0ddb5f248..0a9bab1ec 100644 --- a/frontend/src/views/host/file-management/index.vue +++ b/frontend/src/views/host/file-management/index.vue @@ -89,15 +89,17 @@ v-model="req.search" clearable @clear="search()" - suffix-icon="Search" - @blur="search()" + @keydown.enter="search()" :placeholder="$t('file.search')" > - + + diff --git a/go.mod b/go.mod index d43bbef10..fe6f9b97a 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/docker/go-connections v0.4.0 github.com/fsnotify/fsnotify v1.6.0 github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 - github.com/gabriel-vasile/mimetype v1.4.1 github.com/gin-contrib/gzip v0.0.6 github.com/gin-contrib/i18n v0.0.1 github.com/gin-gonic/gin v1.8.1 diff --git a/go.sum b/go.sum index 54175d1df..31b3da637 100644 --- a/go.sum +++ b/go.sum @@ -260,8 +260,6 @@ github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 h1:6VSn3hB5U5GeA6kQ github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6/go.mod h1:YxOVT5+yHzKvwhsiSIWmbAYM3Dr9AEEbER2dVayfBkg= github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo= github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0= -github.com/gabriel-vasile/mimetype v1.4.1 h1:TRWk7se+TOjCYgRth7+1/OYLNiRNIotknkFtf/dnN7Q= -github.com/gabriel-vasile/mimetype v1.4.1/go.mod h1:05Vi0w3Y9c/lNvJOdmIwvrrAhX3rYhfQQCaf9VJcv7M= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=