From 8c69260972d58919ef36e3aa9d23d59baf46776a Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 9 Oct 2022 19:29:55 +0800 Subject: [PATCH] fix(webdav): set mime by ext if it's empty --- internal/aria2/monitor.go | 7 ++----- internal/fs/util.go | 3 +-- pkg/utils/file.go | 10 ++++++++++ server/webdav/webdav.go | 3 +++ 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/internal/aria2/monitor.go b/internal/aria2/monitor.go index 4f11274b..da746285 100644 --- a/internal/aria2/monitor.go +++ b/internal/aria2/monitor.go @@ -2,7 +2,6 @@ package aria2 import ( "fmt" - "mime" "os" "path" "strconv" @@ -13,6 +12,7 @@ import ( "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/op" "github.com/alist-org/alist/v3/pkg/task" + "github.com/alist-org/alist/v3/pkg/utils" "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -147,10 +147,7 @@ func (m *Monitor) Complete() error { Func: func(tsk *task.Task[uint64]) error { defer wg.Done() size, _ := strconv.ParseInt(file.Length, 10, 64) - mimetype := mime.TypeByExtension(path.Ext(file.Path)) - if mimetype == "" { - mimetype = "application/octet-stream" - } + mimetype := utils.GetMimeType(file.Path) f, err := os.Open(file.Path) if err != nil { return errors.Wrapf(err, "failed to open file %s", file.Path) diff --git a/internal/fs/util.go b/internal/fs/util.go index 3f41641e..9e59e844 100644 --- a/internal/fs/util.go +++ b/internal/fs/util.go @@ -3,7 +3,6 @@ package fs import ( "fmt" "io" - "mime" "net/http" "os" stdpath "path" @@ -38,7 +37,7 @@ var httpClient = &http.Client{} func getFileStreamFromLink(file model.Obj, link *model.Link) (model.FileStreamer, error) { var rc io.ReadCloser - mimetype := mime.TypeByExtension(stdpath.Ext(file.GetName())) + mimetype := utils.GetMimeType(file.GetName()) if link.Data != nil { rc = link.Data } else if link.FilePath != nil { diff --git a/pkg/utils/file.go b/pkg/utils/file.go index 4e66d039..dbe64a02 100644 --- a/pkg/utils/file.go +++ b/pkg/utils/file.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "io/ioutil" + "mime" "os" "path" "path/filepath" @@ -136,3 +137,12 @@ func GetFileType(filename string) int { } return conf.UNKNOWN } + +func GetMimeType(name string) string { + ext := path.Ext(name) + m := mime.TypeByExtension(ext) + if m != "" { + return m + } + return "application/octet-stream" +} diff --git a/server/webdav/webdav.go b/server/webdav/webdav.go index 2c1ff1be..8a86dd1f 100644 --- a/server/webdav/webdav.go +++ b/server/webdav/webdav.go @@ -300,6 +300,9 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int, ReadCloser: r.Body, Mimetype: r.Header.Get("Content-Type"), } + if stream.Mimetype == "" { + stream.Mimetype = utils.GetMimeType(reqPath) + } err = fs.PutDirectly(ctx, path.Dir(reqPath), stream) // TODO(rost): Returning 405 Method Not Allowed might not be appropriate.