Browse Source

pub: fall back to link type in determining mime type (e.g. for attachments)

Benton Edmondson 1 year ago
parent
commit
752cfacfe3
2 changed files with 11 additions and 4 deletions
  1. 10 3
      pub/link.go
  2. 1 1
      pub/post.go

+ 10 - 3
pub/link.go

@@ -7,6 +7,7 @@ import (
 	"servitor/mime"
 	"servitor/object"
 	"net/url"
+	"strings"
 )
 
 type Link struct {
@@ -152,11 +153,17 @@ func (l *Link) SelectWithDefaultMediaType(defaultMediaType *mime.MediaType) (str
 	if l.uriErr != nil {
 		return "", nil, false
 	}
+
 	/* I suppress this error here because it is shown in the alt text */
-	if l.mediaTypeErr != nil {
-		return l.uri.String(), defaultMediaType, true
+	if l.mediaTypeErr == nil {
+		return l.uri.String(), l.mediaType, true
+	}
+
+	if l.kind == "Audio" || l.kind == "Video" || l.kind == "Image" {
+		return l.uri.String(), mime.UnknownSubtype(strings.ToLower(l.kind)), true
 	}
-	return l.uri.String(), l.mediaType, true
+
+	return l.uri.String(), defaultMediaType, true
 }
 
 func SelectFirstLink(links []*Link) (*Link, error) {

+ 1 - 1
pub/post.go

@@ -72,7 +72,7 @@ func NewPostFromObject(o object.Object, id *url.URL) (*Post, error) {
 	p.edited, p.editedErr = o.GetTime("updated")
 	p.parent, p.parentErr = o.GetAny("inReplyTo")
 
-	if p.kind == "Image" || p.kind == "Audio" || p.kind == "Video" {
+	if p.kind == "Audio" || p.kind == "Video" || p.kind == "Image" {
 		p.media, p.mediaErr = getBestLinkShorthand(o, "url", strings.ToLower(p.kind))
 	} else {
 		p.media, p.mediaErr = getFirstLinkShorthand(o, "url")