feat(file): add context support for file operations and enhance file listing (#14459)

This commit is contained in:
蘭
2026-11-18 09:54:17 +08:00
committed by GitHub
parent c77675a220
commit 4909c71df0
10 changed files with 585 additions and 223 deletions
+1 -1
View File
@@ -123,7 +123,7 @@ func (b *BaseApi) ListFiles(c *gin.Context) {
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
fileList, err := fileService.GetFileList(req)
fileList, err := fileService.GetFileListWithContext(c.Request.Context(), req)
if err != nil {
helper.InternalServer(c, err)
return
+40
View File
@@ -1,6 +1,8 @@
package repo
import (
"context"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/global"
"gorm.io/gorm"
@@ -14,6 +16,7 @@ type IFavoriteRepo interface {
Delete(opts ...DBOption) error
GetFirst(opts ...DBOption) (model.Favorite, error)
All() ([]model.Favorite, error)
ListByPaths(ctx context.Context, paths []string) ([]model.Favorite, error)
WithByPath(path string) DBOption
}
@@ -64,3 +67,40 @@ func (f *FavoriteRepo) All() ([]model.Favorite, error) {
}
return favorites, nil
}
const filePathQueryBatchSize = 900
func uniqueFilePaths(paths []string) []string {
unique := make([]string, 0, len(paths))
seen := make(map[string]struct{}, len(paths))
for _, path := range paths {
if _, ok := seen[path]; ok {
continue
}
seen[path] = struct{}{}
unique = append(unique, path)
}
return unique
}
func (f *FavoriteRepo) ListByPaths(ctx context.Context, paths []string) ([]model.Favorite, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
paths = uniqueFilePaths(paths)
favorites := make([]model.Favorite, 0, len(paths))
for start := 0; start < len(paths); start += filePathQueryBatchSize {
if err := ctx.Err(); err != nil {
return nil, err
}
end := min(start+filePathQueryBatchSize, len(paths))
db := getTx(ctx).WithContext(ctx)
firstIDs := db.Model(&model.Favorite{}).Select("MIN(id)").Where("path IN ?", paths[start:end]).Group("path")
var batch []model.Favorite
if err := db.Where("id IN (?)", firstIDs).Order("id ASC").Find(&batch).Error; err != nil {
return nil, err
}
favorites = append(favorites, batch...)
}
return favorites, nil
}
+23
View File
@@ -1,6 +1,8 @@
package repo
import (
"context"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/global"
"gorm.io/gorm"
@@ -15,6 +17,7 @@ type IFileShareRepo interface {
Delete(opts ...DBOption) error
GetFirst(opts ...DBOption) (model.FileShare, error)
All() ([]model.FileShare, error)
ListByPaths(ctx context.Context, paths []string) ([]model.FileShare, error)
WithByPath(path string) DBOption
WithByCode(code string) DBOption
}
@@ -75,3 +78,23 @@ func (r *FileShareRepo) All() ([]model.FileShare, error) {
}
return items, nil
}
func (r *FileShareRepo) ListByPaths(ctx context.Context, paths []string) ([]model.FileShare, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
paths = uniqueFilePaths(paths)
items := make([]model.FileShare, 0, len(paths))
for start := 0; start < len(paths); start += filePathQueryBatchSize {
if err := ctx.Err(); err != nil {
return nil, err
}
end := min(start+filePathQueryBatchSize, len(paths))
var batch []model.FileShare
if err := getTx(ctx).WithContext(ctx).Where("path IN ?", paths[start:end]).Order("path ASC").Find(&batch).Error; err != nil {
return nil, err
}
items = append(items, batch...)
}
return items, nil
}
+42 -5
View File
@@ -54,8 +54,11 @@ const fileHistorySnapshotMaxSize = 10 * 1024 * 1024
var fileTransferLocks = newFileTransferLocks()
var fileListSlots = make(chan struct{}, 2)
type IFileService interface {
GetFileList(op request.FileOption) (response.FileInfo, error)
GetFileListWithContext(ctx context.Context, op request.FileOption) (response.FileInfo, error)
SearchUploadWithPage(req request.SearchUploadWithPage) (int64, interface{}, error)
GetFileTree(op request.FileOption) ([]response.FileTree, error)
Create(op request.FileCreate) error
@@ -100,19 +103,53 @@ func NewIFileService() IFileService {
}
func (f *FileService) GetFileList(op request.FileOption) (response.FileInfo, error) {
return f.GetFileListWithContext(context.Background(), op)
}
func acquireFileListSlot(ctx context.Context) (func(), error) {
if err := ctx.Err(); err != nil {
return nil, err
}
select {
case fileListSlots <- struct{}{}:
if err := ctx.Err(); err != nil {
<-fileListSlots
return nil, err
}
return func() { <-fileListSlots }, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (f *FileService) GetFileListWithContext(ctx context.Context, op request.FileOption) (response.FileInfo, error) {
var fileInfo response.FileInfo
release, err := acquireFileListSlot(ctx)
if err != nil {
return fileInfo, err
}
defer release()
data, err := os.Stat(op.Path)
if err != nil && os.IsNotExist(err) {
return fileInfo, nil
if err != nil {
if os.IsNotExist(err) {
return fileInfo, nil
}
return fileInfo, err
}
if !data.IsDir() {
op.FileOption.Path = filepath.Dir(op.FileOption.Path)
}
info, err := files.NewFileInfo(op.FileOption)
info, err := files.NewFileInfoWithContext(ctx, op.FileOption)
if err != nil {
return fileInfo, err
}
shareMap, err := NewIFileShareService().SharePathCodeMap()
paths := make([]string, 0, len(info.Items)+1)
paths = append(paths, info.Path)
for _, item := range info.Items {
paths = append(paths, item.Path)
}
shareMap, err := NewIFileShareService().SharePathCodeMapForPaths(ctx, paths)
if err != nil {
return fileInfo, err
}
@@ -841,7 +878,7 @@ func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo,
}
info, err := files.NewFileInfo(files.FileOption{
Path: op.Path,
Expand: true,
Expand: !op.IsDetail,
IsDetail: op.IsDetail,
})
if err != nil {
+24
View File
@@ -1,6 +1,7 @@
package service
import (
"context"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
@@ -43,6 +44,7 @@ type IFileShareService interface {
GetPublicByCode(code string) (*response.FileSharePublicInfo, error)
DeleteByPath(path string) error
SharePathCodeMap() (map[string]string, error)
SharePathCodeMapForPaths(ctx context.Context, paths []string) (map[string]string, error)
Check(code, password string) error
PrepareDownload(code, password string) (filePath, fileName string, err error)
}
@@ -324,6 +326,28 @@ func (s *FileShareService) SharePathCodeMap() (map[string]string, error) {
return result, nil
}
func (s *FileShareService) SharePathCodeMapForPaths(ctx context.Context, paths []string) (map[string]string, error) {
items, err := fileShareRepo.ListByPaths(ctx, paths)
if err != nil {
return nil, err
}
result := make(map[string]string, len(items))
now := time.Now().Unix()
for _, item := range items {
if err := ctx.Err(); err != nil {
return nil, err
}
if item.ExpiresUnix > 0 && now > item.ExpiresUnix {
continue
}
if _, err := os.Stat(item.Path); err != nil {
continue
}
result[item.Path] = item.Token
}
return result, nil
}
func (s *FileShareService) Check(code, password string) error {
_, err := s.check(code, password)
return err
+265 -166
View File
@@ -2,7 +2,9 @@ package files
import (
"bufio"
"context"
"fmt"
"io"
"io/fs"
"net/http"
"os"
@@ -141,6 +143,13 @@ type FileSearchInfo struct {
}
func NewFileInfo(op FileOption) (*FileInfo, error) {
return NewFileInfoWithContext(context.Background(), op)
}
func NewFileInfoWithContext(ctx context.Context, op FileOption) (*FileInfo, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
var appFs = afero.NewOsFs()
info, err := appFs.Stat(op.Path)
@@ -167,15 +176,12 @@ func NewFileInfo(op FileOption) (*FileInfo, error) {
Uid: strconv.FormatUint(uint64(info.Sys().(*syscall.Stat_t).Uid), 10),
Gid: strconv.FormatUint(uint64(info.Sys().(*syscall.Stat_t).Gid), 10),
Group: GetGroup(info.Sys().(*syscall.Stat_t).Gid),
MimeType: GetMimeType(op.Path),
IsDetail: op.IsDetail,
}
favoriteRepo := repo.NewIFavoriteRepo()
favorite, _ := favoriteRepo.GetFirst(favoriteRepo.WithByPath(op.Path))
if favorite.ID > 0 {
file.FavoriteID = favorite.ID
if info.Mode().IsRegular() {
file.MimeType = GetMimeType(op.Path)
}
file.IsAppendOnly, file.IsImmutable = getFileAttributes(op.Path)
file.IsAppendOnly, file.IsImmutable = getFileAttributes(ctx, op.Path)
if file.IsSymlink {
linkPath := GetSymlink(op.Path)
@@ -200,15 +206,20 @@ func NewFileInfo(op FileOption) (*FileInfo, error) {
file.Extension = filepath.Ext(file.LinkPath)
}
if op.Expand {
if err := handleExpansion(file, op); err != nil {
if err := handleExpansion(ctx, file, op); err != nil {
return nil, err
}
}
if !op.Expand || !file.IsDir {
if err := file.loadFavorites(ctx); err != nil {
return nil, err
}
}
return file, nil
}
func getFileAttributes(filePath string) (bool, bool) {
output, err := exec.Command("lsattr", "-d", "--", filePath).Output()
func getFileAttributes(ctx context.Context, filePath string) (bool, bool) {
output, err := exec.CommandContext(ctx, "lsattr", "-d", "--", filePath).Output()
if err != nil {
return false, false
}
@@ -219,9 +230,9 @@ func getFileAttributes(filePath string) (bool, bool) {
return strings.Contains(fields[0], "a"), strings.Contains(fields[0], "i")
}
func handleExpansion(file *FileInfo, op FileOption) error {
func handleExpansion(ctx context.Context, file *FileInfo, op FileOption) error {
if file.IsDir {
return file.listChildren(op)
return file.listChildrenWithContext(ctx, op)
}
if !file.IsDetail {
@@ -231,224 +242,312 @@ func handleExpansion(file *FileInfo, op FileOption) error {
return nil
}
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))
func (f *FileInfo) search(ctx context.Context, search string, count int) (files []FileSearchInfo, total int, err error) {
searchCtx, cancel := context.WithCancel(ctx)
defer cancel()
cmd := exec.CommandContext(searchCtx, "find", f.Path, "-name", fmt.Sprintf("*%s*", search), "-print0")
output, err := cmd.StdoutPipe()
if err != nil {
return
return nil, 0, err
}
if err = cmd.Start(); err != nil {
return
if err := cmd.Start(); err != nil {
return nil, 0, err
}
defer func() {
_ = cmd.Wait()
_ = cmd.Process.Kill()
}()
scanner := bufio.NewScanner(output)
for scanner.Scan() {
line := scanner.Text()
info, err := os.Stat(line)
reader := bufio.NewReader(output)
for {
if err = ctx.Err(); err != nil {
break
}
var entry string
entry, err = reader.ReadString(0)
if err != nil {
break
}
entry = strings.TrimSuffix(entry, "\x00")
info, statErr := os.Stat(entry)
if statErr != nil {
continue
}
total++
if total > count {
continue
if total <= count {
files = append(files, FileSearchInfo{Path: entry, FileInfo: info})
}
files = append(files, FileSearchInfo{
Path: line,
FileInfo: info,
})
}
if err = scanner.Err(); err != nil {
return
if err != nil && err != io.EOF {
cancel()
}
_ = cmd.Wait()
if contextErr := ctx.Err(); contextErr != nil {
return nil, 0, contextErr
}
if err == io.EOF {
err = nil
}
return
}
func sortFileList(list []FileSearchInfo, sortBy, sortOrder string) {
_ = sortFileListWithContext(context.Background(), list, sortBy, sortOrder)
}
func sortFileListWithContext(ctx context.Context, list []FileSearchInfo, sortBy, sortOrder string) error {
var less func(i, j int) bool
switch sortBy {
case "name":
if sortOrder == "ascending" {
sort.Slice(list, func(i, j int) bool {
return list[i].Name() < list[j].Name()
})
} else {
sort.Slice(list, func(i, j int) bool {
return list[i].Name() > list[j].Name()
})
}
less = func(i, j int) bool { return list[i].Name() < list[j].Name() }
case "size":
if sortOrder == "ascending" {
sort.Slice(list, func(i, j int) bool {
return list[i].Size() < list[j].Size()
})
} else {
sort.Slice(list, func(i, j int) bool {
return list[i].Size() > list[j].Size()
})
}
less = func(i, j int) bool { return list[i].Size() < list[j].Size() }
case "modTime":
if sortOrder == "ascending" {
sort.Slice(list, func(i, j int) bool {
return list[i].ModTime().Before(list[j].ModTime())
})
} else {
sort.Slice(list, func(i, j int) bool {
return list[i].ModTime().After(list[j].ModTime())
})
}
less = func(i, j int) bool { return list[i].ModTime().Before(list[j].ModTime()) }
default:
return ctx.Err()
}
return sortSliceWithContext(ctx, list, func(i, j int) bool {
if sortOrder != "ascending" {
i, j = j, i
}
return less(i, j)
})
}
func sortSliceWithContext(ctx context.Context, items interface{}, less func(i, j int) bool) (err error) {
if err := ctx.Err(); err != nil {
return err
}
if ctx.Done() == nil {
sort.Slice(items, less)
return nil
}
canceled := &struct{ marker byte }{}
defer func() {
if recovered := recover(); recovered != nil {
if recovered != canceled {
panic(recovered)
}
err = ctx.Err()
}
}()
comparisons := 0
sort.Slice(items, func(i, j int) bool {
if comparisons%256 == 0 && ctx.Err() != nil {
panic(canceled)
}
comparisons++
return less(i, j)
})
return ctx.Err()
}
func (f *FileInfo) listChildren(option FileOption) error {
var (
files []FileSearchInfo
err error
total int
)
return f.listChildrenWithContext(context.Background(), option)
}
func (f *FileInfo) listChildrenWithContext(ctx context.Context, option FileOption) error {
if err := ctx.Err(); err != nil {
return err
}
maxInt := int(^uint(0) >> 1)
if option.Page < 1 || option.PageSize < 1 || option.Page > maxInt/option.PageSize {
return fmt.Errorf("invalid file pagination")
}
var (
entries []FileSearchInfo
total int
err error
)
if option.Search != "" && option.ContainSub {
files, total, err = f.search(option.Search, option.Page*option.PageSize)
if err != nil {
return err
}
entries, total, err = f.search(ctx, option.Search, option.Page*option.PageSize)
} else {
files, err = f.getFiles(option)
if err != nil {
return err
}
entries, err = f.getFiles(ctx, option)
}
items, err := f.processFiles(files, option)
if err != nil {
return err
}
if option.ContainSub {
f.ItemTotal = total
}
start := (option.Page - 1) * option.PageSize
end := option.PageSize + start
var result []*FileInfo
if start < 0 || start > f.ItemTotal || end < 0 || start > end {
result = items
} else {
if end > f.ItemTotal {
result = items[start:]
} else {
result = items[start:end]
candidates := entries[:0]
for _, entry := range entries {
if err := ctx.Err(); err != nil {
return err
}
}
f.Items = result
return nil
}
func (f *FileInfo) getFiles(option FileOption) ([]FileSearchInfo, error) {
infos, err := os.ReadDir(f.Path)
if err != nil {
return nil, err
}
var (
dirs []FileSearchInfo
fileList []FileSearchInfo
)
for _, file := range infos {
fileInfo, err := file.Info()
if err != nil {
if shouldSkipFile(entry, option) {
continue
}
info := FileSearchInfo{
Path: f.Path,
FileInfo: fileInfo,
}
if file.IsDir() {
dirs = append(dirs, info)
} else {
fileList = append(fileList, info)
}
}
sortFileList(dirs, option.SortBy, option.SortOrder)
sortFileList(fileList, option.SortBy, option.SortOrder)
return append(dirs, fileList...), nil
}
func (f *FileInfo) processFiles(files []FileSearchInfo, option FileOption) ([]*FileInfo, error) {
var items []*FileInfo
for _, df := range files {
if shouldSkipFile(df, option) {
continue
}
name, fPath := f.getFilePathAndName(option, df)
name, _ := f.getFilePathAndName(option, entry)
if !option.ShowHidden && IsHidden(name) {
continue
}
f.ItemTotal++
candidates = append(candidates, entry)
}
f.ItemTotal = len(candidates)
if option.ContainSub {
f.ItemTotal = total
}
isSymlink, isInvalidLink := f.checkSymlink(df)
page := candidates
if !option.ContainSub || option.Search != "" {
start := (option.Page - 1) * option.PageSize
end := option.Page * option.PageSize
if start >= len(candidates) {
page = nil
} else {
if end > len(candidates) {
end = len(candidates)
}
page = candidates[start:end]
}
}
f.Items, err = f.processFiles(ctx, page, option)
if err != nil {
return err
}
return f.loadFavorites(ctx)
}
func readDirWithContext(ctx context.Context, dirPath string) ([]os.DirEntry, error) {
dir, err := os.Open(dirPath)
if err != nil {
return nil, err
}
defer dir.Close()
var entries []os.DirEntry
for {
if err := ctx.Err(); err != nil {
return nil, err
}
batch, err := dir.ReadDir(256)
entries = append(entries, batch...)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
}
if err := sortSliceWithContext(ctx, entries, func(i, j int) bool { return entries[i].Name() < entries[j].Name() }); err != nil {
return nil, err
}
return entries, nil
}
func (f *FileInfo) getFiles(ctx context.Context, option FileOption) ([]FileSearchInfo, error) {
entries, err := readDirWithContext(ctx, f.Path)
if err != nil {
return nil, err
}
var dirs, fileList []FileSearchInfo
for _, entry := range entries {
if err := ctx.Err(); err != nil {
return nil, err
}
info, err := entry.Info()
if err != nil {
continue
}
item := FileSearchInfo{Path: f.Path, FileInfo: info}
if entry.IsDir() {
dirs = append(dirs, item)
} else {
fileList = append(fileList, item)
}
}
if err := sortFileListWithContext(ctx, dirs, option.SortBy, option.SortOrder); err != nil {
return nil, err
}
if err := sortFileListWithContext(ctx, fileList, option.SortBy, option.SortOrder); err != nil {
return nil, err
}
return append(dirs, fileList...), ctx.Err()
}
func (f *FileInfo) processFiles(ctx context.Context, entries []FileSearchInfo, option FileOption) ([]*FileInfo, error) {
var items []*FileInfo
users := make(map[uint32]string)
groups := make(map[uint32]string)
for _, entry := range entries {
if err := ctx.Err(); err != nil {
return nil, err
}
name, filePath := f.getFilePathAndName(option, entry)
stat := entry.Sys().(*syscall.Stat_t)
username, ok := users[stat.Uid]
if !ok {
username = GetUsername(stat.Uid)
users[stat.Uid] = username
}
group, ok := groups[stat.Gid]
if !ok {
group = GetGroup(stat.Gid)
groups[stat.Gid] = group
}
isSymlink, isInvalidLink := f.checkSymlink(entry)
file := &FileInfo{
Fs: f.Fs,
Name: name,
Size: df.Size(),
ModTime: df.ModTime(),
FileMode: df.Mode(),
IsDir: df.IsDir(),
IsSymlink: isSymlink,
IsHidden: IsHidden(fPath),
Extension: filepath.Ext(name),
Path: fPath,
Mode: fmt.Sprintf("%04o", df.Mode().Perm()),
User: GetUsername(df.Sys().(*syscall.Stat_t).Uid),
Group: GetGroup(df.Sys().(*syscall.Stat_t).Gid),
Uid: strconv.FormatUint(uint64(df.Sys().(*syscall.Stat_t).Uid), 10),
Gid: strconv.FormatUint(uint64(df.Sys().(*syscall.Stat_t).Gid), 10),
}
favoriteRepo := repo.NewIFavoriteRepo()
favorite, _ := favoriteRepo.GetFirst(favoriteRepo.WithByPath(fPath))
if favorite.ID > 0 {
file.FavoriteID = favorite.ID
Fs: f.Fs, Name: name, Path: filePath,
Size: entry.Size(), ModTime: entry.ModTime(), FileMode: entry.Mode(),
IsDir: entry.IsDir(), IsSymlink: isSymlink, IsHidden: IsHidden(filePath),
Extension: filepath.Ext(name), Mode: fmt.Sprintf("%04o", entry.Mode().Perm()),
User: username, Group: group,
Uid: strconv.FormatUint(uint64(stat.Uid), 10),
Gid: strconv.FormatUint(uint64(stat.Gid), 10),
}
mimeInfo := entry.FileInfo
if isSymlink {
linkPath := GetSymlink(fPath)
linkPath := GetSymlink(filePath)
if !filepath.IsAbs(linkPath) {
dir := filepath.Dir(fPath)
var err error
linkPath, err = filepath.Abs(filepath.Join(dir, linkPath))
resolved, err := filepath.Abs(filepath.Join(filepath.Dir(filePath), linkPath))
if err != nil {
return nil, err
}
linkPath = resolved
}
file.LinkPath = linkPath
targetInfo, err := file.Fs.Stat(linkPath)
mimeInfo = targetInfo
if err != nil {
file.IsDir = false
file.Mode = "-"
file.User = "-"
file.Group = "-"
file.Mode, file.User, file.Group = "-", "-", "-"
} else {
file.IsDir = targetInfo.IsDir()
}
file.Extension = filepath.Ext(file.LinkPath)
}
if df.Size() > 0 {
file.MimeType = GetMimeType(fPath)
if entry.Size() > 0 && mimeInfo != nil && mimeInfo.Mode().IsRegular() {
file.MimeType = getMimeTypeFromFS(f.Fs, filePath)
}
if isInvalidLink {
file.Type = "invalid_link"
}
items = append(items, file)
}
return items, ctx.Err()
}
return items, nil
func (f *FileInfo) loadFavorites(ctx context.Context) error {
if err := ctx.Err(); err != nil {
return err
}
paths := make([]string, 0, len(f.Items)+1)
paths = append(paths, f.Path)
for _, item := range f.Items {
paths = append(paths, item.Path)
}
favorites, err := repo.NewIFavoriteRepo().ListByPaths(ctx, paths)
if ctx.Err() != nil {
return ctx.Err()
}
if err != nil {
return nil
}
ids := make(map[string]uint, len(favorites))
for _, favorite := range favorites {
if id, ok := ids[favorite.Path]; !ok || favorite.ID < id {
ids[favorite.Path] = favorite.ID
}
}
f.FavoriteID = ids[f.Path]
for _, item := range f.Items {
item.FavoriteID = ids[item.Path]
}
return nil
}
func shouldSkipFile(df FileSearchInfo, option FileOption) bool {
+6 -1
View File
@@ -16,6 +16,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/utils/req_helper"
"github.com/spf13/afero"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/japanese"
@@ -39,7 +40,11 @@ func IsBlockDevice(mode os.FileMode) bool {
}
func GetMimeType(path string) string {
file, err := os.Open(path)
return getMimeTypeFromFS(afero.NewOsFs(), path)
}
func getMimeTypeFromFS(fs afero.Fs, path string) string {
file, err := fs.Open(path)
if err != nil {
return ""
}
+9 -7
View File
@@ -10,13 +10,15 @@ export type FileUploadRequestConfig = AxiosRequestConfig & {
skipErrorMessage?: boolean;
};
export const getFilesList = (params: File.ReqFile, currentNode?: string) => {
return http.post<File.File>(
'files/search',
params,
TimeoutEnum.T_5M,
currentNode ? { CurrentNode: currentNode } : undefined,
);
export const getFilesList = (params: File.ReqFile, currentNode?: string, config?: AxiosRequestConfig) => {
return http.postWithConfig<File.File>('files/search', params, {
timeout: TimeoutEnum.T_5M,
...config,
headers: {
...config?.headers,
...(currentNode ? { CurrentNode: currentNode } : {}),
},
});
};
export const fileAiSearch = (params: File.FileAISearchReq) => {
@@ -208,8 +208,8 @@
<div class="file-search-input-shell w-full min-w-0">
<el-input
v-model="req.search"
@clear="search()"
@keydown.enter="search()"
@clear="searchFromStart"
@keydown.enter="searchFromStart"
:placeholder="$t('file.search')"
>
<template #prepend>
@@ -218,7 +218,7 @@
</el-checkbox>
</template>
<template #append>
<el-button icon="Search" @click="search" round />
<el-button icon="Search" @click="searchFromStart" round />
</template>
</el-input>
</div>
@@ -745,7 +745,18 @@
<script setup lang="ts">
import { useSearchState } from '@/utils/search-reset';
import { toRef, computed, nextTick, onBeforeUnmount, onMounted, reactive, ref } from 'vue';
import {
toRef,
computed,
nextTick,
onActivated,
onBeforeUnmount,
onDeactivated,
onMounted,
reactive,
ref,
watch,
} from 'vue';
import {
addFavorite,
batchGetFileRemarks,
@@ -774,6 +785,7 @@ import { useRouter } from 'vue-router';
import { MsgSuccess, MsgWarning } from '@/utils/message';
import { useMultipleSearchable } from './hooks/searchable';
import { mergeDirSizeCache, sortFilesByDisplaySize, updateDirSizeCache } from './size-cache';
import { createFileListRequest } from './list-request';
import { ResultData } from '@/api/interface';
import { useGlobalStore } from '@/composables/useGlobalStore';
import { resolveMaybeFn, type FuTableOperationButton } from '@/components/table/shared';
@@ -860,7 +872,9 @@ const initData = () => ({
sortOrder: 'ascending',
});
let req = reactive(initData());
let loading = ref(false);
const listLoading = ref(false);
const moveLoading = ref(false);
const loading = computed(() => listLoading.value || moveLoading.value);
const paths = ref<FilePaths[]>([]);
const breadcrumbVisiblePaths = ref<FilePaths[]>([]);
const breadcrumbHiddenPaths = ref<FilePaths[]>([]);
@@ -1087,34 +1101,47 @@ const updateButtons = async () => {
moreButtons.value = toolButtons.value.slice(visibleCount);
};
let filePageUnmounted = false;
const fileListRequest = createFileListRequest(
(params: File.ReqFile, node, signal) => getFilesList(params, node, { signal }),
() => ({ node: currentNode.value, tab: editableTabsKey.value, path: req.path }),
(value) => {
listLoading.value = value;
},
);
watch([currentNode, editableTabsKey, () => req.path], () => fileListRequest.cancel(), { flush: 'sync' });
const resetPagination = () => {
paginationConfig.currentPage = 1;
req.page = 1;
req.pageSize = paginationConfig.pageSize;
};
const search = async () => {
dirTotalSize.value = -1;
await getWgetProcess();
loading.value = true;
void getWgetProcess();
if (req.search != '') {
req.sortBy = 'name';
req.sortOrder = 'ascending';
getCurrentTable().clearSort();
getCurrentTable()?.clearSort();
}
req.page = paginationConfig.currentPage;
req.pageSize = paginationConfig.pageSize;
await getFilesList(req)
.then((res) => {
handleSearchResult(res);
})
.finally(() => {
loading.value = false;
});
const result = await searchFile();
if (result?.isCurrent()) {
handleSearchResult(result.response);
}
};
const searchFile = async () => {
loading.value = true;
const searchFromStart = () => {
resetPagination();
return search();
};
const searchFile = () => {
dirTotalSize.value = -1;
try {
return await getFilesList(req);
} finally {
loading.value = false;
}
return fileListRequest.load(req);
};
const resetDirSizeCache = () => {
@@ -1169,6 +1196,7 @@ const findExistingPath = async (filePath: string) => {
const loadInitialExistingPath = async (url: string) => {
const existingPath = await findExistingPath(url);
if (filePageUnmounted) return;
if (existingPath === normalizeFilePath(url)) {
return;
}
@@ -1182,6 +1210,7 @@ const loadInitialExistingPath = async (url: string) => {
sortOrder: oldSortOrder,
showHidden,
});
resetPagination();
lastFilePath.value = req.path;
getPaths(req.path);
updateTab(req.path);
@@ -1193,8 +1222,10 @@ const loadInitialExistingPath = async (url: string) => {
const viewHideFile = async () => {
req.showHidden = !req.showHidden;
localStorage.setItem('show-hidden', req.showHidden ? 'true' : 'false');
let searchResult = await searchFile();
handleSearchResult(searchResult);
resetPagination();
const result = await searchFile();
if (!result?.isCurrent()) return;
handleSearchResult(result.response);
};
const open = async (row: File.File) => {
@@ -1206,17 +1237,8 @@ const open = async (row: File.File) => {
MsgWarning(i18n.global.t('file.clashOpenAlert'));
return;
}
const name = row.name;
if (req.path.endsWith('/')) {
req.path = req.path + name;
} else {
req.path = req.path + '/' + name;
}
paths.value.push({
url: req.path,
name: name,
});
await jump(req.path);
const targetPath = `${req.path}${req.path.endsWith('/') ? '' : '/'}${row.name}`;
await jump(targetPath);
} else {
openView(row);
}
@@ -1353,11 +1375,15 @@ const jump = async (url: string) => {
sortOrder: oldSortOrder,
showHidden,
});
let searchResult = await searchFile();
resetPagination();
const result = await searchFile();
if (!result?.isCurrent()) return;
const searchResult = result.response;
if (!searchResult.data.path) {
req.path = oldUrl;
lastFilePath.value = req.path;
MsgWarning(i18n.global.t('commons.res.notFound'));
await search();
return;
}
req.path = searchResult.data.path;
@@ -1384,8 +1410,10 @@ const backForwardJump = async (url: string) => {
sortOrder: oldSortOrder,
showHidden,
});
let searchResult = await searchFile();
handleSearchResult(searchResult);
resetPagination();
const result = await searchFile();
if (!result?.isCurrent()) return;
handleSearchResult(result.response);
getPaths(req.path);
updateTab(req.path);
await nextTick(function () {
@@ -1913,7 +1941,7 @@ const openPaste = () => {
};
function onLoading(isLoading: boolean) {
loading.value = isLoading;
moveLoading.value = isLoading;
}
const openDownload = (file: File.File) => {
@@ -1974,10 +2002,9 @@ const changeSort = ({ prop, order }) => {
req.sortBy = prop;
req.sortOrder = order;
req.search = '';
req.page = 1;
req.pageSize = paginationConfig.pageSize;
resetPagination();
req.containSub = false;
search();
return search();
};
const showFavorite = (row: File.File) => {
@@ -2451,7 +2478,9 @@ function buildPaths(path: string) {
async function initHistory() {
await loadInitialExistingPath(req.path);
if (filePageUnmounted) return;
await search();
if (filePageUnmounted) return;
history.push(req.path);
pointer = history.length - 1;
}
@@ -2564,6 +2593,7 @@ const changeTab = (targetPath: TabPaneName) => {
saveStorageTabsKey();
if (req.path !== current?.path) {
resetDirSizeCache();
resetPagination();
}
req.path = current ? current.path : '';
lastFilePath.value = req.path;
@@ -2606,7 +2636,9 @@ const updateHeight = () => {
onMounted(async () => {
await loadPath();
if (filePageUnmounted) return;
await nextTick();
if (filePageUnmounted) return;
watchTitleHeight();
window.addEventListener('resize', watchTitleHeight);
updateHeight();
@@ -2614,15 +2646,23 @@ onMounted(async () => {
initShowHidden();
initTabsAndPaths();
await getHostMount();
if (filePageUnmounted) return;
await initHistory();
if (filePageUnmounted) return;
checkFFmpeg();
await nextTick(function () {
if (filePageUnmounted) return;
handlePath();
observeResize();
});
});
onActivated(() => fileListRequest.resume());
onDeactivated(() => fileListRequest.pause());
onBeforeUnmount(() => {
filePageUnmounted = true;
fileListRequest.dispose();
if (resizeObserver) resizeObserver.disconnect();
window.removeEventListener('resize', watchTitleHeight);
window.removeEventListener('resize', updateHeight);
@@ -0,0 +1,92 @@
export interface FileListContext {
node: string;
tab: string;
path: string;
}
interface FileListResponse<Result> {
response: Result;
isCurrent: () => boolean;
}
export const createFileListRequest = <Params extends object, Result>(
request: (params: Params, node: string, signal: AbortSignal) => Promise<Result>,
getContext: () => FileListContext,
setLoading: (loading: boolean) => void,
) => {
let generation = 0;
let paused = false;
let disposed = false;
let active:
| {
key: string;
controller: AbortController;
promise: Promise<FileListResponse<Result> | undefined>;
}
| undefined;
const cancel = () => {
generation++;
active?.controller.abort();
active = undefined;
setLoading(false);
};
const load = (params: Params): Promise<FileListResponse<Result> | undefined> => {
if (paused || disposed) {
return Promise.resolve(undefined);
}
const snapshot = { ...params };
const context = { ...getContext() };
const key = JSON.stringify([context, snapshot]);
if (active?.key === key) {
return active.promise;
}
active?.controller.abort();
const controller = new AbortController();
const currentGeneration = ++generation;
const isCurrent = () => {
const current = getContext();
return (
generation === currentGeneration &&
!controller.signal.aborted &&
current.node === context.node &&
current.tab === context.tab &&
current.path === context.path
);
};
setLoading(true);
const promise = Promise.resolve()
.then(() => (isCurrent() ? request(snapshot, context.node, controller.signal) : undefined))
.then((response) => (response !== undefined && isCurrent() ? { response, isCurrent } : undefined))
.catch((error: unknown) => {
if (!isCurrent()) {
return undefined;
}
throw error;
})
.finally(() => {
if (generation === currentGeneration) {
active = undefined;
setLoading(false);
}
});
active = { key, controller, promise };
return promise;
};
const pause = () => {
paused = true;
cancel();
};
const resume = () => {
if (!disposed) paused = false;
};
const dispose = () => {
disposed = true;
pause();
};
return { load, cancel, pause, resume, dispose };
};