You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.3 KiB
100 lines
3.3 KiB
12 years ago
|
using System;
|
||
13 years ago
|
using System.Collections.Generic;
|
||
13 years ago
|
using System.Linq;
|
||
13 years ago
|
using NLog;
|
||
12 years ago
|
using NzbDrone.Common.Eventing;
|
||
|
using NzbDrone.Core.Download;
|
||
12 years ago
|
using NzbDrone.Core.MediaFiles;
|
||
13 years ago
|
using NzbDrone.Core.Model.Notification;
|
||
13 years ago
|
using NzbDrone.Core.Providers;
|
||
12 years ago
|
using NzbDrone.Core.Tv;
|
||
13 years ago
|
|
||
12 years ago
|
namespace NzbDrone.Core.Jobs.Implementations
|
||
13 years ago
|
{
|
||
|
public class RenameSeriesJob : IJob
|
||
|
{
|
||
12 years ago
|
private readonly IMediaFileService _mediaFileService;
|
||
13 years ago
|
private readonly DiskScanProvider _diskScanProvider;
|
||
12 years ago
|
private readonly ISeriesRepository _seriesRepository;
|
||
12 years ago
|
private readonly IEventAggregator _eventAggregator;
|
||
13 years ago
|
|
||
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||
|
|
||
12 years ago
|
public RenameSeriesJob(IMediaFileService mediaFileService, DiskScanProvider diskScanProvider,
|
||
12 years ago
|
ISeriesRepository seriesRepository,IEventAggregator eventAggregator)
|
||
13 years ago
|
{
|
||
12 years ago
|
_mediaFileService = mediaFileService;
|
||
13 years ago
|
_diskScanProvider = diskScanProvider;
|
||
12 years ago
|
_seriesRepository = seriesRepository;
|
||
12 years ago
|
_eventAggregator = eventAggregator;
|
||
13 years ago
|
}
|
||
|
|
||
|
public string Name
|
||
|
{
|
||
|
get { return "Rename Series"; }
|
||
|
}
|
||
|
|
||
13 years ago
|
public TimeSpan DefaultInterval
|
||
13 years ago
|
{
|
||
13 years ago
|
get { return TimeSpan.FromTicks(0); }
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
public void Start(ProgressNotification notification, dynamic options)
|
||
13 years ago
|
{
|
||
12 years ago
|
List<Series> seriesToRename;
|
||
|
|
||
12 years ago
|
if (options == null || options.SeriesId <= 0)
|
||
12 years ago
|
{
|
||
12 years ago
|
seriesToRename = _seriesRepository.All().ToList();
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
else
|
||
|
{
|
||
12 years ago
|
seriesToRename = new List<Series>{ _seriesRepository.Get((int)options.SeriesId) };
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
foreach(var series in seriesToRename)
|
||
|
{
|
||
|
notification.CurrentMessage = String.Format("Renaming episodes for '{0}'", series.Title);
|
||
13 years ago
|
|
||
12 years ago
|
Logger.Debug("Getting episodes from database for series: {0}", series.Id);
|
||
12 years ago
|
var episodeFiles = _mediaFileService.GetFilesBySeries(series.Id);
|
||
13 years ago
|
|
||
12 years ago
|
if (episodeFiles == null || episodeFiles.Count == 0)
|
||
|
{
|
||
12 years ago
|
Logger.Warn("No episodes in database found for series: {0}", series.Id);
|
||
12 years ago
|
return;
|
||
|
}
|
||
13 years ago
|
|
||
12 years ago
|
var newEpisodeFiles = new List<EpisodeFile>();
|
||
|
var oldEpisodeFiles = new List<EpisodeFile>();
|
||
13 years ago
|
|
||
12 years ago
|
foreach (var episodeFile in episodeFiles)
|
||
13 years ago
|
{
|
||
12 years ago
|
try
|
||
|
{
|
||
|
var oldFile = new EpisodeFile(episodeFile);
|
||
|
var newFile = _diskScanProvider.MoveEpisodeFile(episodeFile);
|
||
|
|
||
|
if (newFile != null)
|
||
|
{
|
||
|
newEpisodeFiles.Add(newFile);
|
||
|
oldEpisodeFiles.Add(oldFile);
|
||
|
}
|
||
|
}
|
||
13 years ago
|
|
||
12 years ago
|
catch (Exception e)
|
||
13 years ago
|
{
|
||
12 years ago
|
Logger.WarnException("An error has occurred while renaming file", e);
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
//Start AfterRename
|
||
13 years ago
|
|
||
12 years ago
|
_eventAggregator.Publish(new SeriesRenamedEvent(series));
|
||
13 years ago
|
|
||
12 years ago
|
notification.CurrentMessage = String.Format("Rename completed for {0}", series.Title);
|
||
|
}
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|