From 0564f0bf7a43ab5976751af59b98aa6746450b60 Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Wed, 22 Mar 2023 13:57:18 -0700 Subject: [PATCH] fix(actions): watch folder (#780) * fix(macros): Download torrent for watchfolders * use in-memory torrent for watchfolder * Update run.go --- internal/action/run.go | 11 ++++------- internal/domain/action.go | 5 +++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/internal/action/run.go b/internal/action/run.go index 823148a..957f642 100644 --- a/internal/action/run.go +++ b/internal/action/run.go @@ -163,12 +163,9 @@ func (s *service) watchFolder(ctx context.Context, action *domain.Action, releas s.log.Trace().Msgf("action WATCH_FOLDER: %v file: %v", action.WatchFolder, release.TorrentTmpFile) - // Open original file - original, err := os.Open(release.TorrentTmpFile) - if err != nil { - return errors.Wrap(err, "could not open temp file: %v", release.TorrentTmpFile) + if len(release.TorrentDataRawBytes) < 1 { + return fmt.Errorf("watch_folder: missing torrent %s", release.TorrentName) } - defer original.Close() // default dir to watch folder // /mnt/watch/{{.Indexer}} @@ -188,7 +185,7 @@ func (s *service) watchFolder(ctx context.Context, action *domain.Action, releas } // Create folder - if err = os.MkdirAll(dir, os.ModePerm); err != nil { + if err := os.MkdirAll(dir, os.ModePerm); err != nil { return errors.Wrap(err, "could not create new folders %v", dir) } @@ -200,7 +197,7 @@ func (s *service) watchFolder(ctx context.Context, action *domain.Action, releas defer newFile.Close() // Copy file - if _, err := io.Copy(newFile, original); err != nil { + if _, err := io.Copy(newFile, bytes.NewReader(release.TorrentDataRawBytes)); err != nil { return errors.Wrap(err, "could not copy file %v to watch folder", newFileName) } diff --git a/internal/domain/action.go b/internal/domain/action.go index d9678af..5570597 100644 --- a/internal/domain/action.go +++ b/internal/domain/action.go @@ -59,7 +59,7 @@ func (a *Action) ParseMacros(release *Release) error { if release.TorrentTmpFile == "" && (strings.Contains(a.ExecArgs, "TorrentPathName") || strings.Contains(a.ExecArgs, "TorrentDataRawBytes") || strings.Contains(a.WebhookData, "TorrentPathName") || strings.Contains(a.WebhookData, "TorrentDataRawBytes") || - strings.Contains(a.SavePath, "TorrentPathName")) { + strings.Contains(a.SavePath, "TorrentPathName") || a.Type == ActionTypeWatchFolder) { if err := release.DownloadTorrentFile(); err != nil { return errors.Wrap(err, "webhook: could not download torrent file for release: %v", release.TorrentName) } @@ -67,7 +67,8 @@ func (a *Action) ParseMacros(release *Release) error { // 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(a.ExecArgs, "TorrentDataRawBytes") || strings.Contains(a.WebhookData, "TorrentDataRawBytes")) { + (strings.Contains(a.ExecArgs, "TorrentDataRawBytes") || strings.Contains(a.WebhookData, "TorrentDataRawBytes") || + a.Type == ActionTypeWatchFolder) { t, err := os.ReadFile(release.TorrentTmpFile) if err != nil { return errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile)