2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package opml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2017-11-21 04:11:06 +01:00
|
|
|
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
|
|
|
"github.com/miniflux/miniflux/storage"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-21 04:11:06 +01:00
|
|
|
// Handler handles the logic for OPML import/export.
|
|
|
|
type Handler struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
store *storage.Storage
|
|
|
|
}
|
|
|
|
|
2017-11-21 04:11:06 +01:00
|
|
|
// Export exports user feeds to OPML.
|
|
|
|
func (h *Handler) Export(userID int64) (string, error) {
|
2017-11-28 06:30:04 +01:00
|
|
|
feeds, err := h.store.Feeds(userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2017-11-21 04:11:06 +01:00
|
|
|
return "", errors.New("unable to fetch feeds")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var subscriptions SubcriptionList
|
|
|
|
for _, feed := range feeds {
|
|
|
|
subscriptions = append(subscriptions, &Subcription{
|
|
|
|
Title: feed.Title,
|
|
|
|
FeedURL: feed.FeedURL,
|
|
|
|
SiteURL: feed.SiteURL,
|
|
|
|
CategoryName: feed.Category.Title,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return Serialize(subscriptions), nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 04:11:06 +01:00
|
|
|
// Import parses and create feeds from an OPML import.
|
|
|
|
func (h *Handler) Import(userID int64, data io.Reader) (err error) {
|
2017-11-20 06:10:04 +01:00
|
|
|
subscriptions, err := Parse(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subscription := range subscriptions {
|
2017-11-21 04:11:06 +01:00
|
|
|
if !h.store.FeedURLExists(userID, subscription.FeedURL) {
|
2017-11-20 06:10:04 +01:00
|
|
|
var category *model.Category
|
|
|
|
|
|
|
|
if subscription.CategoryName == "" {
|
2017-11-28 06:30:04 +01:00
|
|
|
category, err = h.store.FirstCategory(userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2017-11-21 04:11:06 +01:00
|
|
|
return errors.New("unable to find first category")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-28 06:30:04 +01:00
|
|
|
category, err = h.store.CategoryByTitle(userID, subscription.CategoryName)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2017-11-21 04:11:06 +01:00
|
|
|
return errors.New("unable to search category by title")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if category == nil {
|
|
|
|
category = &model.Category{
|
|
|
|
UserID: userID,
|
|
|
|
Title: subscription.CategoryName,
|
|
|
|
}
|
|
|
|
|
2017-11-21 04:11:06 +01:00
|
|
|
err := h.store.CreateCategory(category)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2017-11-21 04:11:06 +01:00
|
|
|
return fmt.Errorf(`unable to create this category: "%s"`, subscription.CategoryName)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
feed := &model.Feed{
|
|
|
|
UserID: userID,
|
|
|
|
Title: subscription.Title,
|
|
|
|
FeedURL: subscription.FeedURL,
|
|
|
|
SiteURL: subscription.SiteURL,
|
|
|
|
Category: category,
|
|
|
|
}
|
|
|
|
|
2017-11-21 04:11:06 +01:00
|
|
|
h.store.CreateFeed(feed)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 04:11:06 +01:00
|
|
|
// NewHandler creates a new handler for OPML files.
|
|
|
|
func NewHandler(store *storage.Storage) *Handler {
|
|
|
|
return &Handler{store: store}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|