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/
pull/1689/head
ta264 5 years ago committed by Qstick
parent ed357181ef
commit aa66358725

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

@ -1,9 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Marr.Data;
using Marr.Data.QGen;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Extensions;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Music;
@ -80,8 +79,10 @@ namespace NzbDrone.Core.MediaFiles
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
.Where(x => x.Path.StartsWith(path))
.Where(x => x.Path.StartsWith(safePath))
.ToList();
}

Loading…
Cancel
Save