Skip to content

Commit

Permalink
fix(webdav): set mime by ext if it's empty
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Oct 9, 2022
1 parent 30f992c commit 8c69260
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
7 changes: 2 additions & 5 deletions internal/aria2/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aria2

import (
"fmt"
"mime"
"os"
"path"
"strconv"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions internal/fs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fs
import (
"fmt"
"io"
"mime"
"net/http"
"os"
stdpath "path"
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/ioutil"
"mime"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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"
}
3 changes: 3 additions & 0 deletions server/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8c69260

Please sign in to comment.