From 39ae7c7ac0948770619ae0c2870afe8423b07ffa Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 22 Aug 2019 22:20:09 +0100 Subject: [PATCH] serve dlna: fix missing mime types on Android causing missing videos Before this fix serve dlna was only using the built in database of mime types to look up the mime types of files. On Android (and possibly other systems) this is very small. The symptoms of this problem was serve dlna only listing images and not videos. After this fix we use the backend's idea of the mime type if possible which will be more accurate. Fixes #3475 --- cmd/serve/dlna/cds.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cmd/serve/dlna/cds.go b/cmd/serve/dlna/cds.go index f5a2d45cb..bb1aae7d9 100644 --- a/cmd/serve/dlna/cds.go +++ b/cmd/serve/dlna/cds.go @@ -1,6 +1,7 @@ package dlna import ( + "context" "encoding/xml" "fmt" "log" @@ -33,7 +34,7 @@ var mediaMimeTypeRegexp = regexp.MustCompile("^(video|audio|image)/") // Turns the given entry and DMS host into a UPnP object. A nil object is // returned if the entry is not of interest. -func (cds *contentDirectoryService) cdsObjectToUpnpavObject(cdsObject object, fileInfo os.FileInfo, host string) (ret interface{}, err error) { +func (cds *contentDirectoryService) cdsObjectToUpnpavObject(cdsObject object, fileInfo vfs.Node, host string) (ret interface{}, err error) { obj := upnpav.Object{ ID: cdsObject.ID(), Restricted: 1, @@ -51,7 +52,15 @@ func (cds *contentDirectoryService) cdsObjectToUpnpavObject(cdsObject object, fi return } - mimeType := fs.MimeTypeFromName(fileInfo.Name()) + // Read the mime type from the fs.Object if possible, + // otherwise fall back to working out what it is from the file path. + var mimeType string + if o, ok := fileInfo.DirEntry().(fs.Object); ok { + mimeType = fs.MimeType(context.TODO(), o) + } else { + mimeType = fs.MimeTypeFromName(fileInfo.Name()) + } + mediaType := mediaMimeTypeRegexp.FindStringSubmatch(mimeType) if mediaType == nil { return