media.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package media // import "miniflux.app/v2/internal/reader/media"
  4. import (
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. )
  9. var textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
  10. // Specs: https://www.rssboard.org/media-rss
  11. type MediaItemElement struct {
  12. MediaCategories MediaCategoryList `xml:"http://search.yahoo.com/mrss/ category"`
  13. MediaGroups []Group `xml:"http://search.yahoo.com/mrss/ group"`
  14. MediaContents []Content `xml:"http://search.yahoo.com/mrss/ content"`
  15. MediaThumbnails []Thumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"`
  16. MediaDescriptions DescriptionList `xml:"http://search.yahoo.com/mrss/ description"`
  17. MediaPeerLinks []PeerLink `xml:"http://search.yahoo.com/mrss/ peerLink"`
  18. }
  19. // AllMediaThumbnails returns all thumbnail elements merged together.
  20. func (e *MediaItemElement) AllMediaThumbnails() []Thumbnail {
  21. var items []Thumbnail
  22. items = append(items, e.MediaThumbnails...)
  23. for _, mediaGroup := range e.MediaGroups {
  24. items = append(items, mediaGroup.MediaThumbnails...)
  25. }
  26. return items
  27. }
  28. // AllMediaContents returns all content elements merged together.
  29. func (e *MediaItemElement) AllMediaContents() []Content {
  30. var items []Content
  31. items = append(items, e.MediaContents...)
  32. for _, mediaGroup := range e.MediaGroups {
  33. items = append(items, mediaGroup.MediaContents...)
  34. }
  35. return items
  36. }
  37. // AllMediaPeerLinks returns all peer link elements merged together.
  38. func (e *MediaItemElement) AllMediaPeerLinks() []PeerLink {
  39. var items []PeerLink
  40. items = append(items, e.MediaPeerLinks...)
  41. for _, mediaGroup := range e.MediaGroups {
  42. items = append(items, mediaGroup.MediaPeerLinks...)
  43. }
  44. return items
  45. }
  46. // FirstMediaDescription returns the first description element.
  47. func (e *MediaItemElement) FirstMediaDescription() string {
  48. description := e.MediaDescriptions.First()
  49. if description != "" {
  50. return description
  51. }
  52. for _, mediaGroup := range e.MediaGroups {
  53. description = mediaGroup.MediaDescriptions.First()
  54. if description != "" {
  55. return description
  56. }
  57. }
  58. return ""
  59. }
  60. // Group represents a XML element "media:group".
  61. type Group struct {
  62. MediaContents []Content `xml:"http://search.yahoo.com/mrss/ content"`
  63. MediaThumbnails []Thumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"`
  64. MediaDescriptions DescriptionList `xml:"http://search.yahoo.com/mrss/ description"`
  65. MediaPeerLinks []PeerLink `xml:"http://search.yahoo.com/mrss/ peerLink"`
  66. }
  67. // Content represents a XML element "media:content".
  68. type Content struct {
  69. URL string `xml:"url,attr"`
  70. Type string `xml:"type,attr"`
  71. FileSize string `xml:"fileSize,attr"`
  72. Medium string `xml:"medium,attr"`
  73. }
  74. // MimeType returns the attachment mime type.
  75. func (mc *Content) MimeType() string {
  76. switch {
  77. case mc.Type == "" && mc.Medium == "image":
  78. return "image/*"
  79. case mc.Type == "" && mc.Medium == "video":
  80. return "video/*"
  81. case mc.Type == "" && mc.Medium == "audio":
  82. return "audio/*"
  83. case mc.Type != "":
  84. return mc.Type
  85. default:
  86. return "application/octet-stream"
  87. }
  88. }
  89. // Size returns the attachment size.
  90. func (mc *Content) Size() int64 {
  91. if mc.FileSize == "" {
  92. return 0
  93. }
  94. size, _ := strconv.ParseInt(mc.FileSize, 10, 0)
  95. return size
  96. }
  97. // Thumbnail represents a XML element "media:thumbnail".
  98. type Thumbnail struct {
  99. URL string `xml:"url,attr"`
  100. }
  101. // MimeType returns the attachment mime type.
  102. func (t *Thumbnail) MimeType() string {
  103. return "image/*"
  104. }
  105. // Size returns the attachment size.
  106. func (t *Thumbnail) Size() int64 {
  107. return 0
  108. }
  109. // PeerLink represents a XML element "media:peerLink".
  110. type PeerLink struct {
  111. URL string `xml:"href,attr"`
  112. Type string `xml:"type,attr"`
  113. }
  114. // MimeType returns the attachment mime type.
  115. func (p *PeerLink) MimeType() string {
  116. if p.Type != "" {
  117. return p.Type
  118. }
  119. return "application/octet-stream"
  120. }
  121. // Size returns the attachment size.
  122. func (p *PeerLink) Size() int64 {
  123. return 0
  124. }
  125. // Description represents a XML element "media:description".
  126. type Description struct {
  127. Type string `xml:"type,attr"`
  128. Description string `xml:",chardata"`
  129. }
  130. // HTML returns the description as HTML.
  131. func (d *Description) HTML() string {
  132. if d.Type == "html" {
  133. return d.Description
  134. }
  135. content := strings.ReplaceAll(d.Description, "\n", "<br>")
  136. return textLinkRegex.ReplaceAllString(content, `<a href="${1}">${1}</a>`)
  137. }
  138. // DescriptionList represents a list of "media:description" XML elements.
  139. type DescriptionList []Description
  140. // First returns the first non-empty description.
  141. func (dl DescriptionList) First() string {
  142. for _, description := range dl {
  143. contents := description.HTML()
  144. if contents != "" {
  145. return contents
  146. }
  147. }
  148. return ""
  149. }
  150. type MediaCategoryList []MediaCategory
  151. func (mcl MediaCategoryList) Labels() []string {
  152. var labels []string
  153. for _, category := range mcl {
  154. label := strings.TrimSpace(category.Label)
  155. if label != "" {
  156. labels = append(labels, label)
  157. }
  158. }
  159. return labels
  160. }
  161. type MediaCategory struct {
  162. Label string `xml:"label,attr"`
  163. }