subscription.go 2.6 KB

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