detect mime type

This commit is contained in:
Chris Lu 2019-03-27 14:25:18 -07:00
parent 65757ae2fd
commit c7e7b6229f
2 changed files with 15 additions and 2 deletions

View file

@ -8,10 +8,12 @@ import (
"github.com/chrislusf/seaweedfs/weed/operation" "github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"github.com/gabriel-vasile/mimetype"
"github.com/seaweedfs/fuse" "github.com/seaweedfs/fuse"
"github.com/seaweedfs/fuse/fs" "github.com/seaweedfs/fuse/fs"
"google.golang.org/grpc" "google.golang.org/grpc"
"net/http" "mime"
"path"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -154,7 +156,13 @@ func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *f
resp.Size = len(req.Data) resp.Size = len(req.Data)
if req.Offset == 0 { 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 fh.dirtyMetadata = true
} }

View file

@ -5,9 +5,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"mime"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
filenamePath "path"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -195,6 +197,9 @@ func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
ETag: etag, ETag: etag,
}}, }},
} }
if ext := filenamePath.Ext(path); ext != "" {
entry.Attr.Mime = mime.TypeByExtension(ext)
}
// glog.V(4).Infof("saving %s => %+v", path, entry) // glog.V(4).Infof("saving %s => %+v", path, entry)
if db_err := fs.filer.CreateEntry(ctx, entry); db_err != nil { if db_err := fs.filer.CreateEntry(ctx, entry); db_err != nil {
fs.filer.DeleteFileByFileId(fileId) fs.filer.DeleteFileByFileId(fileId)