1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-16 16:04:08 +01:00
Radarr/NzbDrone.Core/MediaCover/MediaCoverService.cs

108 lines
3.5 KiB
C#
Raw Normal View History

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