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