|
|
@@ -7,8 +7,10 @@ import (
|
|
|
"fmt"
|
|
|
"io"
|
|
|
|
|
|
+ "miniflux.app/v2/internal/locale"
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
"miniflux.app/v2/internal/storage"
|
|
|
+ "miniflux.app/v2/internal/urllib"
|
|
|
"miniflux.app/v2/internal/validator"
|
|
|
)
|
|
|
|
|
|
@@ -56,25 +58,42 @@ func (h *Handler) Export(userID int64) (string, error) {
|
|
|
return serialize(subscriptions), nil
|
|
|
}
|
|
|
|
|
|
-// Import parses and create feeds from an OPML import.
|
|
|
-func (h *Handler) Import(userID int64, data io.Reader) error {
|
|
|
+// ImportResult summarizes an OPML import.
|
|
|
+type ImportResult struct {
|
|
|
+ Created int
|
|
|
+ Existing int
|
|
|
+ Skipped []SkippedFeed
|
|
|
+}
|
|
|
+
|
|
|
+// SkippedFeed is a feed of the OPML document that was not imported because it is invalid.
|
|
|
+type SkippedFeed struct {
|
|
|
+ FeedURL string
|
|
|
+ Reason *locale.LocalizedError
|
|
|
+}
|
|
|
+
|
|
|
+// Import creates the feeds of an OPML document, skipping the invalid ones.
|
|
|
+func (h *Handler) Import(userID int64, data io.Reader) (*ImportResult, error) {
|
|
|
subscriptions, err := parse(data)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
+ result := &ImportResult{}
|
|
|
for _, subscription := range subscriptions {
|
|
|
if h.store.FeedURLExists(userID, subscription.FeedURL) {
|
|
|
+ result.Existing++
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- category, err := h.resolveCategory(userID, subscription.CategoryName)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
+ // Validate before resolving the category, so that a skipped feed does not leave an empty category behind.
|
|
|
+ if validationErr := validateSubscription(subscription); validationErr != nil {
|
|
|
+ result.Skipped = append(result.Skipped, SkippedFeed{FeedURL: subscription.FeedURL, Reason: validationErr})
|
|
|
+ continue
|
|
|
}
|
|
|
|
|
|
- if validationErr := validateSubscription(userID, category.ID, h.store, subscription); validationErr != nil {
|
|
|
- return fmt.Errorf(`opml: invalid feed settings for %q: %w`, subscription.FeedURL, validationErr)
|
|
|
+ category, err := h.resolveCategory(userID, subscription.CategoryName)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
feed := &model.Feed{
|
|
|
@@ -87,11 +106,12 @@ func (h *Handler) Import(userID int64, data io.Reader) error {
|
|
|
}
|
|
|
applySubscriptionSettings(feed, subscription)
|
|
|
if err := h.store.CreateFeed(feed); err != nil {
|
|
|
- return fmt.Errorf(`opml: unable to create this feed: %q`, subscription.FeedURL)
|
|
|
+ return nil, fmt.Errorf(`opml: unable to create this feed: %q`, subscription.FeedURL)
|
|
|
}
|
|
|
+ result.Created++
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
+ return result, nil
|
|
|
}
|
|
|
|
|
|
func (h *Handler) resolveCategory(userID int64, categoryName string) (*model.Category, error) {
|
|
|
@@ -141,10 +161,13 @@ func applySubscriptionSettings(feed *model.Feed, s subcription) {
|
|
|
feed.IgnoreEntryUpdates = s.IgnoreEntryUpdates
|
|
|
}
|
|
|
|
|
|
-func validateSubscription(userID, categoryID int64, store *storage.Storage, s subcription) error {
|
|
|
+func validateSubscription(s subcription) *locale.LocalizedError {
|
|
|
+ if !urllib.IsAbsoluteURL(s.FeedURL) {
|
|
|
+ return locale.NewLocalizedError("error.invalid_feed_url")
|
|
|
+ }
|
|
|
+
|
|
|
feedCreationRequest := &model.FeedCreationRequest{
|
|
|
FeedURL: s.FeedURL,
|
|
|
- CategoryID: categoryID,
|
|
|
UserAgent: s.UserAgent,
|
|
|
Crawler: s.Crawler,
|
|
|
IgnoreEntryUpdates: s.IgnoreEntryUpdates,
|
|
|
@@ -164,11 +187,7 @@ func validateSubscription(userID, categoryID int64, store *storage.Storage, s su
|
|
|
UrlRewriteRules: s.UrlRewriteRules,
|
|
|
}
|
|
|
|
|
|
- if validationErr := validator.ValidateFeedCreation(store, userID, feedCreationRequest); validationErr != nil {
|
|
|
- return validationErr.Error()
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
+ return validator.ValidateFeedSettings(feedCreationRequest)
|
|
|
}
|
|
|
|
|
|
// NewHandler creates a new handler for OPML files.
|