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 ( import (
"fmt" "fmt"
"mime"
"os" "os"
"path" "path"
"strconv" "strconv"
@ -13,6 +12,7 @@ import (
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op" "github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/task" "github.com/alist-org/alist/v3/pkg/task"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -147,10 +147,7 @@ func (m *Monitor) Complete() error {
Func: func(tsk *task.Task[uint64]) error { Func: func(tsk *task.Task[uint64]) error {
defer wg.Done() defer wg.Done()
size, _ := strconv.ParseInt(file.Length, 10, 64) size, _ := strconv.ParseInt(file.Length, 10, 64)
mimetype := mime.TypeByExtension(path.Ext(file.Path)) mimetype := utils.GetMimeType(file.Path)
if mimetype == "" {
mimetype = "application/octet-stream"
}
f, err := os.Open(file.Path) f, err := os.Open(file.Path)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to open file %s", file.Path) return errors.Wrapf(err, "failed to open file %s", file.Path)

View File

@ -3,7 +3,6 @@ package fs
import ( import (
"fmt" "fmt"
"io" "io"
"mime"
"net/http" "net/http"
"os" "os"
stdpath "path" stdpath "path"
@ -38,7 +37,7 @@ var httpClient = &http.Client{}
func getFileStreamFromLink(file model.Obj, link *model.Link) (model.FileStreamer, error) { func getFileStreamFromLink(file model.Obj, link *model.Link) (model.FileStreamer, error) {
var rc io.ReadCloser var rc io.ReadCloser
mimetype := mime.TypeByExtension(stdpath.Ext(file.GetName())) mimetype := utils.GetMimeType(file.GetName())
if link.Data != nil { if link.Data != nil {
rc = link.Data rc = link.Data
} else if link.FilePath != nil { } else if link.FilePath != nil {

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"mime"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -136,3 +137,12 @@ func GetFileType(filename string) int {
} }
return conf.UNKNOWN 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, ReadCloser: r.Body,
Mimetype: r.Header.Get("Content-Type"), Mimetype: r.Header.Get("Content-Type"),
} }
if stream.Mimetype == "" {
stream.Mimetype = utils.GetMimeType(reqPath)
}
err = fs.PutDirectly(ctx, path.Dir(reqPath), stream) err = fs.PutDirectly(ctx, path.Dir(reqPath), stream)
// TODO(rost): Returning 405 Method Not Allowed might not be appropriate. // TODO(rost): Returning 405 Method Not Allowed might not be appropriate.