mirror of
https://github.com/minio/minio.git
synced 2025-01-11 23:13:23 -05:00
Simplify pkg mimedb
(#6549)
Content-Type resolution can now use a function `TypeByExtension(extension)` to resolve to the respective content-type.
This commit is contained in:
parent
f163bed40d
commit
c7722fbb1b
@ -24,7 +24,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
pathutil "path"
|
pathutil "path"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/minio/minio/cmd/logger"
|
"github.com/minio/minio/cmd/logger"
|
||||||
"github.com/minio/minio/pkg/lock"
|
"github.com/minio/minio/pkg/lock"
|
||||||
@ -137,11 +136,7 @@ func (m fsMetaV1) ToObjectInfo(bucket, object string, fi os.FileInfo) ObjectInfo
|
|||||||
|
|
||||||
// Guess content-type from the extension if possible.
|
// Guess content-type from the extension if possible.
|
||||||
if m.Meta["content-type"] == "" {
|
if m.Meta["content-type"] == "" {
|
||||||
if objectExt := pathutil.Ext(object); objectExt != "" {
|
m.Meta["content-type"] = mimedb.TypeByExtension(pathutil.Ext(object))
|
||||||
if content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]; ok {
|
|
||||||
m.Meta["content-type"] = content.ContentType
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasSuffix(object, slashSeparator) {
|
if hasSuffix(object, slashSeparator) {
|
||||||
|
18
cmd/fs-v1.go
18
cmd/fs-v1.go
@ -674,13 +674,8 @@ func (fs *FSObjects) createFsJSON(object, fsMetaPath string) error {
|
|||||||
fsMeta := newFSMetaV1()
|
fsMeta := newFSMetaV1()
|
||||||
fsMeta.Meta = make(map[string]string)
|
fsMeta.Meta = make(map[string]string)
|
||||||
fsMeta.Meta["etag"] = GenETag()
|
fsMeta.Meta["etag"] = GenETag()
|
||||||
if objectExt := path.Ext(object); objectExt != "" {
|
contentType := mimedb.TypeByExtension(path.Ext(object))
|
||||||
if content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]; ok {
|
fsMeta.Meta["content-type"] = contentType
|
||||||
fsMeta.Meta["content-type"] = content.ContentType
|
|
||||||
} else {
|
|
||||||
fsMeta.Meta["content-type"] = "application/octet-stream"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wlk, werr := fs.rwPool.Create(fsMetaPath)
|
wlk, werr := fs.rwPool.Create(fsMetaPath)
|
||||||
if werr == nil {
|
if werr == nil {
|
||||||
_, err := fsMeta.WriteTo(wlk)
|
_, err := fsMeta.WriteTo(wlk)
|
||||||
@ -695,13 +690,8 @@ func (fs *FSObjects) defaultFsJSON(object string) fsMetaV1 {
|
|||||||
fsMeta := newFSMetaV1()
|
fsMeta := newFSMetaV1()
|
||||||
fsMeta.Meta = make(map[string]string)
|
fsMeta.Meta = make(map[string]string)
|
||||||
fsMeta.Meta["etag"] = defaultEtag
|
fsMeta.Meta["etag"] = defaultEtag
|
||||||
if objectExt := path.Ext(object); objectExt != "" {
|
contentType := mimedb.TypeByExtension(path.Ext(object))
|
||||||
if content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]; ok {
|
fsMeta.Meta["content-type"] = contentType
|
||||||
fsMeta.Meta["content-type"] = content.ContentType
|
|
||||||
} else {
|
|
||||||
fsMeta.Meta["content-type"] = "application/octet-stream"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fsMeta
|
return fsMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,15 +198,8 @@ func (xl xlObjects) newMultipartUpload(ctx context.Context, bucket string, objec
|
|||||||
// establish the writeQuorum using this data
|
// establish the writeQuorum using this data
|
||||||
writeQuorum := dataBlocks + 1
|
writeQuorum := dataBlocks + 1
|
||||||
|
|
||||||
// If not set default to "application/octet-stream"
|
|
||||||
if meta["content-type"] == "" {
|
if meta["content-type"] == "" {
|
||||||
contentType := "application/octet-stream"
|
contentType := mimedb.TypeByExtension(path.Ext(object))
|
||||||
if objectExt := path.Ext(object); objectExt != "" {
|
|
||||||
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
||||||
if ok {
|
|
||||||
contentType = content.ContentType
|
|
||||||
}
|
|
||||||
}
|
|
||||||
meta["content-type"] = contentType
|
meta["content-type"] = contentType
|
||||||
}
|
}
|
||||||
xlMeta.Stat.ModTime = UTCNow()
|
xlMeta.Stat.ModTime = UTCNow()
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/minio/minio/cmd/logger"
|
"github.com/minio/minio/cmd/logger"
|
||||||
@ -785,11 +784,7 @@ func (xl xlObjects) putObject(ctx context.Context, bucket string, object string,
|
|||||||
|
|
||||||
// Guess content-type from the extension if possible.
|
// Guess content-type from the extension if possible.
|
||||||
if metadata["content-type"] == "" {
|
if metadata["content-type"] == "" {
|
||||||
if objectExt := path.Ext(object); objectExt != "" {
|
metadata["content-type"] = mimedb.TypeByExtension(path.Ext(object))
|
||||||
if content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]; ok {
|
|
||||||
metadata["content-type"] = content.ContentType
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if xl.isObject(bucket, object) {
|
if xl.isObject(bucket, object) {
|
||||||
|
@ -29,3 +29,17 @@ func TestMimeLookup(t *testing.T) {
|
|||||||
t.Fatalf("Invalid content type are found expected \"false\", got %t", compressible)
|
t.Fatalf("Invalid content type are found expected \"false\", got %t", compressible)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTypeByExtension(t *testing.T) {
|
||||||
|
var contentType string
|
||||||
|
// Test TypeByExtension.
|
||||||
|
contentType = TypeByExtension(".txt")
|
||||||
|
if contentType != "text/plain" {
|
||||||
|
t.Fatalf("Invalid content type are found expected \"text/plain\", got %s", contentType)
|
||||||
|
}
|
||||||
|
// Test non-existent type resolution
|
||||||
|
contentType = TypeByExtension(".abc")
|
||||||
|
if contentType != "application/octet-stream" {
|
||||||
|
t.Fatalf("Invalid content type are found expected \"application/octet-stream\", got %s", contentType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
33
pkg/mimedb/resolve-db.go
Normal file
33
pkg/mimedb/resolve-db.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* mime-db: Mime Database, (C) 2015, 2016, 2017, 2018 Minio, Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package mimedb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TypeByExtension resolves the extension to its respective content-type.
|
||||||
|
func TypeByExtension(ext string) string {
|
||||||
|
// Set default to "application/octet-stream".
|
||||||
|
var contentType = "application/octet-stream"
|
||||||
|
if ext != "" {
|
||||||
|
if content, ok := DB[strings.ToLower(strings.TrimPrefix(ext, "."))]; ok {
|
||||||
|
contentType = content.ContentType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return contentType
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user