4
0

subscription.go 2.3 KB

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