Skip to content

Commit

Permalink
don't rely on RSS item length, fetching it instead
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal authored and umputun committed Jul 14, 2021
1 parent 2852bdd commit 43d0999
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
10 changes: 4 additions & 6 deletions app/proc/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ func (client TelegramClient) Send(channelID string, item feed.Item) (err error)
return nil
}

contentLength := item.Enclosure.Length
if contentLength <= 0 {
if contentLength, err = getContentLength(item.Enclosure.URL); err != nil {
return errors.Wrapf(err, "can't get length for %s", item.Enclosure.URL)
}
var contentLength int
if contentLength, err = getContentLength(item.Enclosure.URL); err != nil {
return errors.Wrapf(err, "can't get length for %s", item.Enclosure.URL)
}

var message *tb.Message
Expand All @@ -81,7 +79,7 @@ func (client TelegramClient) Send(channelID string, item feed.Item) (err error)
return nil
}

// getContentLength uses HEAD request and called as a fallback in case of item.Enclosure.Length not populated
// getContentLength uses HEAD request to retrieve length of the provided URL
func getContentLength(url string) (int, error) {
resp, err := http.Head(url) // nolint:gosec // URL considered safe
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions app/proc/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ func TestSendIfContentLengthZero(t *testing.T) {
}))
defer ts.Close()

err := client.Send("100500", feed.Item{
Enclosure: feed.Enclosure{
URL: ts.URL,
Length: 0,
},
})
err := client.Send("100500", feed.Item{Enclosure: feed.Enclosure{URL: ts.URL}})

assert.Error(t, err)
assert.EqualError(t, err, fmt.Sprintf("can't get length for %s: non-200 status, 500", ts.URL))
Expand Down

0 comments on commit 43d0999

Please sign in to comment.