From 9cd940ae10597908f8692e55905d1f8b53b6978e Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 18 Oct 2024 08:20:26 +0000 Subject: [PATCH] Fix file storage MoveObject --- pkg/storage/filestorage.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/storage/filestorage.go b/pkg/storage/filestorage.go index 3f39350..f26ecfb 100644 --- a/pkg/storage/filestorage.go +++ b/pkg/storage/filestorage.go @@ -7,6 +7,7 @@ import ( "os" "path" "path/filepath" + "strings" "github.com/yezzey-gp/yproxy/config" "github.com/yezzey-gp/yproxy/pkg/object" @@ -28,6 +29,10 @@ func (s *FileStorageInteractor) CatFileFromStorage(name string, offset int64, _ _, err = io.CopyN(io.Discard, file, offset) return file, err } +func LastFiveDirsInPath(path string) string { + p1 := strings.Split(path, "/") + return fmt.Sprintf("/%s/%s/%s/%s/%s", p1[len(p1)-5], p1[len(p1)-4], p1[len(p1)-3], p1[len(p1)-2], p1[len(p1)-1]) +} func (s *FileStorageInteractor) ListPath(prefix string) ([]*object.ObjectInfo, error) { var data []*object.ObjectInfo err := filepath.WalkDir(s.cnf.StoragePrefix+prefix, func(path string, d fs.DirEntry, err error) error { @@ -45,7 +50,7 @@ func (s *FileStorageInteractor) ListPath(prefix string) ([]*object.ObjectInfo, e if err != nil { return err } - data = append(data, &object.ObjectInfo{Path: fileinfo.Name(), Size: fileinfo.Size()}) + data = append(data, &object.ObjectInfo{Path: LastFiveDirsInPath(path), Size: fileinfo.Size()}) return nil }) return data, err @@ -69,7 +74,11 @@ func (s *FileStorageInteractor) PatchFile(name string, r io.ReadSeeker, startOff } func (s *FileStorageInteractor) MoveObject(from string, to string) error { - return os.Rename(path.Join(s.cnf.StoragePrefix, from), path.Join(s.cnf.StoragePrefix, to)) + fromPath := path.Join(s.cnf.StoragePrefix, from) + toPath := path.Join(s.cnf.StoragePrefix, to) + toDir := path.Dir(toPath) + os.MkdirAll(toDir, 0700) + return os.Rename(fromPath, toPath) } func (s *FileStorageInteractor) DeleteObject(key string) error {