2013-03-04 08:53:02 +03:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
2013-04-24 04:56:00 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-03-04 08:53:02 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaCover
|
|
|
|
{
|
2013-03-05 22:49:34 +03:00
|
|
|
public class MediaCoverService :
|
|
|
|
IHandleAsync<SeriesUpdatedEvent>,
|
|
|
|
IHandleAsync<SeriesDeletedEvent>
|
2013-03-04 08:53:02 +03:00
|
|
|
{
|
2013-04-11 02:41:45 +03:00
|
|
|
private readonly IHttpProvider _httpProvider;
|
2013-05-11 02:53:50 +03:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-05-29 07:10:23 +03:00
|
|
|
private readonly ICoverExistsSpecification _coverExistsSpecification;
|
2013-03-04 08:53:02 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
private readonly string _coverRootFolder;
|
|
|
|
|
2013-05-29 07:10:23 +03:00
|
|
|
public MediaCoverService(IHttpProvider httpProvider, IDiskProvider diskProvider, IEnvironmentProvider environmentProvider,
|
|
|
|
ICoverExistsSpecification coverExistsSpecification, Logger logger)
|
2013-03-04 08:53:02 +03:00
|
|
|
{
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
_diskProvider = diskProvider;
|
2013-05-29 07:10:23 +03:00
|
|
|
_coverExistsSpecification = coverExistsSpecification;
|
2013-03-04 08:53:02 +03:00
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
_coverRootFolder = environmentProvider.GetMediaCoverPath();
|
|
|
|
}
|
|
|
|
|
2013-03-05 09:25:05 +03:00
|
|
|
public void HandleAsync(SeriesUpdatedEvent message)
|
2013-03-04 08:53:02 +03:00
|
|
|
{
|
|
|
|
EnsureCovers(message.Series);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureCovers(Series series)
|
|
|
|
{
|
2013-03-31 23:25:39 +03:00
|
|
|
foreach (var cover in series.Images)
|
2013-03-04 08:53:02 +03:00
|
|
|
{
|
|
|
|
var fileName = GetCoverPath(series.Id, cover.CoverType);
|
2013-05-29 07:10:23 +03:00
|
|
|
if (!_coverExistsSpecification.AlreadyExists(cover.Url, fileName))
|
2013-03-04 08:53:02 +03:00
|
|
|
{
|
|
|
|
DownloadCover(series, cover);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DownloadCover(Series series, MediaCover cover)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var fileName = GetCoverPath(series.Id, cover.CoverType);
|
|
|
|
|
2013-06-03 08:51:16 +03:00
|
|
|
_logger.Info("Downloading {0} for {1} {2}", cover.CoverType, series.Title, cover.Url);
|
2013-04-01 00:45:16 +03:00
|
|
|
_httpProvider.DownloadFile(cover.Url, fileName);
|
2013-03-04 08:53:02 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-04-17 09:55:39 +03:00
|
|
|
_logger.ErrorException("Couldn't download media cover for " + series.TvdbId, e);
|
2013-03-04 08:53:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 22:49:34 +03:00
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
{
|
|
|
|
var path = GetSeriesCoverPath(message.Series.Id);
|
|
|
|
if (_diskProvider.FolderExists(path))
|
|
|
|
{
|
|
|
|
_diskProvider.DeleteFolder(path, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 08:53:02 +03:00
|
|
|
private string GetCoverPath(int seriesId, MediaCoverTypes coverTypes)
|
|
|
|
{
|
2013-03-05 22:49:34 +03:00
|
|
|
return Path.Combine(GetSeriesCoverPath(seriesId), coverTypes.ToString().ToLower() + ".jpg");
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetSeriesCoverPath(int seriesId)
|
|
|
|
{
|
2013-05-17 06:03:52 +03:00
|
|
|
return Path.Combine(_coverRootFolder, seriesId.ToString());
|
2013-03-04 08:53:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|