diff --git a/internal/action/exec.go b/internal/action/exec.go index 28d3f2a..038ccc7 100644 --- a/internal/action/exec.go +++ b/internal/action/exec.go @@ -1,6 +1,7 @@ package action import ( + "io/ioutil" "os/exec" "strings" "time" @@ -20,6 +21,16 @@ func (s *service) execCmd(action domain.Action, release domain.Release) error { } } + // read the file into bytes we can then use in the macro + if len(release.TorrentDataRawBytes) == 0 && release.TorrentTmpFile != "" { + t, err := ioutil.ReadFile(release.TorrentTmpFile) + if err != nil { + return errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile) + } + + release.TorrentDataRawBytes = t + } + // check if program exists cmd, err := exec.LookPath(action.ExecCmd) if err != nil { diff --git a/internal/action/run.go b/internal/action/run.go index 92dec0c..9e049ab 100644 --- a/internal/action/run.go +++ b/internal/action/run.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/tls" "io" + "io/ioutil" "net/http" "os" "path" @@ -139,6 +140,15 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) erro } } + if len(release.TorrentDataRawBytes) == 0 { + t, err := ioutil.ReadFile(release.TorrentTmpFile) + if err != nil { + return errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile) + } + + release.TorrentDataRawBytes = t + } + m := domain.NewMacro(release) // parse and replace values in argument string before continuing @@ -184,12 +194,23 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) erro } func (s *service) webhook(action domain.Action, release domain.Release) error { - if release.TorrentTmpFile == "" && strings.Contains(action.WebhookData, "TorrentPathName") { + // if webhook data contains TorrentPathName or TorrentDataRawBytes, lets download the torrent file + if release.TorrentTmpFile == "" && (strings.Contains(action.WebhookData, "TorrentPathName") || strings.Contains(action.WebhookData, "TorrentDataRawBytes")) { if err := release.DownloadTorrentFile(); err != nil { return errors.Wrap(err, "webhook: could not download torrent file for release: %v", release.TorrentName) } } + // if webhook data contains TorrentDataRawBytes, lets read the file into bytes we can then use in the macro + if len(release.TorrentDataRawBytes) == 0 && strings.Contains(action.WebhookData, "TorrentDataRawBytes") { + t, err := ioutil.ReadFile(release.TorrentTmpFile) + if err != nil { + return errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile) + } + + release.TorrentDataRawBytes = t + } + m := domain.NewMacro(release) // parse and replace values in argument string before continuing diff --git a/internal/domain/macros.go b/internal/domain/macros.go index f0da4cd..3090c10 100644 --- a/internal/domain/macros.go +++ b/internal/domain/macros.go @@ -14,6 +14,7 @@ type Macro struct { TorrentPathName string TorrentHash string TorrentUrl string + TorrentDataRawBytes []byte Indexer string Title string Resolution string @@ -38,6 +39,7 @@ func NewMacro(release Release) Macro { TorrentName: release.TorrentName, TorrentUrl: release.TorrentURL, TorrentPathName: release.TorrentTmpFile, + TorrentDataRawBytes: release.TorrentDataRawBytes, TorrentHash: release.TorrentHash, Indexer: release.Indexer, Title: release.Title, diff --git a/internal/domain/release.go b/internal/domain/release.go index 28cfe85..c72e0bb 100644 --- a/internal/domain/release.go +++ b/internal/domain/release.go @@ -46,6 +46,7 @@ type Release struct { TorrentID string `json:"torrent_id"` TorrentURL string `json:"-"` TorrentTmpFile string `json:"-"` + TorrentDataRawBytes []byte `json:"-"` TorrentHash string `json:"-"` TorrentName string `json:"torrent_name"` // full release name Size uint64 `json:"size"` diff --git a/internal/filter/service.go b/internal/filter/service.go index 4fdc88f..e8144af 100644 --- a/internal/filter/service.go +++ b/internal/filter/service.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "fmt" + "io/ioutil" "net/http" "os/exec" "strings" @@ -447,6 +448,16 @@ func (s *service) execCmd(release *domain.Release, cmd string, args string) (int } } + // read the file into bytes we can then use in the macro + if len(release.TorrentDataRawBytes) == 0 && release.TorrentTmpFile != "" { + t, err := ioutil.ReadFile(release.TorrentTmpFile) + if err != nil { + return 0, errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile) + } + + release.TorrentDataRawBytes = t + } + // check if program exists cmd, err := exec.LookPath(cmd) if err != nil { @@ -490,13 +501,23 @@ func (s *service) execCmd(release *domain.Release, cmd string, args string) (int } func (s *service) webhook(release *domain.Release, url string, data string) (int, error) { - - if release.TorrentTmpFile == "" && strings.Contains(data, "TorrentPathName") { + // if webhook data contains TorrentPathName or TorrentDataRawBytes, lets download the torrent file + if release.TorrentTmpFile == "" && (strings.Contains(data, "TorrentPathName") || strings.Contains(data, "TorrentDataRawBytes")) { if err := release.DownloadTorrentFile(); err != nil { return 0, errors.Wrap(err, "webhook: could not download torrent file for release: %v", release.TorrentName) } } + // if webhook data contains TorrentDataRawBytes, lets read the file into bytes we can then use in the macro + if len(release.TorrentDataRawBytes) == 0 && strings.Contains(data, "TorrentDataRawBytes") { + t, err := ioutil.ReadFile(release.TorrentTmpFile) + if err != nil { + return 0, errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile) + } + + release.TorrentDataRawBytes = t + } + m := domain.NewMacro(*release) // parse and replace values in argument string before continuing