From c05e2d67572e4f672d682472f719903713b214d5 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Tue, 20 Jul 2021 12:36:41 +0300 Subject: [PATCH] [#160] Fix head dir object Signed-off-by: Denis Kirillov --- api/handler/head.go | 54 ++++++--------------------------------------- api/layer/object.go | 4 +--- 2 files changed, 8 insertions(+), 50 deletions(-) diff --git a/api/handler/head.go b/api/handler/head.go index bf177aa..16a686d 100644 --- a/api/handler/head.go +++ b/api/handler/head.go @@ -2,7 +2,6 @@ package handler import ( "bytes" - "context" "errors" "net/http" @@ -26,33 +25,6 @@ func getRangeToDetectContentType(maxSize int64) *layer.RangeParams { } } -func (h *handler) checkIsFolder(ctx context.Context, bucket, object string) *layer.ObjectInfo { - if ln := len(object); ln > 0 && object[ln-1:] != layer.PathSeparator { - return nil - } - - _, dirname := layer.NameFromString(object) - params := &layer.ListObjectsParamsV1{ - ListObjectsParamsCommon: layer.ListObjectsParamsCommon{ - Bucket: bucket, - Prefix: dirname, - Delimiter: layer.PathSeparator, - }} - if list, err := h.obj.ListObjectsV1(ctx, params); err == nil && len(list.Objects) > 0 { - return &layer.ObjectInfo{ - Bucket: bucket, - Name: object, - - ContentType: "text/directory", - - Owner: list.Objects[0].Owner, - Created: list.Objects[0].Created, - } - } - - return nil -} - func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) { var ( err error @@ -64,26 +36,18 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) { rid = api.GetRequestID(r.Context()) ) - if inf = h.checkIsFolder(r.Context(), bkt, obj); inf != nil { - // do nothing for folders - - // h.log.Debug("found folder", - // zap.String("request_id", rid), - // zap.String("bucket_name", bkt), - // zap.String("object_name", obj)) - } else if inf, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err != nil { + if inf, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err != nil { h.log.Error("could not fetch object info", zap.String("request_id", rid), zap.String("bucket_name", bkt), zap.String("object_name", obj), zap.Error(err)) - api.WriteErrorResponse(r.Context(), w, api.Error{ - Code: api.GetAPIError(api.ErrInternalError).Code, - Description: err.Error(), - HTTPStatusCode: http.StatusInternalServerError, - }, r.URL) - + var genErr *api.ObjectNotFound + if ok := errors.As(err, &genErr); ok { + err = api.GetAPIError(api.ErrNoSuchKey) + } + api.WriteErrorResponse(r.Context(), w, err, r.URL) return } buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType)) @@ -101,11 +65,7 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) { zap.Stringer("oid", inf.ID()), zap.Error(err)) - api.WriteErrorResponse(r.Context(), w, api.Error{ - Code: api.GetAPIError(api.ErrInternalError).Code, - Description: err.Error(), - HTTPStatusCode: http.StatusInternalServerError, - }, r.URL) + api.WriteErrorResponse(r.Context(), w, err, r.URL) return } diff --git a/api/layer/object.go b/api/layer/object.go index 9ae56ce..3116aad 100644 --- a/api/layer/object.go +++ b/api/layer/object.go @@ -14,8 +14,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-s3-gw/api" "go.uber.org/zap" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type ( @@ -83,7 +81,7 @@ func (n *layer) objectFindID(ctx context.Context, p *findParams) (*object.ID, er if result, err := n.objectSearch(ctx, p); err != nil { return nil, err } else if ln := len(result); ln == 0 { - return nil, status.Error(codes.NotFound, "object not found") + return nil, &api.ObjectNotFound{Bucket: p.cid.String(), Object: p.val} } else if ln == 1 { return result[0], nil }