From 4909c71df0bd0e1b7454251b856c5d8be549d951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= Date: Wed, 18 Nov 2026 09:54:17 +0800 Subject: [PATCH] feat(file): add context support for file operations and enhance file listing (#14459) --- agent/app/api/v2/file.go | 2 +- agent/app/repo/favorite.go | 40 ++ agent/app/repo/file_share.go | 23 + agent/app/service/file.go | 47 +- agent/app/service/file_share.go | 24 + agent/utils/files/fileinfo.go | 431 +++++++++++------- agent/utils/files/utils.go | 7 +- frontend/src/api/modules/files.ts | 16 +- .../src/views/host/file-management/index.vue | 126 +++-- .../host/file-management/list-request.ts | 92 ++++ 10 files changed, 585 insertions(+), 223 deletions(-) create mode 100644 frontend/src/views/host/file-management/list-request.ts diff --git a/agent/app/api/v2/file.go b/agent/app/api/v2/file.go index e51abf06f..a5740d6fa 100644 --- a/agent/app/api/v2/file.go +++ b/agent/app/api/v2/file.go @@ -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 diff --git a/agent/app/repo/favorite.go b/agent/app/repo/favorite.go index 9fd394fe4..49a55d7c8 100644 --- a/agent/app/repo/favorite.go +++ b/agent/app/repo/favorite.go @@ -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 +} diff --git a/agent/app/repo/file_share.go b/agent/app/repo/file_share.go index 5072a70d3..a8469ca48 100644 --- a/agent/app/repo/file_share.go +++ b/agent/app/repo/file_share.go @@ -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 +} diff --git a/agent/app/service/file.go b/agent/app/service/file.go index 359c18832..cbd676865 100644 --- a/agent/app/service/file.go +++ b/agent/app/service/file.go @@ -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 { diff --git a/agent/app/service/file_share.go b/agent/app/service/file_share.go index 8b0ed91ad..e42c2faf1 100644 --- a/agent/app/service/file_share.go +++ b/agent/app/service/file_share.go @@ -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 diff --git a/agent/utils/files/fileinfo.go b/agent/utils/files/fileinfo.go index 0d6900ccd..ea96d00ef 100644 --- a/agent/utils/files/fileinfo.go +++ b/agent/utils/files/fileinfo.go @@ -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 { diff --git a/agent/utils/files/utils.go b/agent/utils/files/utils.go index b7f664174..b9cde7aeb 100644 --- a/agent/utils/files/utils.go +++ b/agent/utils/files/utils.go @@ -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 "" } diff --git a/frontend/src/api/modules/files.ts b/frontend/src/api/modules/files.ts index edf586993..319dba0d3 100644 --- a/frontend/src/api/modules/files.ts +++ b/frontend/src/api/modules/files.ts @@ -10,13 +10,15 @@ export type FileUploadRequestConfig = AxiosRequestConfig & { skipErrorMessage?: boolean; }; -export const getFilesList = (params: File.ReqFile, currentNode?: string) => { - return http.post( - 'files/search', - params, - TimeoutEnum.T_5M, - currentNode ? { CurrentNode: currentNode } : undefined, - ); +export const getFilesList = (params: File.ReqFile, currentNode?: string, config?: AxiosRequestConfig) => { + return http.postWithConfig('files/search', params, { + timeout: TimeoutEnum.T_5M, + ...config, + headers: { + ...config?.headers, + ...(currentNode ? { CurrentNode: currentNode } : {}), + }, + }); }; export const fileAiSearch = (params: File.FileAISearchReq) => { diff --git a/frontend/src/views/host/file-management/index.vue b/frontend/src/views/host/file-management/index.vue index eb9a37fc2..0a117d559 100644 --- a/frontend/src/views/host/file-management/index.vue +++ b/frontend/src/views/host/file-management/index.vue @@ -208,8 +208,8 @@
@@ -745,7 +745,18 @@