fix: download client and ipt download url

This commit is contained in:
Ludvig Lundgren 2021-08-30 20:54:14 +02:00
parent 9aaf79e7bb
commit 2cba7a90e0
3 changed files with 18 additions and 5 deletions

View file

@ -45,6 +45,7 @@ func (c *HttpClient) DownloadFile(url string, opts map[string]string) (*Download
// Create the file
out, err := os.Create(tmpFileName)
if err != nil {
log.Error().Stack().Err(err).Msgf("error creating temp file: %v", tmpFileName)
return nil, err
}
@ -53,20 +54,24 @@ func (c *HttpClient) DownloadFile(url string, opts map[string]string) (*Download
// Get the data
resp, err := http.Get(url)
if err != nil {
// TODO better error message
log.Error().Stack().Err(err).Msgf("error downloading file %v from %v", tmpFileName, url)
return nil, err
}
defer resp.Body.Close()
// retry logic
log.Trace().Msgf("downloaded file response: %v - status: %v", resp.Status, resp.StatusCode)
if resp.StatusCode != 200 {
log.Error().Stack().Err(err).Msgf("error downloading file: %v - bad status: %d", tmpFileName, resp.StatusCode)
return nil, err
}
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Error().Stack().Err(err).Msgf("error writing downloaded file: %v", tmpFileName)
return nil, err
}
@ -77,5 +82,7 @@ func (c *HttpClient) DownloadFile(url string, opts map[string]string) (*Download
FileName: tmpFileName,
}
log.Trace().Msgf("successfully downloaded file: %v", tmpFileName)
return &res, nil
}

View file

@ -57,6 +57,6 @@ parse:
- torrentSize
match:
torrenturl: "{{ .baseUrl }}/download.php?id={{ .torrentId }}&file={{ .torrentName }}.torrent&passkey={{ .passkey }}"
torrenturl: "{{ .baseUrl }}/download.php/{{ .torrentId }}/{{ .torrentName }}.torrent&torrent_pass={{ .passkey }}"
encode:
- torrentName

View file

@ -1,6 +1,7 @@
package release
import (
"errors"
"fmt"
"github.com/anacrolix/torrent/metainfo"
@ -41,12 +42,17 @@ func (s *service) Process(announce domain.Announce) error {
// TODO check extra headers, cookie
res, err := c.DownloadFile(announce.TorrentUrl, nil)
if err != nil {
log.Error().Err(err).Msgf("could not download file: %v", announce.TorrentName)
log.Error().Stack().Err(err).Msgf("could not download file: %v", announce.TorrentName)
return err
}
if res.FileName == "" {
return err
return errors.New("error downloading file, no tmp file")
}
if res.Body == nil {
log.Error().Stack().Err(err).Msgf("tmp file error - empty body: %v", announce.TorrentName)
return errors.New("empty body")
}
//log.Debug().Msgf("downloaded torrent file: %v", res.FileName)
@ -68,7 +74,7 @@ func (s *service) Process(announce domain.Announce) error {
// take action (watchFolder, test, runProgram, qBittorrent, Deluge etc)
err = s.actionSvc.RunActions(res.FileName, hash, *announce.Filter, announce)
if err != nil {
log.Error().Err(err).Msgf("error running actions for filter: %v", announce.Filter.Name)
log.Error().Stack().Err(err).Msgf("error running actions for filter: %v", announce.Filter.Name)
return err
}