2020-08-19 23:36:46 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-12-10 15:14:32 +00:00
|
|
|
"io"
|
2020-08-19 23:36:46 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-12-10 15:14:32 +00:00
|
|
|
"sync"
|
2020-08-19 23:36:46 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api/layer"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2020-12-10 15:14:32 +00:00
|
|
|
type (
|
|
|
|
detector struct {
|
|
|
|
io.Writer
|
|
|
|
sync.Once
|
|
|
|
|
|
|
|
contentType string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func newDetector(w io.Writer) *detector {
|
|
|
|
return &detector{Writer: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *detector) Write(data []byte) (int, error) {
|
|
|
|
d.Once.Do(func() {
|
|
|
|
if rw, ok := d.Writer.(http.ResponseWriter); ok {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.contentType = http.DetectContentType(data)
|
|
|
|
})
|
|
|
|
|
|
|
|
return d.Writer.Write(data)
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:46 +00:00
|
|
|
func (h *handler) GetObjectHandler(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())
|
|
|
|
)
|
|
|
|
|
|
|
|
if inf, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err != nil {
|
|
|
|
h.log.Error("could not find object",
|
|
|
|
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)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:14:32 +00:00
|
|
|
writer := newDetector(w)
|
|
|
|
|
2020-10-24 13:09:22 +00:00
|
|
|
params := &layer.GetObjectParams{
|
|
|
|
Bucket: inf.Bucket,
|
|
|
|
Object: inf.Name,
|
2020-12-10 15:14:32 +00:00
|
|
|
Writer: writer,
|
2020-10-24 13:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// params.Length = inf.Size
|
2020-08-19 23:36:46 +00:00
|
|
|
|
|
|
|
if err = h.obj.GetObject(r.Context(), params); err != nil {
|
|
|
|
h.log.Error("could not get object",
|
|
|
|
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)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:14:32 +00:00
|
|
|
w.Header().Set("Content-Type", writer.contentType)
|
2020-08-19 23:36:46 +00:00
|
|
|
w.Header().Set("Last-Modified", inf.Created.Format(http.TimeFormat))
|
2020-10-24 13:09:22 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(inf.Size, 10))
|
2020-08-19 23:36:46 +00:00
|
|
|
}
|