Skip to content

Commit

Permalink
A more general and better solution (prefix cutting)
Browse files Browse the repository at this point in the history
  • Loading branch information
visill committed Oct 18, 2024
1 parent 9cd940a commit b37f0f5
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/storage/filestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ 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 @@ -50,7 +46,11 @@ func (s *FileStorageInteractor) ListPath(prefix string) ([]*object.ObjectInfo, e
if err != nil {
return err
}
data = append(data, &object.ObjectInfo{Path: LastFiveDirsInPath(path), Size: fileinfo.Size()})
cPath, ok := strings.CutPrefix(path, prefix)
if !ok {
return err
}
data = append(data, &object.ObjectInfo{Path: cPath, Size: fileinfo.Size()})
return nil
})
return data, err
Expand Down

0 comments on commit b37f0f5

Please sign in to comment.