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