fix(macros): TorrentHash empty (#1699)

fix(macros): TorrentHash empty
This commit is contained in:
ze0s 2024-09-02 15:44:36 +02:00 committed by GitHub
parent 89cf68e773
commit d380c0b178
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 43 additions and 30 deletions

View file

@ -149,12 +149,9 @@ func (s *service) CheckActionPreconditions(ctx context.Context, action *domain.A
}
if action.CheckMacrosNeedRawDataBytes(release) {
tmpFile, err := os.ReadFile(release.TorrentTmpFile)
if err != nil {
return errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile)
if err := release.OpenTorrentFile(); err != nil {
return errors.Wrap(err, "could not open torrent file for release: %s", release.TorrentName)
}
release.TorrentDataRawBytes = tmpFile
}
return nil

View file

@ -64,9 +64,12 @@ func (a *Action) CheckMacrosNeedTorrentTmpFile(release *Release) bool {
if release.TorrentTmpFile == "" &&
(strings.Contains(a.ExecArgs, "TorrentPathName") ||
strings.Contains(a.ExecArgs, "TorrentDataRawBytes") ||
strings.Contains(a.ExecArgs, "TorrentHash") ||
strings.Contains(a.WebhookData, "TorrentPathName") ||
strings.Contains(a.WebhookData, "TorrentDataRawBytes") ||
strings.Contains(a.WebhookData, "TorrentHash") ||
strings.Contains(a.SavePath, "TorrentPathName") ||
strings.Contains(a.SavePath, "TorrentHash") ||
a.Type == ActionTypeWatchFolder) {
return true
}

View file

@ -186,6 +186,22 @@ type FilterExternal struct {
FilterId int `json:"-"`
}
func (f FilterExternal) NeedTorrentDownloaded() bool {
if strings.Contains(f.ExecArgs, "TorrentHash") || strings.Contains(f.WebhookData, "TorrentHash") {
return true
}
if strings.Contains(f.ExecArgs, "TorrentPathName") || strings.Contains(f.WebhookData, "TorrentPathName") {
return true
}
if strings.Contains(f.WebhookData, "TorrentDataRawBytes") {
return true
}
return false
}
type FilterExternalType string
const (

View file

@ -426,6 +426,17 @@ func (r *Release) ParseSizeBytesString(size string) {
}
}
func (r *Release) OpenTorrentFile() error {
tmpFile, err := os.ReadFile(r.TorrentTmpFile)
if err != nil {
return errors.Wrap(err, "could not read torrent file: %v", r.TorrentTmpFile)
}
r.TorrentDataRawBytes = tmpFile
return nil
}
func (r *Release) DownloadTorrentFileCtx(ctx context.Context) error {
return r.downloadTorrentFile(ctx)
}

View file

@ -9,7 +9,6 @@ import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"sort"
"strconv"
@ -551,6 +550,12 @@ func (s *service) RunExternalFilters(ctx context.Context, f *domain.Filter, exte
continue
}
if external.NeedTorrentDownloaded() {
if err := s.downloadSvc.DownloadRelease(ctx, release); err != nil {
return false, errors.Wrap(err, "could not download torrent file for release: %s", release.TorrentName)
}
}
switch external.Type {
case domain.ExternalFilterTypeExec:
// run external script
@ -583,23 +588,14 @@ func (s *service) RunExternalFilters(ctx context.Context, f *domain.Filter, exte
return true, nil
}
func (s *service) execCmd(ctx context.Context, external domain.FilterExternal, release *domain.Release) (int, error) {
func (s *service) execCmd(_ context.Context, external domain.FilterExternal, release *domain.Release) (int, error) {
s.log.Trace().Msgf("filter exec release: %s", release.TorrentName)
if release.TorrentTmpFile == "" && strings.Contains(external.ExecArgs, "TorrentPathName") {
if err := s.downloadSvc.DownloadRelease(ctx, release); err != nil {
return 0, errors.Wrap(err, "could not download torrent file for release: %s", release.TorrentName)
}
}
// read the file into bytes we can then use in the macro
if len(release.TorrentDataRawBytes) == 0 && release.TorrentTmpFile != "" {
t, err := os.ReadFile(release.TorrentTmpFile)
if err != nil {
return 0, errors.Wrap(err, "could not read torrent file: %s", release.TorrentTmpFile)
if err := release.OpenTorrentFile(); err != nil {
return 0, errors.Wrap(err, "could not open torrent file for release: %s", release.TorrentName)
}
release.TorrentDataRawBytes = t
}
// check if program exists
@ -687,21 +683,11 @@ func (s *service) webhook(ctx context.Context, external domain.FilterExternal, r
return 0, errors.New("external filter: missing host for webhook")
}
// if webhook data contains TorrentPathName or TorrentDataRawBytes, lets download the torrent file
if release.TorrentTmpFile == "" && (strings.Contains(external.WebhookData, "TorrentPathName") || strings.Contains(external.WebhookData, "TorrentDataRawBytes")) {
if err := s.downloadSvc.DownloadRelease(ctx, release); err != nil {
return 0, errors.Wrap(err, "webhook: could not download torrent file for release: %s", 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(external.WebhookData, "TorrentDataRawBytes") {
t, err := os.ReadFile(release.TorrentTmpFile)
if err != nil {
return 0, errors.Wrap(err, "could not read torrent file: %s", release.TorrentTmpFile)
if err := release.OpenTorrentFile(); err != nil {
return 0, errors.Wrap(err, "could not open torrent file for release: %s", release.TorrentName)
}
release.TorrentDataRawBytes = t
}
m := domain.NewMacro(*release)