2011-12-02 08:51:32 +01:00
|
|
|
using System.Linq;
|
2011-12-02 02:33:17 +01:00
|
|
|
using System;
|
2011-08-23 07:29:12 +02:00
|
|
|
using NLog;
|
2011-12-02 02:33:17 +01:00
|
|
|
using Ninject;
|
2011-08-23 07:29:12 +02:00
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-12-02 08:51:32 +01:00
|
|
|
using NzbDrone.Core.Providers;
|
2011-08-23 07:29:12 +02:00
|
|
|
|
2011-12-02 08:51:32 +01:00
|
|
|
namespace NzbDrone.Core.Jobs
|
2011-08-23 07:29:12 +02:00
|
|
|
{
|
|
|
|
public class RenameSeriesJob : IJob
|
|
|
|
{
|
|
|
|
private readonly MediaFileProvider _mediaFileProvider;
|
|
|
|
private readonly DiskScanProvider _diskScanProvider;
|
2012-01-05 04:40:25 +01:00
|
|
|
private readonly ExternalNotificationProvider _externalNotificationProvider;
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
2011-08-23 07:29:12 +02:00
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
[Inject]
|
2012-01-05 04:40:25 +01:00
|
|
|
public RenameSeriesJob(MediaFileProvider mediaFileProvider, DiskScanProvider diskScanProvider,
|
|
|
|
ExternalNotificationProvider externalNotificationProvider, SeriesProvider seriesProvider)
|
2011-08-23 07:29:12 +02:00
|
|
|
{
|
|
|
|
_mediaFileProvider = mediaFileProvider;
|
|
|
|
_diskScanProvider = diskScanProvider;
|
2012-01-05 04:40:25 +01:00
|
|
|
_externalNotificationProvider = externalNotificationProvider;
|
|
|
|
_seriesProvider = seriesProvider;
|
2011-08-23 07:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return "Rename Series"; }
|
|
|
|
}
|
|
|
|
|
2012-01-15 03:47:23 +01:00
|
|
|
public TimeSpan DefaultInterval
|
2011-08-23 07:29:12 +02:00
|
|
|
{
|
2012-01-15 03:47:23 +01:00
|
|
|
get { return TimeSpan.FromTicks(0); }
|
2011-08-23 07:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
|
|
|
|
{
|
|
|
|
if (targetId <= 0)
|
|
|
|
throw new ArgumentOutOfRangeException("targetId");
|
|
|
|
|
2012-02-25 03:01:53 +01:00
|
|
|
var series = _seriesProvider.GetSeries(targetId);
|
|
|
|
|
|
|
|
notification.CurrentMessage = String.Format("Renaming episodes for '{0}'", series.Title);
|
|
|
|
|
|
|
|
Logger.Debug("Getting episodes from database for series: {0}", series.SeriesId);
|
|
|
|
var episodeFiles = _mediaFileProvider.GetSeriesFiles(series.SeriesId);
|
2011-08-23 07:29:12 +02:00
|
|
|
|
|
|
|
if (episodeFiles == null || episodeFiles.Count == 0)
|
|
|
|
{
|
2012-02-25 03:01:53 +01:00
|
|
|
Logger.Warn("No episodes in database found for series: {0}", series.SeriesId);
|
2011-08-23 07:29:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var episodeFile in episodeFiles)
|
|
|
|
{
|
2012-02-25 03:01:53 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_diskScanProvider.MoveEpisodeFile(episodeFile);
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
Logger.WarnException("An error has occurred while renaming file", e);
|
|
|
|
}
|
|
|
|
|
2011-08-23 07:29:12 +02:00
|
|
|
}
|
|
|
|
|
2012-01-05 04:40:25 +01:00
|
|
|
//Start AfterRename
|
2012-02-25 03:01:53 +01:00
|
|
|
|
2012-01-05 04:40:25 +01:00
|
|
|
var message = String.Format("Renamed: Series {0}", series.Title);
|
|
|
|
_externalNotificationProvider.AfterRename(message, series);
|
|
|
|
|
2012-02-25 03:01:53 +01:00
|
|
|
notification.CurrentMessage = String.Format("Rename completed for {0}", series.Title);
|
2011-08-23 07:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|