From c7e7b6229f16edb8a0adbcf85d21bed9e44d8109 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 27 Mar 2019 14:25:18 -0700 Subject: [PATCH] detect mime type --- weed/filesys/filehandle.go | 12 ++++++++++-- weed/server/filer_server_handlers_write.go | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/weed/filesys/filehandle.go b/weed/filesys/filehandle.go index 2c2e041e7..e87e0608e 100644 --- a/weed/filesys/filehandle.go +++ b/weed/filesys/filehandle.go @@ -8,10 +8,12 @@ import ( "github.com/chrislusf/seaweedfs/weed/operation" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/util" + "github.com/gabriel-vasile/mimetype" "github.com/seaweedfs/fuse" "github.com/seaweedfs/fuse/fs" "google.golang.org/grpc" - "net/http" + "mime" + "path" "strings" "sync" "time" @@ -154,7 +156,13 @@ func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *f resp.Size = len(req.Data) if req.Offset == 0 { - fh.contentType = http.DetectContentType(req.Data) + // detect mime type + var possibleExt string + fh.contentType, possibleExt = mimetype.Detect(req.Data) + if ext := path.Ext(fh.f.Name); ext != possibleExt { + fh.contentType = mime.TypeByExtension(ext) + } + fh.dirtyMetadata = true } diff --git a/weed/server/filer_server_handlers_write.go b/weed/server/filer_server_handlers_write.go index f20212cc2..cb30ff4c0 100644 --- a/weed/server/filer_server_handlers_write.go +++ b/weed/server/filer_server_handlers_write.go @@ -5,9 +5,11 @@ import ( "encoding/json" "errors" "io/ioutil" + "mime" "net/http" "net/url" "os" + filenamePath "path" "strconv" "strings" "time" @@ -195,6 +197,9 @@ func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) { ETag: etag, }}, } + if ext := filenamePath.Ext(path); ext != "" { + entry.Attr.Mime = mime.TypeByExtension(ext) + } // glog.V(4).Infof("saving %s => %+v", path, entry) if db_err := fs.filer.CreateEntry(ctx, entry); db_err != nil { fs.filer.DeleteFileByFileId(fileId)