Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

better detect file mime type

Chris Lu9cacaf5eca3d
diff --git a/weed/filesys/filehandle.go b/weed/filesys/filehandle.go
index 935f0b3146..d2983d53fc 100644
--- a/weed/filesys/filehandle.go
+++ b/weed/filesys/filehandle.go
@@ -4,12 +4,9 @@ import (
"context"
"fmt"
"math"
- "mime"
- "path"
+ "net/http"
"time"
- "github.com/gabriel-vasile/mimetype"
-
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
@@ -130,12 +127,7 @@ func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *f
if req.Offset == 0 {
// detect mime type
- detectedMIME := mimetype.Detect(data)
- fh.contentType = detectedMIME.String()
- if ext := path.Ext(fh.f.Name); ext != detectedMIME.Extension() {
- fh.contentType = mime.TypeByExtension(ext)
- }
-
+ fh.contentType = http.DetectContentType(data)
fh.dirtyMetadata = true
}
diff --git a/weed/operation/upload_content.go b/weed/operation/upload_content.go
index d4a334f050..5b0441ff99 100644
--- a/weed/operation/upload_content.go
+++ b/weed/operation/upload_content.go
@@ -77,6 +77,12 @@ func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, i
contentIsGzipped := isInputGzipped
shouldGzipNow := false
if !isInputGzipped {
+ if mtype == "" {
+ mtype = http.DetectContentType(data)
+ if mtype == "application/octet-stream" {
+ mtype = ""
+ }
+ }
if shouldBeZipped, iAmSure := util.IsGzippableFileType(filepath.Base(filename), mtype); iAmSure && shouldBeZipped {
shouldGzipNow = true
} else if !iAmSure && mtype == "" && len(data) > 128 {