fix(local): directory handle (#2262)

* fix(local): check symlink dir

* fix(local): set size of dir to 0 (close #2264)
pull/2274/head
BoYanZh 2022-11-09 11:20:09 +08:00 committed by GitHub
parent e05e2fd663
commit cdcbfb24c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -77,12 +77,17 @@ func (d *Local) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
thumb = utils.EncodePath(thumb, true) thumb = utils.EncodePath(thumb, true)
thumb += "?type=thumb" thumb += "?type=thumb"
} }
isFolder := f.IsDir() || isSymlinkDir(f, fullPath)
size := f.Size()
if isFolder {
size = 0
}
file := model.ObjThumb{ file := model.ObjThumb{
Object: model.Object{ Object: model.Object{
Name: f.Name(), Name: f.Name(),
Modified: f.ModTime(), Modified: f.ModTime(),
Size: f.Size(), Size: size,
IsFolder: f.IsDir(), IsFolder: isFolder,
}, },
Thumbnail: model.Thumbnail{ Thumbnail: model.Thumbnail{
Thumbnail: thumb, Thumbnail: thumb,
@ -101,12 +106,17 @@ func (d *Local) Get(ctx context.Context, path string) (model.Obj, error) {
} }
return nil, err return nil, err
} }
isFolder := f.IsDir() || isSymlinkDir(f, path)
size := f.Size()
if isFolder {
size = 0
}
file := model.Object{ file := model.Object{
Path: path, Path: path,
Name: f.Name(), Name: f.Name(),
Modified: f.ModTime(), Modified: f.ModTime(),
Size: f.Size(), Size: size,
IsFolder: f.IsDir(), IsFolder: isFolder,
} }
return &file, nil return &file, nil
} }

View File

@ -1 +1,22 @@
package local package local
import (
"io/fs"
"os"
"path/filepath"
)
func isSymlinkDir(f fs.FileInfo, path string) bool {
if f.Mode()&os.ModeSymlink == os.ModeSymlink {
dst, err := os.Readlink(filepath.Join(path, f.Name()))
if err != nil {
return false
}
stat, err := os.Stat(dst)
if err != nil {
return false
}
return stat.IsDir()
}
return false
}