Fixed: Prevent GetFilesByBasePath matching partial paths

When searching for /test/path it was returning files in /test/path2.
Fix by making sure to search for /test/path/
This commit is contained in:
ta264 2019-10-01 21:33:28 +01:00 committed by Qstick
parent ed357181ef
commit aa66358725
2 changed files with 33 additions and 9 deletions

View File

@ -7,6 +7,7 @@ using NzbDrone.Core.Music;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFiles namespace NzbDrone.Core.Test.MediaFiles
{ {
@ -55,9 +56,9 @@ namespace NzbDrone.Core.Test.MediaFiles
.TheFirst(5) .TheFirst(5)
.With(c => c.AlbumId = album.Id) .With(c => c.AlbumId = album.Id)
.TheFirst(1) .TheFirst(1)
.With(c => c.Path = "/Test/Path/Artist/somefile1.flac") .With(c => c.Path = @"C:\Test\Path\Artist\somefile1.flac".AsOsAgnostic())
.TheNext(1) .TheNext(1)
.With(c => c.Path = "/Test/Path/Artist/somefile2.flac") .With(c => c.Path = @"C:\Test\Path\Artist\somefile2.flac".AsOsAgnostic())
.BuildListOfNew(); .BuildListOfNew();
Db.InsertMany(files); Db.InsertMany(files);
@ -116,21 +117,43 @@ namespace NzbDrone.Core.Test.MediaFiles
secondReleaseFiles.Should().HaveCount(1); secondReleaseFiles.Should().HaveCount(1);
} }
[Test] [TestCase("C:\\Test\\Path")]
public void get_files_by_base_path() [TestCase("C:\\Test\\Path\\")]
public void get_files_by_base_path_should_cope_with_trailing_slash(string dir)
{ {
VerifyData(); VerifyData();
var firstReleaseFiles = Subject.GetFilesWithBasePath("/Test/Path"); var firstReleaseFiles = Subject.GetFilesWithBasePath(dir.AsOsAgnostic());
VerifyEagerLoaded(firstReleaseFiles); VerifyEagerLoaded(firstReleaseFiles);
firstReleaseFiles.Should().HaveCount(2); firstReleaseFiles.Should().HaveCount(2);
} }
[TestCase("C:\\Test\\Path")]
[TestCase("C:\\Test\\Path\\")]
public void get_files_by_base_path_should_not_get_files_for_partial_path(string dir)
{
VerifyData();
var files = Builder<TrackFile>.CreateListOfSize(2)
.All()
.With(c => c.Id = 0)
.With(c => c.Quality =new QualityModel(Quality.MP3_192))
.TheFirst(1)
.With(c => c.Path = @"C:\Test\Path2\Artist\somefile1.flac".AsOsAgnostic())
.TheNext(1)
.With(c => c.Path = @"C:\Test\Path2\Artist\somefile2.flac".AsOsAgnostic())
.BuildListOfNew();
Db.InsertMany(files);
var firstReleaseFiles = Subject.GetFilesWithBasePath(dir.AsOsAgnostic());
firstReleaseFiles.Should().HaveCount(2);
}
[Test] [Test]
public void get_file_by_path() public void get_file_by_path()
{ {
VerifyData(); VerifyData();
var file = Subject.GetFileWithPath("/Test/Path/Artist/somefile2.flac"); var file = Subject.GetFileWithPath(@"C:\Test\Path\Artist\somefile2.flac".AsOsAgnostic());
file.Should().NotBeNull(); file.Should().NotBeNull();
file.Tracks.IsLoaded.Should().BeTrue(); file.Tracks.IsLoaded.Should().BeTrue();

View File

@ -1,9 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using Marr.Data;
using Marr.Data.QGen; using Marr.Data.QGen;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Extensions;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Music; using NzbDrone.Core.Music;
@ -80,8 +79,10 @@ namespace NzbDrone.Core.MediaFiles
public List<TrackFile> GetFilesWithBasePath(string path) public List<TrackFile> GetFilesWithBasePath(string path)
{ {
// ensure path ends with a single trailing path separator to avoid matching partial paths
var safePath = path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
return Query return Query
.Where(x => x.Path.StartsWith(path)) .Where(x => x.Path.StartsWith(safePath))
.ToList(); .ToList();
} }