[#93] Removed unnecessary
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
a6ec27b40d
commit
2af1b16b59
3 changed files with 35 additions and 80 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
|
@ -12,10 +13,18 @@ import (
|
|||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type devNull int
|
||||
const sizeToDetectType = 512
|
||||
|
||||
func (d devNull) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
func getRangeToDetectContentType(maxSize int64) *layer.RangeParams {
|
||||
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 {
|
||||
|
@ -73,7 +82,15 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}, r.URL)
|
||||
|
||||
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",
|
||||
zap.String("request_id", rid),
|
||||
zap.String("bucket_name", bkt),
|
||||
|
@ -89,7 +106,7 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
inf.ContentType = http.DetectContentType(buffer.Bytes())
|
||||
writeHeaders(w.Header(), inf)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package layer
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -103,10 +102,10 @@ func objectInfoFromMeta(bkt *BucketInfo, meta *object.Object, prefix, delimiter
|
|||
filename = prefix + tail[:index+1]
|
||||
userHeaders = nil
|
||||
} else {
|
||||
size, mimeType = getSizeAndMimeType(meta, mimeType)
|
||||
size = int64(meta.PayloadSize())
|
||||
}
|
||||
} else {
|
||||
size, mimeType = getSizeAndMimeType(meta, mimeType)
|
||||
size = int64(meta.PayloadSize())
|
||||
}
|
||||
|
||||
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 {
|
||||
var name = o.ID().String()
|
||||
for _, attr := range o.Attributes() {
|
||||
|
|
Loading…
Reference in a new issue