serve webdav: fix error: Expecting fs.Object or fs.Directory, got <nil>

Before this change rclone serve webdav would sometimes give this error

    Expecting fs.Object or fs.Directory, got <nil>

It turns out that when a file is being updated it doesn't have a
DirEntry and it is allowed to be <nil> so in this case we create the
mime type from the extension.

See: https://forum.rclone.org/t/webdav-union-of-onedrive-expecting-fs-object-or-fs-directory-got-nil/40298
This commit is contained in:
Nick Craig-Wood 2023-07-27 12:06:33 +01:00
parent 363da9aa82
commit 29b1751d0e

View file

@ -6,8 +6,10 @@ import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt" "fmt"
"mime"
"net/http" "net/http"
"os" "os"
"path"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -580,12 +582,14 @@ func (fi FileInfo) ContentType(ctx context.Context) (contentType string, err err
fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo) fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo)
return "application/octet-stream", nil return "application/octet-stream", nil
} }
entry := node.DirEntry() entry := node.DirEntry() // can be nil
switch x := entry.(type) { switch x := entry.(type) {
case fs.Object: case fs.Object:
return fs.MimeType(ctx, x), nil return fs.MimeType(ctx, x), nil
case fs.Directory: case fs.Directory:
return "inode/directory", nil return "inode/directory", nil
case nil:
return mime.TypeByExtension(path.Ext(node.Name())), nil
} }
fs.Errorf(fi, "Expecting fs.Object or fs.Directory, got %T", entry) fs.Errorf(fi, "Expecting fs.Object or fs.Directory, got %T", entry)
return "application/octet-stream", nil return "application/octet-stream", nil