Skip to content

Commit

Permalink
Fix file storage MoveObject
Browse files Browse the repository at this point in the history
  • Loading branch information
visill committed Oct 18, 2024
1 parent 7d2f8d6 commit 9cd940a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/storage/filestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

"github.com/yezzey-gp/yproxy/config"
"github.com/yezzey-gp/yproxy/pkg/object"
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 9cd940a

Please sign in to comment.