[#93] Removed unnecessary

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-06-30 15:29:01 +03:00
parent a6ec27b40d
commit 2af1b16b59
3 changed files with 35 additions and 80 deletions

View file

@ -1,13 +1,10 @@
package handler
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"github.com/gorilla/mux"
"github.com/nspcc-dev/neofs-s3-gw/api"
@ -15,63 +12,6 @@ import (
"go.uber.org/zap"
)
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() {
d.contentType = http.DetectContentType(data)
if rw, ok := d.Writer.(http.ResponseWriter); ok {
rw.WriteHeader(http.StatusOK)
if len(rw.Header().Get(api.ContentType)) == 0 {
rw.Header().Set(api.ContentType, d.contentType)
}
}
})
return d.Writer.Write(data)
}
func (h *handler) contentTypeFetcher(ctx context.Context, w io.Writer, info *layer.ObjectInfo) (string, error) {
return h.contentTypeFetcherWithRange(ctx, w, info, nil)
}
func (h *handler) contentTypeFetcherWithRange(ctx context.Context, w io.Writer, info *layer.ObjectInfo, rangeParams *layer.RangeParams) (string, error) {
if info.IsDir() {
if rangeParams != nil {
return "", fmt.Errorf("it is forbidden to request for a range in the directory")
}
return info.ContentType, nil
}
writer := newDetector(w)
params := &layer.GetObjectParams{
Bucket: info.Bucket,
Object: info.Name,
Writer: writer,
Range: rangeParams,
}
// params.Length = inf.Size
if err := h.obj.GetObject(ctx, params); err != nil {
return "", err
}
return writer.contentType, nil
}
func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams, error) {
const prefix = "bytes="
rangeHeader := headers.Get("Range")
@ -109,7 +49,9 @@ func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams,
}
func writeHeaders(h http.Header, info *layer.ObjectInfo) {
h.Set(api.ContentType, info.ContentType)
if len(info.ContentType) > 0 {
h.Set(api.ContentType, info.ContentType)
}
h.Set(api.LastModified, info.Created.Format(http.TimeFormat))
h.Set(api.ContentLength, strconv.FormatInt(info.Size, 10))
h.Set(api.ETag, info.HashSum)
@ -143,9 +85,15 @@ func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
if params != nil {
writeRangeHeaders(w, params, inf.Size)
}
if inf.ContentType, err = h.contentTypeFetcherWithRange(r.Context(), w, inf, params); err != nil {
getParams := &layer.GetObjectParams{
Bucket: inf.Bucket,
Object: inf.Name,
Writer: w,
Range: params,
}
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
writeError(w, r, h.log, "could not get object", rid, bkt, obj, err)
return
}
}