mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
feat: 文件搜索增加搜索子目录功能 (#819)
This commit is contained in:
parent
eba1e5495f
commit
ebe0f98209
@ -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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
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
|
||||
}
|
||||
return files, nil
|
||||
if err = cmd.Start(); err != nil {
|
||||
return
|
||||
}
|
||||
defer cmd.Wait()
|
||||
defer cmd.Process.Kill()
|
||||
|
||||
scanner := bufio.NewScanner(output)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
fmt.Println(line)
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -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 {
|
||||
|
@ -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
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
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…
x
Reference in New Issue
Block a user