subscription.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package form // import "miniflux.app/v2/internal/ui/form"
  4. import (
  5. "net/http"
  6. "strconv"
  7. "miniflux.app/v2/internal/locale"
  8. "miniflux.app/v2/internal/validator"
  9. )
  10. // SubscriptionForm represents the subscription form.
  11. type SubscriptionForm struct {
  12. URL string
  13. CategoryID int64
  14. Crawler bool
  15. FetchViaProxy bool
  16. AllowSelfSignedCertificates bool
  17. UserAgent string
  18. Cookie string
  19. Username string
  20. Password string
  21. ScraperRules string
  22. RewriteRules string
  23. BlocklistRules string
  24. KeeplistRules string
  25. UrlRewriteRules string
  26. }
  27. // Validate makes sure the form values locale.are valid.
  28. func (s *SubscriptionForm) Validate() *locale.LocalizedError {
  29. if s.URL == "" || s.CategoryID == 0 {
  30. return locale.NewLocalizedError("error.feed_mandatory_fields")
  31. }
  32. if !validator.IsValidURL(s.URL) {
  33. return locale.NewLocalizedError("error.invalid_feed_url")
  34. }
  35. if !validator.IsValidRegex(s.BlocklistRules) {
  36. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  37. }
  38. if !validator.IsValidRegex(s.KeeplistRules) {
  39. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  40. }
  41. if !validator.IsValidRegex(s.UrlRewriteRules) {
  42. return locale.NewLocalizedError("error.feed_invalid_urlrewrite_rule")
  43. }
  44. return nil
  45. }
  46. // NewSubscriptionForm returns a new SubscriptionForm.
  47. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  48. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  49. if err != nil {
  50. categoryID = 0
  51. }
  52. return &SubscriptionForm{
  53. URL: r.FormValue("url"),
  54. CategoryID: int64(categoryID),
  55. Crawler: r.FormValue("crawler") == "1",
  56. AllowSelfSignedCertificates: r.FormValue("allow_self_signed_certificates") == "1",
  57. FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
  58. UserAgent: r.FormValue("user_agent"),
  59. Cookie: r.FormValue("cookie"),
  60. Username: r.FormValue("feed_username"),
  61. Password: r.FormValue("feed_password"),
  62. ScraperRules: r.FormValue("scraper_rules"),
  63. RewriteRules: r.FormValue("rewrite_rules"),
  64. BlocklistRules: r.FormValue("blocklist_rules"),
  65. KeeplistRules: r.FormValue("keeplist_rules"),
  66. UrlRewriteRules: r.FormValue("urlrewrite_rules"),
  67. }
  68. }