fix(webdav): set mime by ext if it's empty

pull/2046/head
Noah Hsu 2022-10-09 19:29:55 +08:00
parent 30f992c6a8
commit 8c69260972
4 changed files with 16 additions and 7 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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"
}

View File

@ -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.