✨ Support alternate source per show
This commit is contained in:
parent
5e8f954a14
commit
1b072f42d3
2 changed files with 64 additions and 26 deletions
|
|
@ -39,21 +39,32 @@ type GroupPrinter func(name string, group Shows)
|
||||||
var ErrNoFiles = errors.New("no files processed")
|
var ErrNoFiles = errors.New("no files processed")
|
||||||
var ErrSkipped = errors.New("skipped")
|
var ErrSkipped = errors.New("skipped")
|
||||||
|
|
||||||
func processFile(conf *Config, walk ShowWalker, show *Show, file os.DirEntry) (processed bool, processError, cbError error) {
|
type processInput struct {
|
||||||
|
file os.DirEntry
|
||||||
|
source string
|
||||||
|
show *Show
|
||||||
|
extRe *regexp.Regexp
|
||||||
|
skips []string
|
||||||
|
walk ShowWalker
|
||||||
|
}
|
||||||
|
|
||||||
|
func processFile(input processInput) (processed bool, processError, cbError error) {
|
||||||
|
file := input.file
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !conf.extRe.MatchString(file.Name()) {
|
if !input.extRe.MatchString(file.Name()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
show := input.show
|
||||||
var found bool
|
var found bool
|
||||||
found, processError = show.Match(file.Name())
|
found, processError = show.Match(file.Name())
|
||||||
if processError != nil || !found {
|
if processError != nil || !found {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
path := fp.Join(conf.Source, file.Name())
|
path := fp.Join(input.source, file.Name())
|
||||||
for _, ext := range conf.Skip {
|
for _, ext := range input.skips {
|
||||||
if !strings.HasPrefix(ext, ".") {
|
if !strings.HasPrefix(ext, ".") {
|
||||||
ext = "." + ext
|
ext = "." + ext
|
||||||
}
|
}
|
||||||
|
|
@ -64,13 +75,31 @@ func processFile(conf *Config, walk ShowWalker, show *Show, file os.DirEntry) (p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cbError = walk(show, path)
|
cbError = input.walk(show, path)
|
||||||
if cbError == nil {
|
if cbError == nil {
|
||||||
processed = true
|
processed = true
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fileMap map[string][]os.DirEntry
|
||||||
|
|
||||||
|
func (fm fileMap) GetFiles(path string) []os.DirEntry {
|
||||||
|
files, ok := fm[path]
|
||||||
|
if ok {
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
fm.ReadPath(path)
|
||||||
|
return fm[path]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm fileMap) ReadPath(path string) {
|
||||||
|
if _, ok := fm[path]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fm[path], _ = os.ReadDir(path)
|
||||||
|
}
|
||||||
|
|
||||||
func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) {
|
func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) {
|
||||||
err = ensureExtRe(d.Config)
|
err = ensureExtRe(d.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -80,33 +109,41 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e
|
||||||
cb = DryRun(os.Stdout)
|
cb = DryRun(os.Stdout)
|
||||||
}
|
}
|
||||||
count := 0
|
count := 0
|
||||||
|
allFiles := fileMap{}
|
||||||
var files []os.DirEntry
|
var files []os.DirEntry
|
||||||
files, err = os.ReadDir(d.Config.Source)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for s := range d.AllShows(gp) {
|
for s := range d.AllShows(gp) {
|
||||||
for _, file := range files {
|
if len(s.Sources) == 0 {
|
||||||
processed, processErr, cbErr := processFile(d.Config, cb, s, file)
|
s.Sources = []string{d.Config.Source}
|
||||||
if !processed && processErr == nil && cbErr == nil {
|
}
|
||||||
continue
|
for _, source := range s.Sources {
|
||||||
|
files = allFiles.GetFiles(source)
|
||||||
|
for _, file := range files {
|
||||||
|
pIn := processInput{file, source, s, d.Config.extRe, d.Config.Skip, cb}
|
||||||
|
processed, processErr, cbErr := processFile(pIn)
|
||||||
|
if !processed && processErr == nil && cbErr == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if errors.Is(processErr, ErrSkipped) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if processErr != nil {
|
||||||
|
return processErr
|
||||||
|
}
|
||||||
|
if cbErr != nil && stopOnError {
|
||||||
|
return cbErr
|
||||||
|
}
|
||||||
|
if cbErr == nil && processed {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
if cbErr != nil {
|
||||||
|
err = cbErr
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
if errors.Is(processErr, ErrSkipped) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if processErr != nil {
|
|
||||||
return processErr
|
|
||||||
}
|
|
||||||
if cbErr != nil && stopOnError {
|
|
||||||
return cbErr
|
|
||||||
}
|
|
||||||
if cbErr == nil && processed {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if cbErr != nil {
|
|
||||||
err = cbErr
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == nil && count < 1 {
|
if err == nil && count < 1 {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ type Show struct {
|
||||||
Pattern string
|
Pattern string
|
||||||
Name string
|
Name string
|
||||||
Anime bool
|
Anime bool
|
||||||
|
Sources []string
|
||||||
|
|
||||||
re *regexp.Regexp
|
re *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue