forked from TrueCloudLab/frostfs-s3-gw
[#160] Fix head dir object
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
642f8cc3eb
commit
c05e2d6757
2 changed files with 8 additions and 50 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue