2020-08-19 23:36:34 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-06-30 12:29:01 +00:00
|
|
|
"bytes"
|
2020-08-19 23:36:34 +00:00
|
|
|
"net/http"
|
|
|
|
|
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
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
2020-08-19 23:36:34 +00:00
|
|
|
)
|
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
if inf, err = h.obj.GetObjectInfo(r.Context(), reqInfo.BucketName, reqInfo.ObjectName); err != nil {
|
|
|
|
h.logAndSendError(w, "could not fetch object info", reqInfo, err)
|
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-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not get object", reqInfo, err, zap.Stringer("oid", inf.ID()))
|
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) {
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo := api.GetReqInfo(r.Context())
|
2020-08-22 02:45:48 +00:00
|
|
|
|
2021-08-23 08:37:08 +00:00
|
|
|
bktInfo, err := h.obj.GetBucketInfo(r.Context(), reqInfo.BucketName)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not fetch object info", reqInfo, err)
|
2020-08-22 02:45:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:37:08 +00:00
|
|
|
w.Header().Set(api.ContainerID, bktInfo.CID.String())
|
2020-08-22 02:45:48 +00:00
|
|
|
api.WriteResponse(w, http.StatusOK, nil, api.MimeNone)
|
|
|
|
}
|