id improved parent method

This commit is contained in:
Djeeberjr 2021-11-02 00:33:33 +01:00
parent aac1ca8891
commit aa82cd938c

View File

@ -52,17 +52,28 @@ func (i ID) Raw() interface{} {
return i return i
} }
// Parent returns the parent dir ID. // Parent returns the parent dir ID. If its a file then return containing directory.
// If this is a directory then return the dir one up.
func (i ID) Parent() *ID { func (i ID) Parent() *ID {
if i.Key == "/" { if i.Key == "/" {
// Already at root. We dont have a parent // Already at root. We dont have a parent
return nil return nil
} }
parent := &ID{ var parent *ID
if i.IsDirectory() {
parts := strings.Split(i.Key, "/")
parent = &ID{
Bucket: i.Bucket,
Key: strings.Join(parts[:len(parts)-2], "/") + "/",
}
} else {
parent = &ID{
Bucket: i.Bucket, Bucket: i.Bucket,
Key: filepath.Dir(i.Key) + "/", Key: filepath.Dir(i.Key) + "/",
} }
}
parent.Normalize() parent.Normalize()