mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 16:29:17 +08:00
feat: 文件搜索增加搜索子目录功能 (#819)
This commit is contained in:
parent
eba1e5495f
commit
ebe0f98209
@ -1,11 +1,13 @@
|
|||||||
package files
|
package files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/1Panel-dev/1Panel/backend/buserr"
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
||||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -96,35 +98,39 @@ func NewFileInfo(op FileOption) (*FileInfo, error) {
|
|||||||
return file, nil
|
return file, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileInfo) search(dir, showHidden bool, af afero.Afero, search string, count int) ([]FileSearchInfo, error) {
|
func (f *FileInfo) search(search string, count int) (files []FileSearchInfo, total int, err error) {
|
||||||
var files []FileSearchInfo
|
cmd := exec.Command("find", f.Path, "-name", search)
|
||||||
if err := afero.Walk(af, f.Path, func(path string, info fs.FileInfo, err error) error {
|
output, err := cmd.StdoutPipe()
|
||||||
if info != nil {
|
if err != nil {
|
||||||
|
return
|
||||||
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
|
|
||||||
}
|
}
|
||||||
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 {
|
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 (
|
var (
|
||||||
files []FileSearchInfo
|
files []FileSearchInfo
|
||||||
err error
|
err error
|
||||||
|
total int
|
||||||
)
|
)
|
||||||
|
|
||||||
if search != "" && containSub {
|
if search != "" && containSub {
|
||||||
files, err = f.search(dir, showHidden, *afs, search, page*pageSize)
|
files, total, err = f.search(search, page*pageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -159,10 +166,8 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
|
|||||||
if dir && !df.IsDir() {
|
if dir && !df.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
name := df.Name()
|
name := df.Name()
|
||||||
fPath := path.Join(df.Path, df.Name())
|
fPath := path.Join(df.Path, df.Name())
|
||||||
|
|
||||||
if search != "" {
|
if search != "" {
|
||||||
if containSub {
|
if containSub {
|
||||||
fPath = df.Path
|
fPath = df.Path
|
||||||
@ -175,12 +180,10 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !showHidden && IsHidden(name) {
|
if !showHidden && IsHidden(name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
f.ItemTotal++
|
f.ItemTotal++
|
||||||
|
|
||||||
isSymlink, isInvalidLink := false, false
|
isSymlink, isInvalidLink := false, false
|
||||||
if IsSymlink(df.Mode()) {
|
if IsSymlink(df.Mode()) {
|
||||||
isSymlink = true
|
isSymlink = true
|
||||||
@ -204,7 +207,6 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
|
|||||||
Extension: filepath.Ext(name),
|
Extension: filepath.Ext(name),
|
||||||
Path: fPath,
|
Path: fPath,
|
||||||
Mode: fmt.Sprintf("%04o", df.Mode().Perm()),
|
Mode: fmt.Sprintf("%04o", df.Mode().Perm()),
|
||||||
MimeType: GetMimeType(fPath),
|
|
||||||
User: GetUsername(df.Sys().(*syscall.Stat_t).Uid),
|
User: GetUsername(df.Sys().(*syscall.Stat_t).Uid),
|
||||||
Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid),
|
Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid),
|
||||||
}
|
}
|
||||||
@ -212,13 +214,15 @@ func (f *FileInfo) listChildren(dir, showHidden, containSub bool, search string,
|
|||||||
if isSymlink {
|
if isSymlink {
|
||||||
file.LinkPath = GetSymlink(fPath)
|
file.LinkPath = GetSymlink(fPath)
|
||||||
}
|
}
|
||||||
|
if df.Size() > 0 {
|
||||||
|
file.MimeType = GetMimeType(fPath)
|
||||||
|
}
|
||||||
if isInvalidLink {
|
if isInvalidLink {
|
||||||
file.Type = "invalid_link"
|
file.Type = "invalid_link"
|
||||||
}
|
}
|
||||||
items = append(items, file)
|
items = append(items, file)
|
||||||
}
|
}
|
||||||
|
f.ItemTotal = total
|
||||||
start := (page - 1) * pageSize
|
start := (page - 1) * pageSize
|
||||||
end := pageSize + start
|
end := pageSize + start
|
||||||
var result []*FileInfo
|
var result []*FileInfo
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package files
|
package files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gabriel-vasile/mimetype"
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -15,11 +15,19 @@ func IsSymlink(mode os.FileMode) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetMimeType(path string) string {
|
func GetMimeType(path string) string {
|
||||||
mime, err := mimetype.DetectFile(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
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 {
|
func GetSymlink(path string) string {
|
||||||
|
@ -89,15 +89,17 @@
|
|||||||
v-model="req.search"
|
v-model="req.search"
|
||||||
clearable
|
clearable
|
||||||
@clear="search()"
|
@clear="search()"
|
||||||
suffix-icon="Search"
|
@keydown.enter="search()"
|
||||||
@blur="search()"
|
|
||||||
:placeholder="$t('file.search')"
|
:placeholder="$t('file.search')"
|
||||||
>
|
>
|
||||||
<!-- <template #prepend>
|
<template #prepend>
|
||||||
<el-checkbox :disabled="req.path == '/'" v-model="req.containSub">
|
<el-checkbox v-model="req.containSub">
|
||||||
{{ $t('file.sub') }}
|
{{ $t('file.sub') }}
|
||||||
</el-checkbox>
|
</el-checkbox>
|
||||||
</template> -->
|
</template>
|
||||||
|
<template #append>
|
||||||
|
<el-button icon="Search" @click="search" />
|
||||||
|
</template>
|
||||||
</el-input>
|
</el-input>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
1
go.mod
1
go.mod
@ -14,7 +14,6 @@ require (
|
|||||||
github.com/docker/go-connections v0.4.0
|
github.com/docker/go-connections v0.4.0
|
||||||
github.com/fsnotify/fsnotify v1.6.0
|
github.com/fsnotify/fsnotify v1.6.0
|
||||||
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
|
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/gzip v0.0.6
|
||||||
github.com/gin-contrib/i18n v0.0.1
|
github.com/gin-contrib/i18n v0.0.1
|
||||||
github.com/gin-gonic/gin v1.8.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/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 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo=
|
||||||
github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
|
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/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 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
|
||||||
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
|
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user