From c8d1eacc292579e6a4daf72c1c1dfcce69e2ff25 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Sat, 4 Sep 2021 11:01:40 +0200 Subject: parser: Move method --- parser/parser.go | 19 +++++++++++++++++++ queue/item.go | 27 +++------------------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index e86f418..621992d 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1,9 +1,14 @@ package parser import ( + "bytes" "fmt" + "os" + "path/filepath" "regexp" "strconv" + "strings" + "text/template" ) var ( @@ -41,6 +46,20 @@ func (m *Media) Equal(o Media) bool { return m.Name == o.Name && m.Season == o.Season && m.Episode == o.Episode && m.Year == o.Year } +func (m *Media) PathIn(dir *template.Template) (string, error) { + var b bytes.Buffer + if err := dir.Execute(&b, m); err != nil { + return "", err + } + path := b.String() + // When path has a trailing slash, the actual destination path will be a directory inside LocalPath (same + // behaviour as rsync) + if strings.HasSuffix(path, string(os.PathSeparator)) { + path = filepath.Join(path, m.Release) + } + return path, nil +} + func Default(s string) (Media, error) { return Media{Release: s}, nil } diff --git a/queue/item.go b/queue/item.go index 53a5f63..27d3821 100644 --- a/queue/item.go +++ b/queue/item.go @@ -1,14 +1,9 @@ package queue import ( - "bytes" - "os" "path/filepath" - "strings" "time" - "text/template" - "github.com/mpolden/lftpq/parser" ) @@ -51,21 +46,6 @@ func (i *Item) setMedia(dirname string) error { return nil } -func (i *Item) setLocalPath(t *template.Template) error { - var b bytes.Buffer - if err := t.Execute(&b, i.Media); err != nil { - return err - } - path := b.String() - // When path has a trailing slash, the actual destination path will be a directory inside LocalPath (same - // behaviour as rsync) - if strings.HasSuffix(path, string(os.PathSeparator)) { - path = filepath.Join(path, filepath.Base(i.RemotePath)) - } - i.LocalPath = path - return nil -} - func (i *Item) duplicates(readDir readDir) []Item { var items []Item parent := filepath.Join(i.LocalPath, "..") @@ -97,8 +77,7 @@ func newItem(remotePath string, modTime time.Time, itemParser itemParser) (Item, if err := item.setMedia(filepath.Base(remotePath)); err != nil { return item, err } - if err := item.setLocalPath(itemParser.template); err != nil { - return item, err - } - return item, nil + var err error + item.LocalPath, err = item.Media.PathIn(itemParser.template) + return item, err } -- cgit v1.2.3