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
1 changed files with 5 additions and 1 deletions

View File

@ -6,8 +6,10 @@ import (
"encoding/xml"
"errors"
"fmt"
"mime"
"net/http"
"os"
"path"
"strconv"
"strings"
"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)
return "application/octet-stream", nil
}
entry := node.DirEntry()
entry := node.DirEntry() // can be nil
switch x := entry.(type) {
case fs.Object:
return fs.MimeType(ctx, x), nil
case fs.Directory:
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)
return "application/octet-stream", nil