2013-03-03 21:53:02 -08:00
|
|
|
using System;
|
2013-06-12 18:37:05 -07:00
|
|
|
using System.Collections.Generic;
|
2013-03-03 21:53:02 -08:00
|
|
|
using System.IO;
|
2013-07-04 22:16:49 -07:00
|
|
|
using System.Net;
|
2013-03-03 21:53:02 -08:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
2014-01-05 22:20:08 -08:00
|
|
|
using NzbDrone.Common.Disk;
|
2013-06-27 17:04:52 -07:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-01-01 22:56:19 -08:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-09-13 23:36:07 -07:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-03-03 21:53:02 -08:00
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaCover
|
|
|
|
{
|
2014-01-21 21:22:09 -08:00
|
|
|
public interface IMapCoversToLocal
|
|
|
|
{
|
|
|
|
void ConvertToLocalUrls(int seriesId, IEnumerable<MediaCover> covers);
|
|
|
|
string GetCoverPath(int seriesId, MediaCoverTypes mediaCoverTypes);
|
2014-01-25 23:14:55 -08:00
|
|
|
|
2014-01-21 21:22:09 -08:00
|
|
|
}
|
|
|
|
|
2013-03-05 11:49:34 -08:00
|
|
|
public class MediaCoverService :
|
|
|
|
IHandleAsync<SeriesUpdatedEvent>,
|
2013-06-12 18:37:05 -07:00
|
|
|
IHandleAsync<SeriesDeletedEvent>,
|
|
|
|
IMapCoversToLocal
|
2013-03-03 21:53:02 -08:00
|
|
|
{
|
2013-04-10 16:41:45 -07:00
|
|
|
private readonly IHttpProvider _httpProvider;
|
2013-05-10 16:53:50 -07:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-05-28 21:10:23 -07:00
|
|
|
private readonly ICoverExistsSpecification _coverExistsSpecification;
|
2014-01-01 22:56:19 -08:00
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2014-01-25 23:14:55 -08:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2013-03-03 21:53:02 -08:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
private readonly string _coverRootFolder;
|
|
|
|
|
2014-01-25 23:14:55 -08:00
|
|
|
public MediaCoverService(IHttpProvider httpProvider,
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
IAppFolderInfo appFolderInfo,
|
|
|
|
ICoverExistsSpecification coverExistsSpecification,
|
|
|
|
IConfigFileProvider configFileProvider,
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
Logger logger)
|
2013-03-03 21:53:02 -08:00
|
|
|
{
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
_diskProvider = diskProvider;
|
2013-05-28 21:10:23 -07:00
|
|
|
_coverExistsSpecification = coverExistsSpecification;
|
2014-01-01 22:56:19 -08:00
|
|
|
_configFileProvider = configFileProvider;
|
2014-01-25 23:14:55 -08:00
|
|
|
_eventAggregator = eventAggregator;
|
2013-03-03 21:53:02 -08:00
|
|
|
_logger = logger;
|
|
|
|
|
2013-07-04 21:43:28 -07:00
|
|
|
_coverRootFolder = appFolderInfo.GetMediaCoverPath();
|
2013-03-03 21:53:02 -08:00
|
|
|
}
|
|
|
|
|
2014-01-21 21:22:09 -08:00
|
|
|
public string GetCoverPath(int seriesId, MediaCoverTypes coverTypes)
|
2013-03-03 21:53:02 -08:00
|
|
|
{
|
2014-01-21 21:22:09 -08:00
|
|
|
return Path.Combine(GetSeriesCoverPath(seriesId), coverTypes.ToString().ToLower() + ".jpg");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ConvertToLocalUrls(int seriesId, IEnumerable<MediaCover> covers)
|
|
|
|
{
|
|
|
|
foreach (var mediaCover in covers)
|
|
|
|
{
|
|
|
|
var filePath = GetCoverPath(seriesId, mediaCover.CoverType);
|
|
|
|
|
|
|
|
mediaCover.Url = _configFileProvider.UrlBase + @"/MediaCover/" + seriesId + "/" + mediaCover.CoverType.ToString().ToLower() + ".jpg";
|
|
|
|
|
|
|
|
if (_diskProvider.FileExists(filePath))
|
|
|
|
{
|
2014-03-12 22:27:36 -07:00
|
|
|
var lastWrite = _diskProvider.FileGetLastWriteUtc(filePath);
|
2014-01-21 21:22:09 -08:00
|
|
|
mediaCover.Url += "?lastWrite=" + lastWrite.Ticks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetSeriesCoverPath(int seriesId)
|
|
|
|
{
|
|
|
|
return Path.Combine(_coverRootFolder, seriesId.ToString());
|
2013-03-03 21:53:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureCovers(Series series)
|
|
|
|
{
|
2013-03-31 13:25:39 -07:00
|
|
|
foreach (var cover in series.Images)
|
2013-03-03 21:53:02 -08:00
|
|
|
{
|
|
|
|
var fileName = GetCoverPath(series.Id, cover.CoverType);
|
2013-08-29 19:00:40 -07:00
|
|
|
try
|
2013-03-03 21:53:02 -08:00
|
|
|
{
|
2013-08-29 19:00:40 -07:00
|
|
|
if (!_coverExistsSpecification.AlreadyExists(cover.Url, fileName))
|
|
|
|
{
|
|
|
|
DownloadCover(series, cover);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (WebException e)
|
|
|
|
{
|
2013-08-29 19:05:33 -07:00
|
|
|
_logger.Warn(string.Format("Couldn't download media cover for {0}. {1}", series, e.Message));
|
2013-08-29 19:00:40 -07:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Couldn't download media cover for " + series, e);
|
2013-03-03 21:53:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DownloadCover(Series series, MediaCover cover)
|
|
|
|
{
|
2013-08-29 19:00:40 -07:00
|
|
|
var fileName = GetCoverPath(series.Id, cover.CoverType);
|
|
|
|
|
|
|
|
_logger.Info("Downloading {0} for {1} {2}", cover.CoverType, series, cover.Url);
|
|
|
|
_httpProvider.DownloadFile(cover.Url, fileName);
|
2013-03-03 21:53:02 -08:00
|
|
|
}
|
|
|
|
|
2014-01-21 21:22:09 -08:00
|
|
|
public void HandleAsync(SeriesUpdatedEvent message)
|
|
|
|
{
|
|
|
|
EnsureCovers(message.Series);
|
2014-01-25 23:14:55 -08:00
|
|
|
_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Series));
|
2014-01-21 21:22:09 -08:00
|
|
|
}
|
|
|
|
|
2013-03-05 11:49:34 -08:00
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
{
|
|
|
|
var path = GetSeriesCoverPath(message.Series.Id);
|
|
|
|
if (_diskProvider.FolderExists(path))
|
|
|
|
{
|
|
|
|
_diskProvider.DeleteFolder(path, true);
|
|
|
|
}
|
|
|
|
}
|
2013-03-03 21:53:02 -08:00
|
|
|
}
|
|
|
|
}
|