[#160] Fix head dir object

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-07-20 12:36:41 +03:00
parent 642f8cc3eb
commit c05e2d6757
2 changed files with 8 additions and 50 deletions

View file

@ -2,7 +2,6 @@ package handler
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"net/http" "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) { func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
var ( var (
err error err error
@ -64,26 +36,18 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
rid = api.GetRequestID(r.Context()) rid = api.GetRequestID(r.Context())
) )
if inf = h.checkIsFolder(r.Context(), bkt, obj); inf != nil { if inf, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err != 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 {
h.log.Error("could not fetch object info", h.log.Error("could not fetch object info",
zap.String("request_id", rid), zap.String("request_id", rid),
zap.String("bucket_name", bkt), zap.String("bucket_name", bkt),
zap.String("object_name", obj), zap.String("object_name", obj),
zap.Error(err)) zap.Error(err))
api.WriteErrorResponse(r.Context(), w, api.Error{ var genErr *api.ObjectNotFound
Code: api.GetAPIError(api.ErrInternalError).Code, if ok := errors.As(err, &genErr); ok {
Description: err.Error(), err = api.GetAPIError(api.ErrNoSuchKey)
HTTPStatusCode: http.StatusInternalServerError, }
}, r.URL) api.WriteErrorResponse(r.Context(), w, err, r.URL)
return return
} }
buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType)) 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.Stringer("oid", inf.ID()),
zap.Error(err)) zap.Error(err))
api.WriteErrorResponse(r.Context(), w, api.Error{ api.WriteErrorResponse(r.Context(), w, err, r.URL)
Code: api.GetAPIError(api.ErrInternalError).Code,
Description: err.Error(),
HTTPStatusCode: http.StatusInternalServerError,
}, r.URL)
return return
} }

View file

@ -14,8 +14,6 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-s3-gw/api" "github.com/nspcc-dev/neofs-s3-gw/api"
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
) )
type ( 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 { if result, err := n.objectSearch(ctx, p); err != nil {
return nil, err return nil, err
} else if ln := len(result); ln == 0 { } 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 { } else if ln == 1 {
return result[0], nil return result[0], nil
} }