media.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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?://[^\s]+)`)
  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. items := make([]Thumbnail, 0, len(e.MediaThumbnails)+len(e.MediaGroups))
  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. items := make([]Content, 0, len(e.MediaContents)+len(e.MediaGroups))
  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. items := make([]PeerLink, 0, len(e.MediaPeerLinks)+len(e.MediaGroups))
  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. if mc.Type != "" {
  77. return mc.Type
  78. }
  79. switch mc.Medium {
  80. case "image":
  81. return "image/*"
  82. case "video":
  83. return "video/*"
  84. case "audio":
  85. return "audio/*"
  86. default:
  87. return "application/octet-stream"
  88. }
  89. }
  90. // Size returns the attachment size.
  91. func (mc *Content) Size() int64 {
  92. size, _ := strconv.ParseInt(mc.FileSize, 10, 0)
  93. return size
  94. }
  95. // Thumbnail represents a XML element "media:thumbnail".
  96. type Thumbnail struct {
  97. URL string `xml:"url,attr"`
  98. }
  99. // MimeType returns the attachment mime type.
  100. func (t *Thumbnail) MimeType() string {
  101. return "image/*"
  102. }
  103. // Size returns the attachment size.
  104. func (t *Thumbnail) Size() int64 {
  105. return 0
  106. }
  107. // PeerLink represents a XML element "media:peerLink".
  108. type PeerLink struct {
  109. URL string `xml:"href,attr"`
  110. Type string `xml:"type,attr"`
  111. }
  112. // MimeType returns the attachment mime type.
  113. func (p *PeerLink) MimeType() string {
  114. if p.Type != "" {
  115. return p.Type
  116. }
  117. return "application/octet-stream"
  118. }
  119. // Size returns the attachment size.
  120. func (p *PeerLink) Size() int64 {
  121. return 0
  122. }
  123. // Description represents a XML element "media:description".
  124. type Description struct {
  125. Type string `xml:"type,attr"`
  126. Description string `xml:",chardata"`
  127. }
  128. // HTML returns the description as HTML.
  129. func (d *Description) HTML() string {
  130. if d.Type == "html" {
  131. return d.Description
  132. }
  133. content := textLinkRegex.ReplaceAllString(d.Description, `<a href="${1}">${1}</a>`)
  134. return strings.ReplaceAll(content, "\n", "<br>")
  135. }
  136. // DescriptionList represents a list of "media:description" XML elements.
  137. type DescriptionList []Description
  138. // First returns the first non-empty description.
  139. func (dl DescriptionList) First() string {
  140. for _, description := range dl {
  141. contents := description.HTML()
  142. if contents != "" {
  143. return contents
  144. }
  145. }
  146. return ""
  147. }
  148. type MediaCategoryList []MediaCategory
  149. func (mcl MediaCategoryList) Labels() []string {
  150. var labels []string
  151. for _, category := range mcl {
  152. label := strings.TrimSpace(category.Label)
  153. if label != "" {
  154. labels = append(labels, label)
  155. }
  156. }
  157. return labels
  158. }
  159. type MediaCategory struct {
  160. Label string `xml:"label,attr"`
  161. }