handler.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opml // import "miniflux.app/v2/internal/reader/opml"
  4. import (
  5. "fmt"
  6. "io"
  7. "miniflux.app/v2/internal/locale"
  8. "miniflux.app/v2/internal/model"
  9. "miniflux.app/v2/internal/storage"
  10. "miniflux.app/v2/internal/urllib"
  11. "miniflux.app/v2/internal/validator"
  12. )
  13. // Handler handles the logic for OPML import/export.
  14. type Handler struct {
  15. store *storage.Storage
  16. }
  17. // Export exports user feeds to OPML.
  18. func (h *Handler) Export(userID int64) (string, error) {
  19. feeds, err := h.store.Feeds(userID)
  20. if err != nil {
  21. return "", err
  22. }
  23. subscriptions := make([]subcription, 0, len(feeds))
  24. for _, feed := range feeds {
  25. subscriptions = append(subscriptions, subcription{
  26. Title: feed.Title,
  27. FeedURL: feed.FeedURL,
  28. SiteURL: feed.SiteURL,
  29. Description: feed.Description,
  30. CategoryName: feed.Category.Title,
  31. ScraperRules: feed.ScraperRules,
  32. RewriteRules: feed.RewriteRules,
  33. UrlRewriteRules: feed.UrlRewriteRules,
  34. BlocklistRules: feed.BlocklistRules,
  35. KeeplistRules: feed.KeeplistRules,
  36. BlockFilterEntryRules: feed.BlockFilterEntryRules,
  37. KeepFilterEntryRules: feed.KeepFilterEntryRules,
  38. UserAgent: feed.UserAgent,
  39. Crawler: feed.Crawler,
  40. IgnoreHTTPCache: feed.IgnoreHTTPCache,
  41. FetchViaProxy: feed.FetchViaProxy,
  42. Disabled: feed.Disabled,
  43. NoMediaPlayer: feed.NoMediaPlayer,
  44. HideGlobally: feed.HideGlobally,
  45. AllowSelfSignedCertificates: feed.AllowSelfSignedCertificates,
  46. DisableHTTP2: feed.DisableHTTP2,
  47. IgnoreEntryUpdates: feed.IgnoreEntryUpdates,
  48. })
  49. }
  50. return serialize(subscriptions), nil
  51. }
  52. // ImportResult summarizes an OPML import.
  53. type ImportResult struct {
  54. Created int
  55. Existing int
  56. Skipped []SkippedFeed
  57. }
  58. // SkippedFeed is a feed of the OPML document that was not imported because it is invalid.
  59. type SkippedFeed struct {
  60. FeedURL string
  61. Reason *locale.LocalizedError
  62. }
  63. // Import creates the feeds of an OPML document, skipping the invalid ones.
  64. func (h *Handler) Import(userID int64, data io.Reader) (*ImportResult, error) {
  65. subscriptions, err := parse(data)
  66. if err != nil {
  67. return nil, err
  68. }
  69. result := &ImportResult{}
  70. for _, subscription := range subscriptions {
  71. if h.store.FeedURLExists(userID, subscription.FeedURL) {
  72. result.Existing++
  73. continue
  74. }
  75. // Validate before resolving the category, so that a skipped feed does not leave an empty category behind.
  76. if validationErr := validateSubscription(subscription); validationErr != nil {
  77. result.Skipped = append(result.Skipped, SkippedFeed{FeedURL: subscription.FeedURL, Reason: validationErr})
  78. continue
  79. }
  80. category, err := h.resolveCategory(userID, subscription.CategoryName)
  81. if err != nil {
  82. return nil, err
  83. }
  84. feed := &model.Feed{
  85. UserID: userID,
  86. Title: subscription.Title,
  87. FeedURL: subscription.FeedURL,
  88. SiteURL: subscription.SiteURL,
  89. Description: subscription.Description,
  90. Category: category,
  91. }
  92. applySubscriptionSettings(feed, subscription)
  93. if err := h.store.CreateFeed(feed); err != nil {
  94. return nil, fmt.Errorf(`opml: unable to create this feed: %q`, subscription.FeedURL)
  95. }
  96. result.Created++
  97. }
  98. return result, nil
  99. }
  100. func (h *Handler) resolveCategory(userID int64, categoryName string) (*model.Category, error) {
  101. if categoryName == "" {
  102. category, err := h.store.FirstCategory(userID)
  103. if err != nil {
  104. return nil, fmt.Errorf("opml: unable to find first category: %w", err)
  105. }
  106. if category == nil {
  107. return nil, storage.ErrNoCategory
  108. }
  109. return category, nil
  110. }
  111. category, err := h.store.CategoryByTitle(userID, categoryName)
  112. if err != nil {
  113. return nil, fmt.Errorf("opml: unable to search category by title: %w", err)
  114. }
  115. if category == nil {
  116. category, err = h.store.CreateCategory(userID, &model.CategoryCreationRequest{Title: categoryName})
  117. if err != nil {
  118. return nil, fmt.Errorf(`opml: unable to create this category: %q`, categoryName)
  119. }
  120. }
  121. return category, nil
  122. }
  123. func applySubscriptionSettings(feed *model.Feed, s subcription) {
  124. feed.ScraperRules = s.ScraperRules
  125. feed.RewriteRules = s.RewriteRules
  126. feed.UrlRewriteRules = s.UrlRewriteRules
  127. feed.BlocklistRules = s.BlocklistRules
  128. feed.KeeplistRules = s.KeeplistRules
  129. feed.BlockFilterEntryRules = s.BlockFilterEntryRules
  130. feed.KeepFilterEntryRules = s.KeepFilterEntryRules
  131. feed.UserAgent = s.UserAgent
  132. feed.Crawler = s.Crawler
  133. feed.IgnoreHTTPCache = s.IgnoreHTTPCache
  134. feed.FetchViaProxy = s.FetchViaProxy
  135. feed.Disabled = s.Disabled
  136. feed.NoMediaPlayer = s.NoMediaPlayer
  137. feed.HideGlobally = s.HideGlobally
  138. feed.AllowSelfSignedCertificates = s.AllowSelfSignedCertificates
  139. feed.DisableHTTP2 = s.DisableHTTP2
  140. feed.IgnoreEntryUpdates = s.IgnoreEntryUpdates
  141. }
  142. func validateSubscription(s subcription) *locale.LocalizedError {
  143. if !urllib.IsAbsoluteURL(s.FeedURL) {
  144. return locale.NewLocalizedError("error.invalid_feed_url")
  145. }
  146. feedCreationRequest := &model.FeedCreationRequest{
  147. FeedURL: s.FeedURL,
  148. UserAgent: s.UserAgent,
  149. Crawler: s.Crawler,
  150. IgnoreEntryUpdates: s.IgnoreEntryUpdates,
  151. Disabled: s.Disabled,
  152. NoMediaPlayer: s.NoMediaPlayer,
  153. IgnoreHTTPCache: s.IgnoreHTTPCache,
  154. AllowSelfSignedCertificates: s.AllowSelfSignedCertificates,
  155. FetchViaProxy: s.FetchViaProxy,
  156. HideGlobally: s.HideGlobally,
  157. DisableHTTP2: s.DisableHTTP2,
  158. ScraperRules: s.ScraperRules,
  159. RewriteRules: s.RewriteRules,
  160. BlocklistRules: s.BlocklistRules,
  161. KeeplistRules: s.KeeplistRules,
  162. BlockFilterEntryRules: s.BlockFilterEntryRules,
  163. KeepFilterEntryRules: s.KeepFilterEntryRules,
  164. UrlRewriteRules: s.UrlRewriteRules,
  165. }
  166. return validator.ValidateFeedSettings(feedCreationRequest)
  167. }
  168. // NewHandler creates a new handler for OPML files.
  169. func NewHandler(store *storage.Storage) *Handler {
  170. return &Handler{store: store}
  171. }