subscription.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. UserAgent string
  18. Username string
  19. Password string
  20. ScraperRules string
  21. RewriteRules string
  22. BlocklistRules string
  23. KeeplistRules string
  24. }
  25. // Validate makes sure the form values are valid.
  26. func (s *SubscriptionForm) Validate() error {
  27. if s.URL == "" || s.CategoryID == 0 {
  28. return errors.NewLocalizedError("error.feed_mandatory_fields")
  29. }
  30. if !validator.IsValidURL(s.URL) {
  31. return errors.NewLocalizedError("error.invalid_feed_url")
  32. }
  33. if !validator.IsValidRegex(s.BlocklistRules) {
  34. return errors.NewLocalizedError("error.feed_invalid_blocklist_rule")
  35. }
  36. if !validator.IsValidRegex(s.KeeplistRules) {
  37. return errors.NewLocalizedError("error.feed_invalid_keeplist_rule")
  38. }
  39. return nil
  40. }
  41. // NewSubscriptionForm returns a new SubscriptionForm.
  42. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  43. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  44. if err != nil {
  45. categoryID = 0
  46. }
  47. return &SubscriptionForm{
  48. URL: r.FormValue("url"),
  49. CategoryID: int64(categoryID),
  50. Crawler: r.FormValue("crawler") == "1",
  51. FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
  52. UserAgent: r.FormValue("user_agent"),
  53. Username: r.FormValue("feed_username"),
  54. Password: r.FormValue("feed_password"),
  55. ScraperRules: r.FormValue("scraper_rules"),
  56. RewriteRules: r.FormValue("rewrite_rules"),
  57. BlocklistRules: r.FormValue("blocklist_rules"),
  58. KeeplistRules: r.FormValue("keeplist_rules"),
  59. }
  60. }