mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-19 13:10:13 -07:00
Fixed: Backup Mediacover Existing Check to Length if No Modified Date
This commit is contained in:
parent
802f7f96c0
commit
2be52c22d6
4 changed files with 51 additions and 45 deletions
|
@ -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);
|
||||||
|
|
||||||
private void GivenExistingFileDate(DateTime lastModifiedDate)
|
|
||||||
{
|
|
||||||
GivenFileExistsOnDisk();
|
|
||||||
Mocker.GetMock<IDiskProvider>().Setup(c => c.FileGetLastWrite(It.IsAny<string>())).Returns(lastModifiedDate);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_return_false_if_file_not_exists()
|
public void should_return_false_if_file_not_exists()
|
||||||
{
|
{
|
||||||
Subject.AlreadyExists(DateTime.Now, "c:\\file.exe").Should().BeFalse();
|
Mocker.GetMock<IDiskProvider>().Setup(c => c.FileExists(It.IsAny<string>())).Returns(false);
|
||||||
|
|
||||||
|
Subject.AlreadyExists(DateTime.Now, 0, "c:\\file.exe").Should().BeFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_return_false_if_file_exists_but_diffrent_date()
|
public void should_return_false_if_file_exists_but_diffrent_date()
|
||||||
{
|
{
|
||||||
GivenExistingFileDate(DateTime.Now);
|
GivenFileExistsOnDisk(DateTime.Now);
|
||||||
|
|
||||||
Subject.AlreadyExists(DateTime.Now.AddHours(-5), "c:\\file.exe").Should().BeFalse();
|
Subject.AlreadyExists(DateTime.Now.AddHours(-5), 0, "c:\\file.exe").Should().BeFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_return_ture_if_file_exists_and_same_date()
|
public void should_return_ture_if_file_exists_and_same_date_but_no_length_header()
|
||||||
{
|
{
|
||||||
var givenDate = DateTime.Now;
|
var givenDate = DateTime.Now;
|
||||||
|
|
||||||
GivenExistingFileDate(givenDate);
|
GivenFileExistsOnDisk(givenDate);
|
||||||
|
|
||||||
Subject.AlreadyExists(givenDate, "c:\\file.exe").Should().BeTrue();
|
Subject.AlreadyExists(givenDate, null, "c:\\file.exe").Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_return_ture_if_file_exists_and_date_header_is_null_but_has_length_header()
|
||||||
|
{
|
||||||
|
|
||||||
|
GivenFileExistsOnDisk(DateTime.Now);
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
DateTime? lastModifiedLocal = _diskProvider.FileGetLastWrite(localPath);
|
if (serverModifiedDate.HasValue)
|
||||||
|
{
|
||||||
|
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…
Add table
Add a link
Reference in a new issue