From f4d822626eaba17a4240d5a48b96819a23ba6823 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 12 Sep 2022 12:37:47 +0100 Subject: [PATCH] serve webdav: fix incorrect Content-Type immediately after upload Before this change, if the Content-Type for an object was read immediately after upload (before the object had been uploaded to the backing store) then the Content-Type would be returned incorrectly. This error would be more likely with `--vfs-cache-mode full` and `writes` but may have been possible with the other `--vfs-cache-mode`s. This fixes the problem by always returning a sensible guess at the content type - the same guess we would use for uploading the object. Fixes #6433 --- cmd/serve/webdav/webdav.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/cmd/serve/webdav/webdav.go b/cmd/serve/webdav/webdav.go index bfd0eda31..b60c7be2a 100644 --- a/cmd/serve/webdav/webdav.go +++ b/cmd/serve/webdav/webdav.go @@ -377,18 +377,21 @@ func (fi FileInfo) ETag(ctx context.Context) (etag string, err error) { // ContentType returns a content type for the FileInfo func (fi FileInfo) ContentType(ctx context.Context) (contentType string, err error) { // defer log.Trace(fi, "")("etag=%q, err=%v", &contentType, &err) - node, ok := (fi.FileInfo).(vfs.Node) - if !ok { - fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo) - return "application/octet-stream", nil - } - entry := node.DirEntry() - switch x := entry.(type) { - case fs.Object: - return fs.MimeType(ctx, x), nil - case fs.Directory: + if fi.IsDir() { return "inode/directory", nil } - fs.Errorf(fi, "Expecting fs.Object or fs.Directory, got %T", entry) - return "application/octet-stream", nil + if node, ok := (fi.FileInfo).(vfs.Node); !ok { + fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo) + } else { + entry := node.DirEntry() + switch x := entry.(type) { + case nil: + // object hasn't been uploaded yet if entry is nil + case fs.Object: + return fs.MimeType(ctx, x), nil + default: + fs.Errorf(fi, "Expecting fs.Object or nil, got %T", entry) + } + } + return fs.MimeTypeFromName(fi.Name()), nil }