From aa82cd938c67708a331275bf07c3324d1827bbb6 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 2 Nov 2021 00:33:33 +0100 Subject: [PATCH] id improved parent method --- internal/types/id.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/types/id.go b/internal/types/id.go index cc57033..ca1d006 100644 --- a/internal/types/id.go +++ b/internal/types/id.go @@ -52,16 +52,27 @@ func (i ID) Raw() interface{} { 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 { if i.Key == "/" { // Already at root. We dont have a parent return nil } - parent := &ID{ - Bucket: i.Bucket, - Key: filepath.Dir(i.Key) + "/", + 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, + Key: filepath.Dir(i.Key) + "/", + } } parent.Normalize()