feat(download-client): add support for Porla (#553)

* Add support for the 'Test' button to work

* Make Porla show up in filter actions select

* Add an empty Porla action

* Make Porla action find download client

* Make implementation actually add torrent to Porla

* Fix qBittorrent import

* Finish up Porla action

* Check length on commitish before slicing

* Move Porla to the other DL clients

* Add Porla to type name map

* Move Porla to beneath the other download clients
This commit is contained in:
Viktor Elofsson 2023-01-29 18:17:01 +01:00 committed by GitHub
parent b95c1e6913
commit 870e109f6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 271 additions and 2 deletions

79
internal/action/porla.go Normal file
View file

@ -0,0 +1,79 @@
package action
import (
"bufio"
"context"
"encoding/base64"
"io/ioutil"
"os"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/porla"
"github.com/dcarbone/zadapters/zstdlog"
"github.com/rs/zerolog"
)
func (s *service) porla(action domain.Action, release domain.Release) ([]string, error) {
s.log.Debug().Msgf("action Porla: %v", action.Name)
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
return nil, errors.Wrap(err, "error finding client: %v", action.ClientID)
}
if client == nil {
return nil, errors.New("could not find client by id: %v", action.ClientID)
}
porlaSettings := porla.Settings{
Hostname: client.Host,
AuthToken: client.Settings.APIKey,
}
porlaSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "Porla").Str("client", client.Name).Logger(), zerolog.TraceLevel)
prl := porla.NewClient(porlaSettings)
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
return nil, errors.Wrap(err, "error downloading torrent file for release: %v", release.TorrentName)
}
}
file, err := os.Open(release.TorrentTmpFile)
if err != nil {
return nil, errors.Wrap(err, "error opening file %v", release.TorrentTmpFile)
}
defer file.Close()
reader := bufio.NewReader(file)
content, err := ioutil.ReadAll(reader)
if err != nil {
return nil, errors.Wrap(err, "failed to read file: %v", release.TorrentTmpFile)
}
opts := &porla.TorrentsAddReq{
DownloadLimit: -1,
SavePath: action.SavePath,
Ti: base64.StdEncoding.EncodeToString(content),
UploadLimit: -1,
}
if action.LimitDownloadSpeed > 0 {
opts.DownloadLimit = action.LimitDownloadSpeed * 1000
}
if action.LimitUploadSpeed > 0 {
opts.UploadLimit = action.LimitUploadSpeed * 1000
}
if err = prl.TorrentsAdd(opts); err != nil {
return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
}
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", release.TorrentHash, client.Name)
return nil, nil
}