From 5870f0426002c8e26a9ff472b23e15d7bf1235f7 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 14 Oct 2018 11:46:41 -0700 Subject: Simplify feed parser and format detection - Avoid doing multiple buffer copies - Move parser and format detection logic to its own package --- reader/subscription/finder.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'reader/subscription') diff --git a/reader/subscription/finder.go b/reader/subscription/finder.go index 027e810..c59c002 100644 --- a/reader/subscription/finder.go +++ b/reader/subscription/finder.go @@ -5,15 +5,15 @@ package subscription // import "miniflux.app/reader/subscription" import ( - "bytes" "fmt" "io" + "strings" "time" "miniflux.app/errors" "miniflux.app/http/client" "miniflux.app/logger" - "miniflux.app/reader/feed" + "miniflux.app/reader/parser" "miniflux.app/timer" "miniflux.app/url" @@ -56,20 +56,12 @@ func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscr return nil, errors.NewLocalizedError(errEmptyBody) } - body, err := response.NormalizeBodyEncoding() - if err != nil { + if err := response.EnsureUnicodeBody(); err != nil { return nil, err } - var buffer bytes.Buffer - size, _ := io.Copy(&buffer, body) - if size == 0 { - return nil, errors.NewLocalizedError(errEmptyBody) - } - - reader := bytes.NewReader(buffer.Bytes()) - - if format := feed.DetectFeedFormat(reader); format != feed.FormatUnknown { + body := response.String() + if format := parser.DetectFeedFormat(body); format != parser.FormatUnknown { var subscriptions Subscriptions subscriptions = append(subscriptions, &Subscription{ Title: response.EffectiveURL, @@ -80,8 +72,7 @@ func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscr return subscriptions, nil } - reader.Seek(0, io.SeekStart) - return parseDocument(response.EffectiveURL, bytes.NewReader(buffer.Bytes())) + return parseDocument(response.EffectiveURL, strings.NewReader(body)) } func parseDocument(websiteURL string, data io.Reader) (Subscriptions, error) { -- cgit v1.2.3