Fixed: Use original file path when calculating preferred word score for existing file

Closes #3488
Closes #3913
This commit is contained in:
Mark McDowall 2021-02-13 17:12:54 -08:00
parent 3c45349404
commit 10c770b116
2 changed files with 56 additions and 0 deletions

View File

@ -1,3 +1,4 @@
using System.IO;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
@ -76,5 +77,37 @@ namespace NzbDrone.Core.Test.MediaFiles
Subject.Calculate(_series, _episodeFile).Should().Be(20);
}
[Test]
public void should_return_score_for_original_path_folder_name_if_highest()
{
var folderName = "folder-name";
var fileName = "file-name";
_episodeFile.OriginalFilePath = Path.Combine(folderName, fileName);
GivenPreferredWordScore(_episodeFile.RelativePath, 20);
GivenPreferredWordScore(_episodeFile.Path, 50);
GivenPreferredWordScore(folderName, 60);
GivenPreferredWordScore(fileName, 50);
Subject.Calculate(_series, _episodeFile).Should().Be(60);
}
[Test]
public void should_return_score_for_original_path_file_name_if_highest()
{
var folderName = "folder-name";
var fileName = "file-name";
_episodeFile.OriginalFilePath = Path.Combine(folderName, fileName);
GivenPreferredWordScore(_episodeFile.RelativePath, 20);
GivenPreferredWordScore(_episodeFile.Path, 50);
GivenPreferredWordScore(folderName, 40);
GivenPreferredWordScore(fileName, 50);
Subject.Calculate(_series, _episodeFile).Should().Be(50);
}
}
}

View File

@ -1,4 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI;
using Newtonsoft.Json.Serialization;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Profiles.Releases;
@ -21,6 +25,7 @@ namespace NzbDrone.Core.MediaFiles
_preferredWordService = preferredWordService;
_logger = logger;
}
public int Calculate(Series series, EpisodeFile episodeFile)
{
var scores = new List<int>();
@ -34,6 +39,24 @@ namespace NzbDrone.Core.MediaFiles
_logger.Trace("No stored scene name for {0}", episodeFile);
}
// The file may not have a screen name if the file/folder name contained spaces, but the original path is still store and valuable.
if (episodeFile.OriginalFilePath.IsNotNullOrWhiteSpace())
{
var segments = episodeFile.OriginalFilePath.Split(Path.DirectorySeparatorChar).ToList();
for (int i = 0; i < segments.Count; i++)
{
var isLast = i == segments.Count - 1;
var segment = isLast ? Path.GetFileNameWithoutExtension(segments[i]) : segments[i];
scores.Add(_preferredWordService.Calculate(series, segment, 0));
}
}
else
{
_logger.Trace("No stored scene name for {0}", episodeFile);
}
// Calculate using RelativePath or Path, but not both
if (episodeFile.RelativePath.IsNotNullOrWhiteSpace())
{