handler.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2017 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 handler // import "miniflux.app/reader/handler"
  5. import (
  6. "fmt"
  7. "time"
  8. "miniflux.app/config"
  9. "miniflux.app/errors"
  10. "miniflux.app/http/client"
  11. "miniflux.app/locale"
  12. "miniflux.app/logger"
  13. "miniflux.app/model"
  14. "miniflux.app/reader/browser"
  15. "miniflux.app/reader/icon"
  16. "miniflux.app/reader/parser"
  17. "miniflux.app/reader/processor"
  18. "miniflux.app/storage"
  19. "miniflux.app/timer"
  20. )
  21. var (
  22. errDuplicate = "This feed already exists (%s)"
  23. errNotFound = "Feed %d not found"
  24. errCategoryNotFound = "Category not found for this user"
  25. )
  26. // CreateFeed fetch, parse and store a new feed.
  27. func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequest) (*model.Feed, error) {
  28. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[CreateFeed] FeedURL=%s", feedCreationRequest.FeedURL))
  29. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  30. return nil, errors.NewLocalizedError(errCategoryNotFound)
  31. }
  32. request := client.NewClientWithConfig(feedCreationRequest.FeedURL, config.Opts)
  33. request.WithCredentials(feedCreationRequest.Username, feedCreationRequest.Password)
  34. request.WithUserAgent(feedCreationRequest.UserAgent)
  35. request.WithCookie(feedCreationRequest.Cookie)
  36. request.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  37. if feedCreationRequest.FetchViaProxy {
  38. request.WithProxy()
  39. }
  40. response, requestErr := browser.Exec(request)
  41. if requestErr != nil {
  42. return nil, requestErr
  43. }
  44. if store.FeedURLExists(userID, response.EffectiveURL) {
  45. return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  46. }
  47. subscription, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  48. if parseErr != nil {
  49. return nil, parseErr
  50. }
  51. subscription.UserID = userID
  52. subscription.UserAgent = feedCreationRequest.UserAgent
  53. subscription.Cookie = feedCreationRequest.Cookie
  54. subscription.Username = feedCreationRequest.Username
  55. subscription.Password = feedCreationRequest.Password
  56. subscription.Crawler = feedCreationRequest.Crawler
  57. subscription.Disabled = feedCreationRequest.Disabled
  58. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  59. subscription.AllowSelfSignedCertificates = feedCreationRequest.AllowSelfSignedCertificates
  60. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  61. subscription.ScraperRules = feedCreationRequest.ScraperRules
  62. subscription.RewriteRules = feedCreationRequest.RewriteRules
  63. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  64. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  65. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  66. subscription.WithClientResponse(response)
  67. subscription.CheckedNow()
  68. processor.ProcessFeedEntries(store, subscription)
  69. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  70. return nil, storeErr
  71. }
  72. logger.Debug("[CreateFeed] Feed saved with ID: %d", subscription.ID)
  73. checkFeedIcon(
  74. store,
  75. subscription.ID,
  76. subscription.SiteURL,
  77. feedCreationRequest.FetchViaProxy,
  78. feedCreationRequest.AllowSelfSignedCertificates,
  79. )
  80. return subscription, nil
  81. }
  82. // RefreshFeed refreshes a feed.
  83. func RefreshFeed(store *storage.Storage, userID, feedID int64) error {
  84. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[RefreshFeed] feedID=%d", feedID))
  85. userLanguage := store.UserLanguage(userID)
  86. printer := locale.NewPrinter(userLanguage)
  87. originalFeed, storeErr := store.FeedByID(userID, feedID)
  88. if storeErr != nil {
  89. return storeErr
  90. }
  91. if originalFeed == nil {
  92. return errors.NewLocalizedError(errNotFound, feedID)
  93. }
  94. weeklyEntryCount := 0
  95. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  96. var weeklyCountErr error
  97. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  98. if weeklyCountErr != nil {
  99. return weeklyCountErr
  100. }
  101. }
  102. originalFeed.CheckedNow()
  103. originalFeed.ScheduleNextCheck(weeklyEntryCount)
  104. request := client.NewClientWithConfig(originalFeed.FeedURL, config.Opts)
  105. request.WithCredentials(originalFeed.Username, originalFeed.Password)
  106. request.WithUserAgent(originalFeed.UserAgent)
  107. request.WithCookie(originalFeed.Cookie)
  108. request.AllowSelfSignedCertificates = originalFeed.AllowSelfSignedCertificates
  109. if !originalFeed.IgnoreHTTPCache {
  110. request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
  111. }
  112. if originalFeed.FetchViaProxy {
  113. request.WithProxy()
  114. }
  115. response, requestErr := browser.Exec(request)
  116. if requestErr != nil {
  117. originalFeed.WithError(requestErr.Localize(printer))
  118. store.UpdateFeedError(originalFeed)
  119. return requestErr
  120. }
  121. if store.AnotherFeedURLExists(userID, originalFeed.ID, response.EffectiveURL) {
  122. storeErr := errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  123. originalFeed.WithError(storeErr.Error())
  124. store.UpdateFeedError(originalFeed)
  125. return storeErr
  126. }
  127. if originalFeed.IgnoreHTTPCache || response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  128. logger.Debug("[RefreshFeed] Feed #%d has been modified", feedID)
  129. updatedFeed, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  130. if parseErr != nil {
  131. originalFeed.WithError(parseErr.Localize(printer))
  132. store.UpdateFeedError(originalFeed)
  133. return parseErr
  134. }
  135. originalFeed.Entries = updatedFeed.Entries
  136. processor.ProcessFeedEntries(store, originalFeed)
  137. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries).
  138. if storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, !originalFeed.Crawler); storeErr != nil {
  139. originalFeed.WithError(storeErr.Error())
  140. store.UpdateFeedError(originalFeed)
  141. return storeErr
  142. }
  143. // We update caching headers only if the feed has been modified,
  144. // because some websites don't return the same headers when replying with a 304.
  145. originalFeed.WithClientResponse(response)
  146. checkFeedIcon(
  147. store,
  148. originalFeed.ID,
  149. originalFeed.SiteURL,
  150. originalFeed.FetchViaProxy,
  151. originalFeed.AllowSelfSignedCertificates,
  152. )
  153. } else {
  154. logger.Debug("[RefreshFeed] Feed #%d not modified", feedID)
  155. }
  156. originalFeed.ResetErrorCounter()
  157. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  158. originalFeed.WithError(storeErr.Error())
  159. store.UpdateFeedError(originalFeed)
  160. return storeErr
  161. }
  162. return nil
  163. }
  164. func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL string, fetchViaProxy, allowSelfSignedCertificates bool) {
  165. if !store.HasIcon(feedID) {
  166. icon, err := icon.FindIcon(websiteURL, fetchViaProxy, allowSelfSignedCertificates)
  167. if err != nil {
  168. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  169. } else if icon == nil {
  170. logger.Debug(`[CheckFeedIcon] No icon found (feedID=%d websiteURL=%s)`, feedID, websiteURL)
  171. } else {
  172. if err := store.CreateFeedIcon(feedID, icon); err != nil {
  173. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  174. }
  175. }
  176. }
  177. }