From d2ab6bab2313761f61c91776eb5bf175ed22b80d Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 13 Nov 2024 18:54:17 +0300 Subject: [PATCH] Fix browse page invocation Signed-off-by: Alex Vanin --- internal/handler/download.go | 14 +++++++---- internal/handler/handler.go | 47 ++++++++++++++++++++++++++++++++++++ internal/handler/utils.go | 10 ++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/internal/handler/download.go b/internal/handler/download.go index 3e04541..7605164 100644 --- a/internal/handler/download.go +++ b/internal/handler/download.go @@ -23,12 +23,16 @@ import ( // DownloadByAddressOrBucketName handles download requests using simple cid/oid or bucketname/key format. func (h *Handler) DownloadByAddressOrBucketName(c *fasthttp.RequestCtx) { - cnrIDStr, _ := c.UserValue("cid").(string) - var cnrID cid.ID - if err := cnrID.DecodeString(cnrIDStr); err != nil { - h.byS3Path(c, h.receiveFile) - } else { + oidURLParam := c.UserValue("oid").(string) + cidURLParam := c.UserValue("cid").(string) + + switch { + case isContainerID(oidURLParam) && isObjectID(cidURLParam): h.byNativeAddress(c, h.receiveFile) + case !isContainerRoot(cidURLParam) && !isDir(cidURLParam): + h.byS3Path(c, h.receiveFile) + default: + h.browseIndex(c) } } diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 3247e47..d87fcb4 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -285,6 +285,53 @@ func (h *Handler) byS3Path(c *fasthttp.RequestCtx, f func(context.Context, reque f(ctx, *h.newRequest(c, log), addr) } +func (h *Handler) browseIndex(c *fasthttp.RequestCtx) { + if !h.config.IndexPageEnabled() { + c.SetStatusCode(fasthttp.StatusNotFound) + return + } + + cidURLParam := c.UserValue("cid").(string) + oidURLParam := c.UserValue("oid").(string) + + ctx := utils.GetContextFromRequest(c) + reqLog := utils.GetReqLogOrDefault(ctx, h.log) + log := reqLog.With(zap.String("cid", cidURLParam), zap.String("oid", oidURLParam)) + + unescapedKey, err := url.QueryUnescape(oidURLParam) + if err != nil { + logAndSendBucketError(c, log, err) + return + } + + bktInfo, err := h.getBucketInfo(ctx, oidURLParam, log) + if err != nil { + logAndSendBucketError(c, log, err) + return + } + + c.SetStatusCode(fasthttp.StatusOK) + + listFunc := h.getDirObjectsS3 + isNativeList := false + + _, err = h.tree.GetLatestVersion(ctx, &bktInfo.CID, "") + if err != nil { + // tree probe failed, try to use native + fmt.Println("!!! I failed tree probe") + listFunc = h.getDirObjectsS3 + isNativeList = true + } + + c.SetStatusCode(fasthttp.StatusOK) + h.browseObjects(c, browseParams{ + bucketInfo: bktInfo, + prefix: unescapedKey, + listObjects: listFunc, + isNative: isNativeList, + }) +} + // byAttribute is a wrapper similar to byNativeAddress. func (h *Handler) byAttribute(c *fasthttp.RequestCtx, f func(context.Context, request, oid.Address)) { scid, _ := c.UserValue("cid").(string) diff --git a/internal/handler/utils.go b/internal/handler/utils.go index 42665a9..979197b 100644 --- a/internal/handler/utils.go +++ b/internal/handler/utils.go @@ -51,6 +51,16 @@ func isContainerRoot(key string) bool { return key == "" } +func isContainerID(s string) bool { + var cnrID cid.ID + return cnrID.DecodeString(s) == nil +} + +func isObjectID(s string) bool { + var objID oid.ID + return objID.DecodeString(s) == nil +} + func checkErrorType(err error) int { switch { case err == nil: