Browse Source

feat: 文件搜索增加搜索子目录功能 (#819)

pull/821/head
zhengkunwang223 2 years ago committed by GitHub
parent
commit
ebe0f98209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 72
      backend/utils/files/fileinfo.go
  2. 14
      backend/utils/files/utils.go
  3. 12
      frontend/src/views/host/file-management/index.vue
  4. 1
      go.mod
  5. 2
      go.sum

72
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

14
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 {

12
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')"
>
<!-- <template #prepend>
<el-checkbox :disabled="req.path == '/'" v-model="req.containSub">
<template #prepend>
<el-checkbox v-model="req.containSub">
{{ $t('file.sub') }}
</el-checkbox>
</template> -->
</template>
<template #append>
<el-button icon="Search" @click="search" />
</template>
</el-input>
</div>
</template>

1
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

2
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=

Loading…
Cancel
Save