From eb299ce84755e7445ae866bec93940fedfea6f09 Mon Sep 17 00:00:00 2001 From: Leonardo Galli Date: Sat, 21 Jan 2017 12:57:04 +0100 Subject: [PATCH] Fix Corruped Media Cover Images. --- .../CoverAlreadyExistsSpecification.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs b/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs index 83f1e13de..6e29988a6 100644 --- a/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs +++ b/src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs @@ -1,5 +1,8 @@ -using NzbDrone.Common.Disk; +using System; +using NzbDrone.Common.Disk; using NzbDrone.Common.Http; +using System.Drawing; +using NLog; namespace NzbDrone.Core.MediaCover { @@ -12,11 +15,13 @@ namespace NzbDrone.Core.MediaCover { private readonly IDiskProvider _diskProvider; private readonly IHttpClient _httpClient; + private readonly Logger _logger; - public CoverAlreadyExistsSpecification(IDiskProvider diskProvider, IHttpClient httpClient) + public CoverAlreadyExistsSpecification(IDiskProvider diskProvider, IHttpClient httpClient, Logger logger) { _diskProvider = diskProvider; _httpClient = httpClient; + _logger = logger; } public bool AlreadyExists(string url, string path) @@ -26,9 +31,31 @@ namespace NzbDrone.Core.MediaCover return false; } + if (!IsValidGDIPlusImage(path)) + { + _diskProvider.DeleteFile(path); + return false; + } + var headers = _httpClient.Head(new HttpRequest(url)).Headers; var fileSize = _diskProvider.GetFileSize(path); return fileSize == headers.ContentLength; } + + private bool IsValidGDIPlusImage(string filename) + { + try + { + using (var bmp = new Bitmap(filename)) + { + } + return true; + } + catch (Exception ex) + { + _logger.Debug(ex, "Corrupted image found at: {0}. Redownloading...", filename); + return false; + } + } } } \ No newline at end of file