[#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 package handler
import ( import (
"context"
"fmt" "fmt"
"io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/nspcc-dev/neofs-s3-gw/api" "github.com/nspcc-dev/neofs-s3-gw/api"
@ -15,63 +12,6 @@ import (
"go.uber.org/zap" "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) { func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams, error) {
const prefix = "bytes=" const prefix = "bytes="
rangeHeader := headers.Get("Range") 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) { func writeHeaders(h http.Header, info *layer.ObjectInfo) {
if len(info.ContentType) > 0 {
h.Set(api.ContentType, info.ContentType) h.Set(api.ContentType, info.ContentType)
}
h.Set(api.LastModified, info.Created.Format(http.TimeFormat)) h.Set(api.LastModified, info.Created.Format(http.TimeFormat))
h.Set(api.ContentLength, strconv.FormatInt(info.Size, 10)) h.Set(api.ContentLength, strconv.FormatInt(info.Size, 10))
h.Set(api.ETag, info.HashSum) h.Set(api.ETag, info.HashSum)
@ -143,9 +85,15 @@ func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
if params != nil { if params != nil {
writeRangeHeaders(w, params, inf.Size) 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) writeError(w, r, h.log, "could not get object", rid, bkt, obj, err)
return
} }
} }

View file

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"bytes"
"context" "context"
"net/http" "net/http"
@ -12,10 +13,18 @@ import (
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
type devNull int const sizeToDetectType = 512
func (d devNull) Write(p []byte) (n int, err error) { func getRangeToDetectContentType(maxSize int64) *layer.RangeParams {
return len(p), nil end := uint64(maxSize)
if sizeToDetectType < end {
end = sizeToDetectType
}
return &layer.RangeParams{
Start: 0,
End: end - 1,
}
} }
func (h *handler) checkIsFolder(ctx context.Context, bucket, object string) *layer.ObjectInfo { func (h *handler) checkIsFolder(ctx context.Context, bucket, object string) *layer.ObjectInfo {
@ -73,7 +82,15 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
}, r.URL) }, r.URL)
return return
} else if inf.ContentType, err = h.contentTypeFetcher(r.Context(), devNull(0), inf); err != nil { }
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 {
h.log.Error("could not get object", h.log.Error("could not get object",
zap.String("request_id", rid), zap.String("request_id", rid),
zap.String("bucket_name", bkt), zap.String("bucket_name", bkt),
@ -89,7 +106,7 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
inf.ContentType = http.DetectContentType(buffer.Bytes())
writeHeaders(w.Header(), inf) writeHeaders(w.Header(), inf)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View file

@ -1,7 +1,6 @@
package layer package layer
import ( import (
"net/http"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -103,10 +102,10 @@ func objectInfoFromMeta(bkt *BucketInfo, meta *object.Object, prefix, delimiter
filename = prefix + tail[:index+1] filename = prefix + tail[:index+1]
userHeaders = nil userHeaders = nil
} else { } else {
size, mimeType = getSizeAndMimeType(meta, mimeType) size = int64(meta.PayloadSize())
} }
} else { } else {
size, mimeType = getSizeAndMimeType(meta, mimeType) size = int64(meta.PayloadSize())
} }
return &ObjectInfo{ return &ObjectInfo{
@ -124,15 +123,6 @@ func objectInfoFromMeta(bkt *BucketInfo, meta *object.Object, prefix, delimiter
} }
} }
func getSizeAndMimeType(meta *object.Object, contentType string) (size int64, mimeType string) {
size = int64(meta.PayloadSize())
mimeType = contentType
if len(mimeType) == 0 {
mimeType = http.DetectContentType(meta.Payload())
}
return
}
func filenameFromObject(o *object.Object) string { func filenameFromObject(o *object.Object) string {
var name = o.ID().String() var name = o.ID().String()
for _, attr := range o.Attributes() { for _, attr := range o.Attributes() {