forked from TrueCloudLab/frostfs-s3-gw
[#289] Add detect mimetype by extension
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
ef4550ba45
commit
9f740b9683
2 changed files with 31 additions and 14 deletions
|
@ -69,18 +69,20 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(info.ContentType) == 0 {
|
if len(info.ContentType) == 0 {
|
||||||
buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType))
|
if info.ContentType = layer.MimeByFileName(info.Name); len(info.ContentType) == 0 {
|
||||||
getParams := &layer.GetObjectParams{
|
buffer := bytes.NewBuffer(make([]byte, 0, sizeToDetectType))
|
||||||
ObjectInfo: info,
|
getParams := &layer.GetObjectParams{
|
||||||
Writer: buffer,
|
ObjectInfo: info,
|
||||||
Range: getRangeToDetectContentType(info.Size),
|
Writer: buffer,
|
||||||
BucketInfo: bktInfo,
|
Range: getRangeToDetectContentType(info.Size),
|
||||||
|
BucketInfo: bktInfo,
|
||||||
|
}
|
||||||
|
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
||||||
|
h.logAndSendError(w, "could not get object", reqInfo, err, zap.Stringer("oid", info.ID))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
info.ContentType = http.DetectContentType(buffer.Bytes())
|
||||||
}
|
}
|
||||||
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
|
||||||
h.logAndSendError(w, "could not get object", reqInfo, err, zap.Stringer("oid", info.ID))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
info.ContentType = http.DetectContentType(buffer.Bytes())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = h.setLockingHeaders(r.Context(), bktInfo, info, w.Header()); err != nil {
|
if err = h.setLockingHeaders(r.Context(), bktInfo, info, w.Header()); err != nil {
|
||||||
|
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mime"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -155,6 +157,15 @@ func (n *layer) objectGet(ctx context.Context, bktInfo *data.BucketInfo, objID o
|
||||||
return res.Head, nil
|
return res.Head, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MimeByFileName detect mime type by filename extension.
|
||||||
|
func MimeByFileName(name string) string {
|
||||||
|
ext := filepath.Ext(name)
|
||||||
|
if len(ext) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return mime.TypeByExtension(ext)
|
||||||
|
}
|
||||||
|
|
||||||
// PutObject stores object into NeoFS, took payload from io.Reader.
|
// PutObject stores object into NeoFS, took payload from io.Reader.
|
||||||
func (n *layer) PutObject(ctx context.Context, p *PutObjectParams) (*data.ObjectInfo, error) {
|
func (n *layer) PutObject(ctx context.Context, p *PutObjectParams) (*data.ObjectInfo, error) {
|
||||||
own := n.Owner(ctx)
|
own := n.Owner(ctx)
|
||||||
|
@ -169,11 +180,15 @@ func (n *layer) PutObject(ctx context.Context, p *PutObjectParams) (*data.Object
|
||||||
r := p.Reader
|
r := p.Reader
|
||||||
if r != nil {
|
if r != nil {
|
||||||
if len(p.Header[api.ContentType]) == 0 {
|
if len(p.Header[api.ContentType]) == 0 {
|
||||||
d := newDetector(r)
|
if contentType := MimeByFileName(p.Object); len(contentType) == 0 {
|
||||||
if contentType, err := d.Detect(); err == nil {
|
d := newDetector(r)
|
||||||
|
if contentType, err = d.Detect(); err == nil {
|
||||||
|
p.Header[api.ContentType] = contentType
|
||||||
|
}
|
||||||
|
r = d.MultiReader()
|
||||||
|
} else {
|
||||||
p.Header[api.ContentType] = contentType
|
p.Header[api.ContentType] = contentType
|
||||||
}
|
}
|
||||||
r = d.MultiReader()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue