media.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2019 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package media // import "miniflux.app/reader/media"
  5. import (
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. )
  10. var textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
  11. // Element represents XML media elements.
  12. type Element struct {
  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 *Element) 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 *Element) 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 *Element) 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 *Element) 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 == "" && mc.Medium == "video":
  84. return "video/*"
  85. case mc.Type != "":
  86. return mc.Type
  87. default:
  88. return "application/octet-stream"
  89. }
  90. }
  91. // Size returns the attachment size.
  92. func (mc *Content) Size() int64 {
  93. if mc.FileSize == "" {
  94. return 0
  95. }
  96. size, _ := strconv.ParseInt(mc.FileSize, 10, 0)
  97. return size
  98. }
  99. // Thumbnail represents a XML element "media:thumbnail".
  100. type Thumbnail struct {
  101. URL string `xml:"url,attr"`
  102. }
  103. // MimeType returns the attachment mime type.
  104. func (t *Thumbnail) MimeType() string {
  105. return "image/*"
  106. }
  107. // Size returns the attachment size.
  108. func (t *Thumbnail) Size() int64 {
  109. return 0
  110. }
  111. // PeerLink represents a XML element "media:peerLink".
  112. type PeerLink struct {
  113. URL string `xml:"href,attr"`
  114. Type string `xml:"type,attr"`
  115. }
  116. // MimeType returns the attachment mime type.
  117. func (p *PeerLink) MimeType() string {
  118. if p.Type != "" {
  119. return p.Type
  120. }
  121. return "application/octet-stream"
  122. }
  123. // Size returns the attachment size.
  124. func (p *PeerLink) Size() int64 {
  125. return 0
  126. }
  127. // Description represents a XML element "media:description".
  128. type Description struct {
  129. Type string `xml:"type,attr"`
  130. Description string `xml:",chardata"`
  131. }
  132. // HTML returns the description as HTML.
  133. func (d *Description) HTML() string {
  134. if d.Type == "html" {
  135. return d.Description
  136. }
  137. content := strings.Replace(d.Description, "\n", "<br>", -1)
  138. return textLinkRegex.ReplaceAllString(content, `<a href="${1}">${1}</a>`)
  139. }
  140. // DescriptionList represents a list of "media:description" XML elements.
  141. type DescriptionList []Description
  142. // First returns the first non-empty description.
  143. func (dl DescriptionList) First() string {
  144. for _, description := range dl {
  145. contents := description.HTML()
  146. if contents != "" {
  147. return contents
  148. }
  149. }
  150. return ""
  151. }