feat(releases): support magnet links (#730)

* feat(releases): support magnet links

* feat(feeds): support magnet links

* feat(actions): log messages

* fix: component warning

* fix: check hasprefix instead of hassuffix for magnet

* feat(release): resolve magnet uri from link

* fix(actions): deluge use magnet uri

* fix(macros): add `MagnetURI` var

* fix(actions): run magnet resolving before macros

* feat(feeds): set download type on creation
This commit is contained in:
ze0s 2023-02-28 22:16:10 +01:00 committed by GitHub
parent c6101cc765
commit ca196f0bf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 770 additions and 260 deletions

View file

@ -2,66 +2,93 @@ package action
import (
"context"
"os"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"os"
"github.com/mrobinsn/go-rtorrent/rtorrent"
)
func (s *service) rtorrent(ctx context.Context, action *domain.Action, release domain.Release) ([]string, error) {
s.log.Debug().Msgf("action rTorrent: %v", action.Name)
s.log.Debug().Msgf("action rTorrent: %s", action.Name)
var err error
// get client for action
client, err := s.clientSvc.FindByID(ctx, action.ClientID)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID)
s.log.Error().Stack().Err(err).Msgf("error finding client: %d", action.ClientID)
return nil, err
}
if client == nil {
return nil, errors.New("could not find client by id: %v", action.ClientID)
return nil, errors.New("could not find client by id: %d", action.ClientID)
}
var rejections []string
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFileCtx(ctx); err != nil {
s.log.Error().Err(err).Msgf("could not download torrent file for release: %v", release.TorrentName)
return nil, err
}
}
// create client
rt := rtorrent.New(client.Host, true)
tmpFile, err := os.ReadFile(release.TorrentTmpFile)
if err != nil {
return nil, errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile)
}
if release.HasMagnetUri() {
var args []*rtorrent.FieldValue
var args []*rtorrent.FieldValue
if action.Label != "" {
args = append(args, &rtorrent.FieldValue{
Field: rtorrent.DLabel,
Value: action.Label,
})
}
if action.SavePath != "" {
args = append(args, &rtorrent.FieldValue{
Field: rtorrent.DDirectory,
Value: action.SavePath,
})
}
if action.Label != "" {
args = append(args, &rtorrent.FieldValue{
Field: rtorrent.DLabel,
Value: action.Label,
})
}
if action.SavePath != "" {
args = append(args, &rtorrent.FieldValue{
Field: rtorrent.DDirectory,
Value: action.SavePath,
})
}
if err := rt.Add(release.MagnetURI, args...); err != nil {
return nil, errors.Wrap(err, "could not add torrent from magnet: %s", release.MagnetURI)
}
if err := rt.AddTorrent(tmpFile, args...); err != nil {
return nil, errors.Wrap(err, "could not add torrent file: %v", release.TorrentTmpFile)
}
s.log.Info().Msgf("torrent from magnet successfully added to client: '%s'", client.Name)
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", "", client.Name)
return nil, nil
} else {
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFileCtx(ctx); err != nil {
s.log.Error().Err(err).Msgf("could not download torrent file for release: %s", release.TorrentName)
return nil, err
}
}
tmpFile, err := os.ReadFile(release.TorrentTmpFile)
if err != nil {
return nil, errors.Wrap(err, "could not read torrent file: %s", release.TorrentTmpFile)
}
var args []*rtorrent.FieldValue
if action.Label != "" {
args = append(args, &rtorrent.FieldValue{
Field: rtorrent.DLabel,
Value: action.Label,
})
}
if action.SavePath != "" {
args = append(args, &rtorrent.FieldValue{
Field: rtorrent.DDirectory,
Value: action.SavePath,
})
}
if err := rt.AddTorrent(tmpFile, args...); err != nil {
return nil, errors.Wrap(err, "could not add torrent file: %s", release.TorrentTmpFile)
}
s.log.Info().Msgf("torrent successfully added to client: '%s'", client.Name)
}
return rejections, nil
}