Fixed: Backup Mediacover Existing Check to Length if No Modified Date

pull/6/head
Qstick 5 years ago
parent 802f7f96c0
commit 2be52c22d6

@ -11,41 +11,46 @@ namespace NzbDrone.Core.Test.MediaCoverTests
[TestFixture] [TestFixture]
public class CoverAlreadyExistsSpecificationFixture : CoreTest<CoverAlreadyExistsSpecification> public class CoverAlreadyExistsSpecificationFixture : CoreTest<CoverAlreadyExistsSpecification>
{ {
private void GivenFileExistsOnDisk() private void GivenFileExistsOnDisk(DateTime? givenDate)
{ {
Mocker.GetMock<IDiskProvider>().Setup(c => c.FileExists(It.IsAny<string>())).Returns(true); Mocker.GetMock<IDiskProvider>().Setup(c => c.FileExists(It.IsAny<string>())).Returns(true);
Mocker.GetMock<IDiskProvider>().Setup(c => c.FileGetLastWrite(It.IsAny<string>())).Returns(givenDate ?? DateTime.Now);
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetFileSize(It.IsAny<string>())).Returns(1000);
} }
[Test]
private void GivenExistingFileDate(DateTime lastModifiedDate) public void should_return_false_if_file_not_exists()
{ {
GivenFileExistsOnDisk(); Mocker.GetMock<IDiskProvider>().Setup(c => c.FileExists(It.IsAny<string>())).Returns(false);
Mocker.GetMock<IDiskProvider>().Setup(c => c.FileGetLastWrite(It.IsAny<string>())).Returns(lastModifiedDate);
Subject.AlreadyExists(DateTime.Now, 0, "c:\\file.exe").Should().BeFalse();
} }
[Test] [Test]
public void should_return_false_if_file_not_exists() public void should_return_false_if_file_exists_but_diffrent_date()
{ {
Subject.AlreadyExists(DateTime.Now, "c:\\file.exe").Should().BeFalse(); GivenFileExistsOnDisk(DateTime.Now);
Subject.AlreadyExists(DateTime.Now.AddHours(-5), 0, "c:\\file.exe").Should().BeFalse();
} }
[Test] [Test]
public void should_return_false_if_file_exists_but_diffrent_date() public void should_return_ture_if_file_exists_and_same_date_but_no_length_header()
{ {
GivenExistingFileDate(DateTime.Now); var givenDate = DateTime.Now;
Subject.AlreadyExists(DateTime.Now.AddHours(-5), "c:\\file.exe").Should().BeFalse(); GivenFileExistsOnDisk(givenDate);
Subject.AlreadyExists(givenDate, null, "c:\\file.exe").Should().BeTrue();
} }
[Test] [Test]
public void should_return_ture_if_file_exists_and_same_date() public void should_return_ture_if_file_exists_and_date_header_is_null_but_has_length_header()
{ {
var givenDate = DateTime.Now;
GivenExistingFileDate(givenDate); GivenFileExistsOnDisk(DateTime.Now);
Subject.AlreadyExists(givenDate, "c:\\file.exe").Should().BeTrue(); Subject.AlreadyExists(null, 1000, "c:\\file.exe").Should().BeTrue();
} }
} }
} }

@ -100,7 +100,7 @@ namespace NzbDrone.Core.Test.MediaCoverTests
public void should_resize_covers_if_main_downloaded() public void should_resize_covers_if_main_downloaded()
{ {
Mocker.GetMock<ICoverExistsSpecification>() Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<DateTime>(), It.IsAny<string>())) .Setup(v => v.AlreadyExists(It.IsAny<DateTime?>(), It.IsAny<long?>(), It.IsAny<string>()))
.Returns(false); .Returns(false);
Mocker.GetMock<IAlbumService>() Mocker.GetMock<IAlbumService>()
@ -121,7 +121,7 @@ namespace NzbDrone.Core.Test.MediaCoverTests
public void should_resize_covers_if_missing() public void should_resize_covers_if_missing()
{ {
Mocker.GetMock<ICoverExistsSpecification>() Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<DateTime>(), It.IsAny<string>())) .Setup(v => v.AlreadyExists(It.IsAny<DateTime?>(), It.IsAny<long?>(), It.IsAny<string>()))
.Returns(true); .Returns(true);
Mocker.GetMock<IAlbumService>() Mocker.GetMock<IAlbumService>()
@ -142,7 +142,7 @@ namespace NzbDrone.Core.Test.MediaCoverTests
public void should_not_resize_covers_if_exists() public void should_not_resize_covers_if_exists()
{ {
Mocker.GetMock<ICoverExistsSpecification>() Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<DateTime>(), It.IsAny<string>())) .Setup(v => v.AlreadyExists(It.IsAny<DateTime?>(), It.IsAny<long?>(), It.IsAny<string>()))
.Returns(true); .Returns(true);
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
@ -167,7 +167,7 @@ namespace NzbDrone.Core.Test.MediaCoverTests
public void should_resize_covers_if_existing_is_empty() public void should_resize_covers_if_existing_is_empty()
{ {
Mocker.GetMock<ICoverExistsSpecification>() Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<DateTime>(), It.IsAny<string>())) .Setup(v => v.AlreadyExists(It.IsAny<DateTime?>(), It.IsAny<long?>(), It.IsAny<string>()))
.Returns(true); .Returns(true);
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
@ -192,7 +192,7 @@ namespace NzbDrone.Core.Test.MediaCoverTests
public void should_log_error_if_resize_failed() public void should_log_error_if_resize_failed()
{ {
Mocker.GetMock<ICoverExistsSpecification>() Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<DateTime>(), It.IsAny<string>())) .Setup(v => v.AlreadyExists(It.IsAny<DateTime?>(), It.IsAny<long?>(), It.IsAny<string>()))
.Returns(true); .Returns(true);
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()

@ -1,11 +1,12 @@
using System; using System;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Http;
namespace NzbDrone.Core.MediaCover namespace NzbDrone.Core.MediaCover
{ {
public interface ICoverExistsSpecification public interface ICoverExistsSpecification
{ {
bool AlreadyExists(DateTime serverModifiedDate, string localPath); bool AlreadyExists(DateTime? serverModifiedDate, long? serverContentLength, string localPath);
} }
public class CoverAlreadyExistsSpecification : ICoverExistsSpecification public class CoverAlreadyExistsSpecification : ICoverExistsSpecification
@ -17,16 +18,28 @@ namespace NzbDrone.Core.MediaCover
_diskProvider = diskProvider; _diskProvider = diskProvider;
} }
public bool AlreadyExists(DateTime lastModifiedDateServer, string localPath) public bool AlreadyExists(DateTime? serverModifiedDate, long? serverContentLength, string localPath)
{ {
if (!_diskProvider.FileExists(localPath)) if (!_diskProvider.FileExists(localPath))
{ {
return false; return false;
} }
if (serverModifiedDate.HasValue)
{
DateTime? lastModifiedLocal = _diskProvider.FileGetLastWrite(localPath); DateTime? lastModifiedLocal = _diskProvider.FileGetLastWrite(localPath);
return lastModifiedLocal.Value.ToUniversalTime() == lastModifiedDateServer.ToUniversalTime(); return lastModifiedLocal.Value.ToUniversalTime() == serverModifiedDate.Value.ToUniversalTime();
}
if (serverContentLength.HasValue && serverContentLength.Value > 0)
{
var fileSize = _diskProvider.GetFileSize(localPath);
return fileSize == serverContentLength;
}
return false;
} }
} }
} }

@ -115,13 +115,13 @@ namespace NzbDrone.Core.MediaCover
try try
{ {
var lastModifiedServer = GetCoverModifiedDate(cover.Url); var serverFileHeaders = _httpClient.Head(new HttpRequest(cover.Url) { AllowAutoRedirect = true }).Headers;
alreadyExists = _coverExistsSpecification.AlreadyExists(lastModifiedServer, fileName); alreadyExists = _coverExistsSpecification.AlreadyExists(serverFileHeaders.LastModified, serverFileHeaders.ContentLength, fileName);
if (!alreadyExists) if (!alreadyExists)
{ {
DownloadCover(artist, cover, lastModifiedServer); DownloadCover(artist, cover, serverFileHeaders.LastModified ?? DateTime.Now);
} }
} }
catch (WebException e) catch (WebException e)
@ -145,11 +145,13 @@ namespace NzbDrone.Core.MediaCover
var alreadyExists = false; var alreadyExists = false;
try try
{ {
var lastModifiedServer = GetCoverModifiedDate(cover.Url); var serverFileHeaders = _httpClient.Head(new HttpRequest(cover.Url) { AllowAutoRedirect = true }).Headers;
alreadyExists = _coverExistsSpecification.AlreadyExists(lastModifiedServer, fileName);
alreadyExists = _coverExistsSpecification.AlreadyExists(serverFileHeaders.LastModified, serverFileHeaders.ContentLength, fileName);
if (!alreadyExists) if (!alreadyExists)
{ {
DownloadAlbumCover(album, cover, lastModifiedServer); DownloadAlbumCover(album, cover, serverFileHeaders.LastModified ?? DateTime.Now);
} }
} }
catch (WebException e) catch (WebException e)
@ -249,20 +251,6 @@ namespace NzbDrone.Core.MediaCover
} }
} }
private DateTime GetCoverModifiedDate(string url)
{
var lastModifiedServer = DateTime.Now;
var headers = _httpClient.Head(new HttpRequest(url)).Headers;
if (headers.LastModified.HasValue)
{
lastModifiedServer = headers.LastModified.Value;
}
return lastModifiedServer;
}
private int[] GetDefaultHeights(MediaCoverTypes coverType) private int[] GetDefaultHeights(MediaCoverTypes coverType)
{ {
switch (coverType) switch (coverType)

Loading…
Cancel
Save