2020-08-19 23:36:34 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-06-30 12:29:01 +00:00
|
|
|
"bytes"
|
2021-07-09 08:57:44 +00:00
|
|
|
"errors"
|
2020-08-19 23:36:34 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2020-08-19 23:36:34 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-06-30 12:29:01 +00:00
|
|
|
const sizeToDetectType = 512
|
2021-01-14 17:39:48 +00:00
|
|
|
|
2021-06-30 12:29:01 +00:00
|
|
|
func getRangeToDetectContentType(maxSize int64) *layer.RangeParams {
|
|
|
|
end := uint64(maxSize)
|
|
|
|
if sizeToDetectType < end {
|
|
|
|
end = sizeToDetectType
|
|
|
|
}
|
|
|
|
|
|
|
|
return &layer.RangeParams{
|
|
|
|
Start: 0,
|
|
|
|
End: end - 1,
|
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:34 +00:00
|
|
|
func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
inf *layer.ObjectInfo
|
|
|
|
|
|
|
|
req = mux.Vars(r)
|
|
|
|
bkt = req["bucket"]
|
|
|
|
obj = req["object"]
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
)
|
|
|
|
|
2021-07-20 09:36:41 +00:00
|
|
|
if inf, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err != nil {
|
2020-08-19 23:36:34 +00:00
|
|
|
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))
|
|
|
|
|
2021-07-20 09:36:41 +00:00
|
|
|
var genErr *api.ObjectNotFound
|
|
|
|
if ok := errors.As(err, &genErr); ok {
|
|
|
|
err = api.GetAPIError(api.ErrNoSuchKey)
|
|
|
|
}
|
|
|
|
api.WriteErrorResponse(r.Context(), w, err, r.URL)
|
2020-08-19 23:36:34 +00:00
|
|
|
return
|
2021-06-30 12:29:01 +00:00
|
|
|
}
|
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType))
|
|
|
|
getParams := &layer.GetObjectParams{
|
|
|
|
Bucket: inf.Bucket,
|
|
|
|
Object: inf.Name,
|
|
|
|
Writer: buffer,
|
|
|
|
Range: getRangeToDetectContentType(inf.Size),
|
|
|
|
}
|
|
|
|
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
2021-01-14 17:39:48 +00:00
|
|
|
h.log.Error("could not get object",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.String("bucket_name", bkt),
|
|
|
|
zap.String("object_name", obj),
|
|
|
|
zap.Stringer("oid", inf.ID()),
|
|
|
|
zap.Error(err))
|
2020-08-19 23:36:34 +00:00
|
|
|
|
2021-07-20 09:36:41 +00:00
|
|
|
api.WriteErrorResponse(r.Context(), w, err, r.URL)
|
2021-01-14 17:39:48 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-06-30 12:29:01 +00:00
|
|
|
inf.ContentType = http.DetectContentType(buffer.Bytes())
|
2021-01-14 17:39:48 +00:00
|
|
|
writeHeaders(w.Header(), inf)
|
2020-10-24 13:09:22 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-08-19 23:36:34 +00:00
|
|
|
}
|
2020-08-22 02:45:48 +00:00
|
|
|
|
|
|
|
func (h *handler) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
req = mux.Vars(r)
|
|
|
|
bkt = req["bucket"]
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
)
|
|
|
|
|
|
|
|
if _, err := h.obj.GetBucketInfo(r.Context(), bkt); err != nil {
|
|
|
|
h.log.Error("could not fetch object info",
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.String("bucket_name", bkt),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
code := http.StatusBadRequest
|
2021-07-09 08:57:44 +00:00
|
|
|
if errors.Is(err, layer.ErrBucketNotFound) {
|
|
|
|
code = http.StatusNotFound
|
2020-08-22 02:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api.WriteResponse(w, code, nil, api.MimeNone)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.WriteResponse(w, http.StatusOK, nil, api.MimeNone)
|
|
|
|
}
|