From 29a21289f9ca249b221de326387d3b70e73da832 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Thu, 4 Jan 2024 12:55:32 +0000 Subject: [PATCH] Fix empty object name error If a default index has been provided, and a request has no object name then append the default index path before attempting to look up the object. This avoids the error "storage: object name is empty" when browsing at the root of the bucket. --- main.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 1671f4f..489032a 100644 --- a/main.go +++ b/main.go @@ -97,13 +97,23 @@ func wrapper(fn func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc { } func fetchObjectAttrs(ctx context.Context, bucket, object string) (*storage.ObjectAttrs, error) { + var err error + var indexAppended bool + if object == "" && *defaultIndex != "" { + object, err = url.JoinPath(object, *defaultIndex) + if err != nil { + return nil, err + } + indexAppended = true + } + attrs, err := client.Bucket(bucket).Object(strings.TrimSuffix(object, "/")).Attrs(ctx) if err != nil { if errors.Is(err, storage.ErrObjectNotExist) { - if *defaultIndex == "" { + if *defaultIndex == "" || indexAppended { return nil, err } - object, err := url.JoinPath(object, *defaultIndex) + object, err = url.JoinPath(object, *defaultIndex) if err != nil { return nil, err }