mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(actions): watch dir rename file (#515)
This commit is contained in:
parent
716784e7a6
commit
547b01a50d
1 changed files with 23 additions and 9 deletions
|
@ -154,6 +154,10 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) erro
|
||||||
m := domain.NewMacro(release)
|
m := domain.NewMacro(release)
|
||||||
|
|
||||||
// parse and replace values in argument string before continuing
|
// parse and replace values in argument string before continuing
|
||||||
|
// /mnt/watch/{{.Indexer}}
|
||||||
|
// /mnt/watch/mock
|
||||||
|
// /mnt/watch/{{.Indexer}}-{{.TorrentName}}.torrent
|
||||||
|
// /mnt/watch/mock-Torrent.Name-GROUP.torrent
|
||||||
watchFolderArgs, err := m.Parse(action.WatchFolder)
|
watchFolderArgs, err := m.Parse(action.WatchFolder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "could not parse watch folder macro: %v", action.WatchFolder)
|
return errors.Wrap(err, "could not parse watch folder macro: %v", action.WatchFolder)
|
||||||
|
@ -168,27 +172,37 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) erro
|
||||||
}
|
}
|
||||||
defer original.Close()
|
defer original.Close()
|
||||||
|
|
||||||
_, tmpFileName := filepath.Split(release.TorrentTmpFile)
|
// default dir to watch folder
|
||||||
fullFileName := filepath.Join(watchFolderArgs, tmpFileName+".torrent")
|
dir := watchFolderArgs
|
||||||
|
newFileName := watchFolderArgs
|
||||||
|
|
||||||
|
// if watchFolderArgs does not contain .torrent, create
|
||||||
|
if !strings.Contains(watchFolderArgs, ".torrent") {
|
||||||
|
_, tmpFileName := filepath.Split(release.TorrentTmpFile)
|
||||||
|
|
||||||
|
newFileName = filepath.Join(watchFolderArgs, tmpFileName+".torrent")
|
||||||
|
} else {
|
||||||
|
dir, _ = filepath.Split(watchFolderArgs)
|
||||||
|
}
|
||||||
|
|
||||||
// Create folder
|
// Create folder
|
||||||
if err = os.MkdirAll(watchFolderArgs, os.ModePerm); err != nil {
|
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
return errors.Wrap(err, "could not create new folders %v", fullFileName)
|
return errors.Wrap(err, "could not create new folders %v", dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new file
|
// Create new file
|
||||||
newFile, err := os.Create(fullFileName)
|
newFile, err := os.Create(newFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "could not create new file %v", fullFileName)
|
return errors.Wrap(err, "could not create new file %v", newFileName)
|
||||||
}
|
}
|
||||||
defer newFile.Close()
|
defer newFile.Close()
|
||||||
|
|
||||||
// Copy file
|
// Copy file
|
||||||
if _, err := io.Copy(newFile, original); err != nil {
|
if _, err := io.Copy(newFile, original); err != nil {
|
||||||
return errors.Wrap(err, "could not copy file %v to watch folder", fullFileName)
|
return errors.Wrap(err, "could not copy file %v to watch folder", newFileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.log.Info().Msgf("saved file to watch folder: %v", fullFileName)
|
s.log.Info().Msgf("saved file to watch folder: %v", newFileName)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue