1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core/Providers/BannerProvider.cs
Mark McDowall 861f6c1a0c Fixed broken tests.
Fixed spelling mistake.
2012-03-06 23:34:26 -08:00

87 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers
{
public class BannerProvider
{
private readonly HttpProvider _httpProvider;
private readonly EnvironmentProvider _environmentProvider;
private readonly DiskProvider _diskProvider;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private const string BANNER_URL_PREFIX = "http://www.thetvdb.com/banners/";
public BannerProvider(HttpProvider httpProvider, EnvironmentProvider environmentProvider,
DiskProvider diskProvider)
{
_httpProvider = httpProvider;
_environmentProvider = environmentProvider;
_diskProvider = diskProvider;
}
public BannerProvider()
{
}
public virtual bool Download(ProgressNotification notification, Series series)
{
//var bannerPath = _environmentProvider.GetBannerPath();
logger.Trace("Ensuring Banner Folder exists: ", _environmentProvider.GetBannerPath());
_diskProvider.CreateDirectory(_environmentProvider.GetBannerPath());
var bannerFilename = Path.Combine(_environmentProvider.GetBannerPath(), series.SeriesId.ToString()) + ".jpg";
notification.CurrentMessage = string.Format("Downloading banner for '{0}'", series.Title);
logger.Trace("Downloading banner for '{0}'", series.Title);
try
{
_httpProvider.DownloadFile(BANNER_URL_PREFIX + series.BannerUrl, bannerFilename);
notification.CurrentMessage = string.Format("Successfully download banner for '{0}'", series.Title);
logger.Trace("Successfully download banner for '{0}'", series.Title);
}
catch (Exception)
{
logger.Debug("Failed to download banner for '{0}'", series.Title);
notification.CurrentMessage = string.Format("Failed to download banner for '{0}'", series.Title);
return false;
}
return true;
}
public virtual bool Delete(int seriesId)
{
var bannerPath = _environmentProvider.GetBannerPath();
var bannerFilename = Path.Combine(bannerPath, seriesId.ToString()) + ".jpg";
try
{
logger.Trace("Checking if banner exists: {0}", bannerFilename);
if (_diskProvider.FileExists(bannerFilename))
{
logger.Trace("Deleting existing banner: {0}", bannerFilename);
_diskProvider.DeleteFile(bannerFilename);
}
}
catch(Exception ex)
{
logger.WarnException("Failed to delete banner: " + bannerFilename, ex);
return false;
}
return true;
}
}
}