Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/*: Set content-type as best errort #234

Merged
merged 8 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/mime/mime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mime

import (
"mime"
"strings"
)

// TypeByFileName will get mime type via filename, and return "" if not detected.
func TypeByFileName(filename string) string {
x := strings.Split(filename, ".")
if len(x) < 2 {
return ""
}

return mime.TypeByExtension("." + x[len(x)-1])
}
27 changes: 27 additions & 0 deletions pkg/mime/mime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mime

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTypeByFileName(t *testing.T) {
cases := []struct {
name string
filename string
expected string
}{
{"normal case", "test.pdf", "application/pdf"},
{"no ext", "testxxx", ""},
{"not a valid type", "test.xxx", ""},
{"multiple dots", "test.xx.a.pdf", "application/pdf"},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got := TypeByFileName(tt.filename)
assert.Equal(t, tt.expected, got)
})
}
}
6 changes: 6 additions & 0 deletions services/azblob/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err

o.SetETag(string(output.ETag()))

if output.ContentType() != "" {
o.SetContentType(output.ContentType())
}
if len(output.ContentMD5()) > 0 {
o.SetContentMD5(base64.StdEncoding.EncodeToString(output.ContentMD5()))
}
Expand Down Expand Up @@ -269,6 +272,9 @@ func (s *Storage) formatFileObject(v azblob.BlobItem) (o *types.Object, err erro
if v.Properties.ContentLength != nil {
o.Size = *v.Properties.ContentLength
}
if v.Properties.ContentType != nil && *v.Properties.ContentType != "" {
o.SetContentType(*v.Properties.ContentType)
}
if len(v.Properties.ContentMD5) > 0 {
o.SetContentMD5(base64.StdEncoding.EncodeToString(v.Properties.ContentMD5))
}
Expand Down
8 changes: 7 additions & 1 deletion services/cos/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
}
o.UpdatedAt = lastModified

o.SetETag(output.Header.Get("ETag"))
if v := output.Header.Get("Content-Type"); v != "" {
o.SetContentType(v)
}

if v := output.Header.Get("ETag"); v != "" {
o.SetETag(output.Header.Get(v))
}

storageClass, err := formatStorageClass(output.Header.Get(storageClassHeader))
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions services/fs/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/Xuanwo/storage/pkg/iowrap"
"github.com/Xuanwo/storage/pkg/mime"
"github.com/Xuanwo/storage/services"
"github.com/Xuanwo/storage/types"
"github.com/Xuanwo/storage/types/metadata"
Expand Down Expand Up @@ -81,6 +82,10 @@ func (s *Storage) List(path string, pairs ...*types.Pair) (err error) {
continue
}

if v := mime.TypeByFileName(v.Name()); v != "" {
o.SetContentType(v)
}

o.Type = types.ObjectTypeFile
if opt.HasFileFunc {
opt.FileFunc(o)
Expand Down Expand Up @@ -213,6 +218,10 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
return
}
if fi.Mode().IsRegular() {
if v := mime.TypeByFileName(path); v != "" {
o.SetContentType(v)
}

o.Type = types.ObjectTypeFile
return
}
Expand Down
71 changes: 31 additions & 40 deletions services/gcs/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,10 @@ func (s *Storage) List(path string, pairs ...*types.Pair) (err error) {
continue
}

o := &types.Object{
ID: object.Name,
Name: s.getRelPath(object.Name),
Type: types.ObjectTypeFile,
Size: object.Size,
UpdatedAt: object.Updated,
ObjectMeta: metadata.NewObjectMeta(),
}

o.SetContentType(object.ContentType)
o.SetETag(object.Etag)

if len(object.MD5) > 0 {
o.SetContentMD5(base64.StdEncoding.EncodeToString(object.MD5))
}

storageClass, err := formatStorageClass(object.StorageClass)
o, err := s.formatFileObject(object)
if err != nil {
return err
}
o.SetStorageClass(storageClass)

if opt.HasFileFunc {
opt.FileFunc(o)
Expand Down Expand Up @@ -206,28 +189,7 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
return nil, err
}

o = &types.Object{
ID: attr.Name,
Name: path,
Type: types.ObjectTypeFile,
Size: attr.Size,
UpdatedAt: attr.Updated,
ObjectMeta: metadata.NewObjectMeta(),
}

o.SetETag(attr.Etag)

if len(attr.MD5) > 0 {
o.SetContentMD5(base64.StdEncoding.EncodeToString(attr.MD5))
}

storageClass, err := formatStorageClass(attr.StorageClass)
if err != nil {
return nil, err
}
o.SetStorageClass(storageClass)

return o, nil
return s.formatFileObject(attr)
}

// Delete implements Storager.Delete
Expand Down Expand Up @@ -274,3 +236,32 @@ func (s *Storage) formatError(op string, err error, path ...string) error {
Path: path,
}
}

func (s *Storage) formatFileObject(v *gs.ObjectAttrs) (o *types.Object, err error) {
o = &types.Object{
ID: v.Name,
Name: s.getRelPath(v.Name),
Type: types.ObjectTypeFile,
Size: v.Size,
UpdatedAt: v.Updated,
ObjectMeta: metadata.NewObjectMeta(),
}

if v.ContentType != "" {
o.SetContentType(v.ContentType)
}
if v.Etag != "" {
o.SetETag(v.Etag)
}
if len(v.MD5) > 0 {
o.SetContentMD5(base64.StdEncoding.EncodeToString(v.MD5))
}

storageClass, err := formatStorageClass(v.StorageClass)
if err != nil {
return nil, err
}
o.SetStorageClass(storageClass)

return
}
3 changes: 3 additions & 0 deletions services/kodo/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (s *Storage) List(path string, pairs ...*types.Pair) (err error) {
UpdatedAt: convertUnixTimestampToTime(v.PutTime),
ObjectMeta: metadata.NewObjectMeta(),
}

o.SetContentType(v.MimeType)
o.SetETag(v.Hash)

Expand Down Expand Up @@ -191,7 +192,9 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
UpdatedAt: convertUnixTimestampToTime(fi.PutTime),
ObjectMeta: metadata.NewObjectMeta(),
}

o.SetETag(fi.Hash)
o.SetContentType(fi.MimeType)

storageClass, err := formatStorageClass(fi.Type)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions services/oss/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
//
// ref: https://help.aliyun.com/document_detail/31965.html
o.SetETag(output.Get("ETag"))
o.SetContentType(output.Get("Content-Type"))

storageClass, err := formatStorageClass(output.Get(storageClassHeader))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions services/uss/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (s *Storage) List(path string, pairs ...*types.Pair) (err error) {
ObjectMeta: metadata.NewObjectMeta(),
}
o.SetETag(v.ETag)
o.SetContentType(v.ContentType)

if opt.HasObjectFunc {
opt.ObjectFunc(o)
Expand Down Expand Up @@ -187,6 +188,7 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
ObjectMeta: metadata.NewObjectMeta(),
}
o.SetETag(output.ETag)
o.SetContentType(output.ContentType)

return o, nil
}
Expand Down