handler.go 7.3 KB

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