Prefer studio name later in filename (#2057)

This commit is contained in:
WithoutPants
2021-11-26 08:29:25 +11:00
committed by GitHub
parent ff32f22c1a
commit 19e69f5310
5 changed files with 118 additions and 35 deletions

View File

@@ -58,7 +58,9 @@ func getPathWords(path string) []string {
return ret
}
func nameMatchesPath(name, path string) bool {
// nameMatchesPath returns the index in the path for the right-most match.
// Returns -1 if not found.
func nameMatchesPath(name, path string) int {
// escape specific regex characters
name = regexp.QuoteMeta(name)
@@ -72,7 +74,13 @@ func nameMatchesPath(name, path string) bool {
reStr = `(?:^|_|[^\w\d])` + reStr + `(?:$|_|[^\w\d])`
re := regexp.MustCompile(reStr)
return re.MatchString(path)
found := re.FindAllStringIndex(path, -1)
if found == nil {
return -1
}
return found[len(found)-1][0]
}
func PathToPerformers(path string, performerReader models.PerformerReader) ([]*models.Performer, error) {
@@ -86,7 +94,7 @@ func PathToPerformers(path string, performerReader models.PerformerReader) ([]*m
var ret []*models.Performer
for _, p := range performers {
// TODO - commenting out alias handling until both sides work correctly
if nameMatchesPath(p.Name.String, path) { // || nameMatchesPath(p.Aliases.String, path) {
if nameMatchesPath(p.Name.String, path) != -1 { // || nameMatchesPath(p.Aliases.String, path) {
ret = append(ret, p)
}
}
@@ -94,7 +102,10 @@ func PathToPerformers(path string, performerReader models.PerformerReader) ([]*m
return ret, nil
}
func PathToStudios(path string, reader models.StudioReader) ([]*models.Studio, error) {
// PathToStudio returns the Studio that matches the given path.
// Where multiple matching studios are found, the one that matches the latest
// position in the path is returned.
func PathToStudio(path string, reader models.StudioReader) (*models.Studio, error) {
words := getPathWords(path)
candidates, err := reader.QueryForAutoTag(words)
@@ -102,29 +113,26 @@ func PathToStudios(path string, reader models.StudioReader) ([]*models.Studio, e
return nil, err
}
var ret []*models.Studio
var ret *models.Studio
index := -1
for _, c := range candidates {
matches := false
if nameMatchesPath(c.Name.String, path) {
matches = true
matchIndex := nameMatchesPath(c.Name.String, path)
if matchIndex != -1 && matchIndex > index {
ret = c
index = matchIndex
}
if !matches {
aliases, err := reader.GetAliases(c.ID)
if err != nil {
return nil, err
}
for _, alias := range aliases {
if nameMatchesPath(alias, path) {
matches = true
break
}
}
aliases, err := reader.GetAliases(c.ID)
if err != nil {
return nil, err
}
if matches {
ret = append(ret, c)
for _, alias := range aliases {
matchIndex = nameMatchesPath(alias, path)
if matchIndex != -1 && matchIndex > index {
ret = c
index = matchIndex
}
}
}
@@ -142,7 +150,7 @@ func PathToTags(path string, tagReader models.TagReader) ([]*models.Tag, error)
var ret []*models.Tag
for _, t := range tags {
matches := false
if nameMatchesPath(t.Name, path) {
if nameMatchesPath(t.Name, path) != -1 {
matches = true
}
@@ -152,7 +160,7 @@ func PathToTags(path string, tagReader models.TagReader) ([]*models.Tag, error)
return nil, err
}
for _, alias := range aliases {
if nameMatchesPath(alias, path) {
if nameMatchesPath(alias, path) != -1 {
matches = true
break
}
@@ -223,7 +231,7 @@ func PathToScenes(name string, paths []string, sceneReader models.SceneReader) (
var ret []*models.Scene
for _, p := range scenes {
if nameMatchesPath(name, p.Path) {
if nameMatchesPath(name, p.Path) != -1 {
ret = append(ret, p)
}
}
@@ -287,7 +295,7 @@ func PathToImages(name string, paths []string, imageReader models.ImageReader) (
var ret []*models.Image
for _, p := range images {
if nameMatchesPath(name, p.Path) {
if nameMatchesPath(name, p.Path) != -1 {
ret = append(ret, p)
}
}
@@ -351,7 +359,7 @@ func PathToGalleries(name string, paths []string, galleryReader models.GalleryRe
var ret []*models.Gallery
for _, p := range gallerys {
if nameMatchesPath(name, p.Path.String) {
if nameMatchesPath(name, p.Path.String) != -1 {
ret = append(ret, p)
}
}