2011-02-22 06:22:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using NLog;
|
2011-03-03 08:50:33 +00:00
|
|
|
|
using NzbDrone.Core.Helpers;
|
2011-02-22 06:22:40 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-04-04 03:50:12 +00:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-02-22 06:22:40 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
2011-04-09 00:08:03 +00:00
|
|
|
|
public class RenameProvider
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
2011-04-10 02:44:01 +00:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-10 01:34:36 +00:00
|
|
|
|
private readonly ConfigProvider _configProvider;
|
2011-04-10 02:44:01 +00:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
|
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
|
|
|
|
private readonly List<EpisodeRenameModel> _epsToRename = new List<EpisodeRenameModel>();
|
2011-04-08 16:24:17 +00:00
|
|
|
|
private readonly ExternalNotificationProvider _externalNotificationProvider;
|
2011-04-10 02:44:01 +00:00
|
|
|
|
private readonly MediaFileProvider _mediaFileProvider;
|
|
|
|
|
private readonly SeasonProvider _seasonProvider;
|
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
2011-03-06 20:45:35 +00:00
|
|
|
|
|
2011-02-22 06:22:40 +00:00
|
|
|
|
private Thread _renameThread;
|
|
|
|
|
|
2011-04-10 01:34:36 +00:00
|
|
|
|
public RenameProvider(SeriesProvider seriesProvider, SeasonProvider seasonProvider,
|
2011-04-10 02:44:01 +00:00
|
|
|
|
EpisodeProvider episodeProvider, MediaFileProvider mediaFileProvider,
|
|
|
|
|
DiskProvider diskProvider, ConfigProvider configProvider,
|
|
|
|
|
ExternalNotificationProvider extenalNotificationProvider)
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_seasonProvider = seasonProvider;
|
|
|
|
|
_episodeProvider = episodeProvider;
|
|
|
|
|
_mediaFileProvider = mediaFileProvider;
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_configProvider = configProvider;
|
2011-03-06 20:45:35 +00:00
|
|
|
|
_externalNotificationProvider = extenalNotificationProvider;
|
2011-02-22 06:22:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 00:08:03 +00:00
|
|
|
|
public virtual void RenameAll()
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
|
|
|
|
//Get a list of all episode files/episodes and rename them
|
|
|
|
|
|
2011-02-24 01:23:58 +00:00
|
|
|
|
foreach (var episodeFile in _mediaFileProvider.GetEpisodeFiles())
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
2011-02-24 01:23:58 +00:00
|
|
|
|
var series = _seriesProvider.GetSeries(episodeFile.SeriesId);
|
2011-04-10 02:44:01 +00:00
|
|
|
|
var erm = new EpisodeRenameModel {SeriesName = series.Title, Folder = series.Path};
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
|
|
|
|
if (series.SeasonFolder)
|
2011-04-10 02:44:01 +00:00
|
|
|
|
erm.Folder += Path.DirectorySeparatorChar +
|
|
|
|
|
EpisodeRenameHelper.GetSeasonFolder(episodeFile.Episodes[0].SeasonNumber,
|
|
|
|
|
_configProvider.GetValue(
|
|
|
|
|
"Sorting_SeasonFolderFormat", "Season %s",
|
|
|
|
|
true));
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
2011-02-24 01:23:58 +00:00
|
|
|
|
erm.EpisodeFile = episodeFile;
|
|
|
|
|
_epsToRename.Add(erm);
|
|
|
|
|
StartRename();
|
2011-02-22 06:22:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-09 00:08:03 +00:00
|
|
|
|
public virtual void RenameSeries(int seriesId)
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
|
|
|
|
//Get a list of all applicable episode files/episodes and rename them
|
|
|
|
|
|
|
|
|
|
var series = _seriesProvider.GetSeries(seriesId);
|
|
|
|
|
|
2011-02-24 01:23:58 +00:00
|
|
|
|
foreach (var episodeFile in _mediaFileProvider.GetEpisodeFiles().Where(s => s.SeriesId == seriesId))
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
2011-04-10 02:44:01 +00:00
|
|
|
|
var erm = new EpisodeRenameModel {SeriesName = series.Title, Folder = series.Path};
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
|
|
|
|
if (series.SeasonFolder)
|
2011-04-10 02:44:01 +00:00
|
|
|
|
erm.Folder += Path.DirectorySeparatorChar +
|
|
|
|
|
EpisodeRenameHelper.GetSeasonFolder(episodeFile.Episodes[0].SeasonNumber,
|
|
|
|
|
_configProvider.GetValue(
|
|
|
|
|
"Sorting_SeasonFolderFormat", "Season %s",
|
|
|
|
|
true));
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
2011-02-24 01:23:58 +00:00
|
|
|
|
erm.EpisodeFile = episodeFile;
|
|
|
|
|
_epsToRename.Add(erm);
|
2011-02-22 06:22:40 +00:00
|
|
|
|
StartRename();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-09 00:08:03 +00:00
|
|
|
|
public virtual void RenameSeason(int seasonId)
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
|
|
|
|
//Get a list of all applicable episode files/episodes and rename them
|
|
|
|
|
var season = _seasonProvider.GetSeason(seasonId);
|
|
|
|
|
var series = _seriesProvider.GetSeries(season.SeriesId);
|
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
foreach (
|
|
|
|
|
var episodeFile in _mediaFileProvider.GetEpisodeFiles().Where(s => s.Episodes[0].SeasonId == seasonId))
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
2011-04-10 02:44:01 +00:00
|
|
|
|
var erm = new EpisodeRenameModel {SeriesName = series.Title, Folder = series.Path};
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
|
|
|
|
if (series.SeasonFolder)
|
2011-04-10 02:44:01 +00:00
|
|
|
|
erm.Folder += Path.DirectorySeparatorChar +
|
|
|
|
|
EpisodeRenameHelper.GetSeasonFolder(episodeFile.Episodes[0].SeasonNumber,
|
|
|
|
|
_configProvider.GetValue(
|
|
|
|
|
"Sorting_SeasonFolderFormat", "Season %s",
|
|
|
|
|
true));
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
2011-02-24 01:23:58 +00:00
|
|
|
|
erm.EpisodeFile = episodeFile;
|
|
|
|
|
_epsToRename.Add(erm);
|
2011-02-22 06:22:40 +00:00
|
|
|
|
StartRename();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-09 00:08:03 +00:00
|
|
|
|
public virtual void RenameEpisode(int episodeId)
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
2011-02-24 01:23:58 +00:00
|
|
|
|
//This will properly rename multi-episode files if asked to rename either of the episode
|
2011-02-22 06:22:40 +00:00
|
|
|
|
var episode = _episodeProvider.GetEpisode(episodeId);
|
|
|
|
|
var series = _seriesProvider.GetSeries(episode.SeriesId);
|
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
var episodeFile =
|
|
|
|
|
_mediaFileProvider.GetEpisodeFiles().Where(s => s.Episodes.Contains(episode)).FirstOrDefault();
|
2011-02-22 06:22:40 +00:00
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
var erm = new EpisodeRenameModel {SeriesName = series.Title, Folder = series.Path};
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
|
|
|
|
if (series.SeasonFolder)
|
2011-04-10 02:44:01 +00:00
|
|
|
|
erm.Folder += Path.DirectorySeparatorChar +
|
|
|
|
|
EpisodeRenameHelper.GetSeasonFolder(episodeFile.Episodes[0].SeasonNumber,
|
|
|
|
|
_configProvider.GetValue(
|
|
|
|
|
"Sorting_SeasonFolderFormat", "Season %s", true));
|
2011-03-03 08:50:33 +00:00
|
|
|
|
|
|
|
|
|
erm.EpisodeFile = episodeFile;
|
|
|
|
|
_epsToRename.Add(erm);
|
|
|
|
|
StartRename();
|
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-09 00:08:03 +00:00
|
|
|
|
public virtual void RenameEpisodeFile(int episodeFileId, bool newDownload)
|
2011-03-03 08:50:33 +00:00
|
|
|
|
{
|
|
|
|
|
//This will properly rename multi-episode files if asked to rename either of the episode
|
|
|
|
|
var episodeFile = _mediaFileProvider.GetEpisodeFile(episodeFileId);
|
|
|
|
|
var series = _seriesProvider.GetSeries(episodeFile.Series.SeriesId);
|
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
var erm = new EpisodeRenameModel {SeriesName = series.Title, Folder = series.Path};
|
2011-03-03 08:50:33 +00:00
|
|
|
|
|
|
|
|
|
if (series.SeasonFolder)
|
2011-04-10 02:44:01 +00:00
|
|
|
|
erm.Folder += Path.DirectorySeparatorChar +
|
|
|
|
|
EpisodeRenameHelper.GetSeasonFolder(episodeFile.Episodes[0].SeasonNumber,
|
|
|
|
|
_configProvider.GetValue(
|
|
|
|
|
"Sorting_SeasonFolderFormat", "Season %s", true));
|
2011-02-25 07:20:24 +00:00
|
|
|
|
|
2011-02-24 01:23:58 +00:00
|
|
|
|
erm.EpisodeFile = episodeFile;
|
|
|
|
|
_epsToRename.Add(erm);
|
2011-02-22 06:22:40 +00:00
|
|
|
|
StartRename();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartRename()
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Episode Rename Starting");
|
|
|
|
|
if (_renameThread == null || !_renameThread.IsAlive)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Initializing background rename of episodes");
|
|
|
|
|
_renameThread = new Thread(RenameProcessor)
|
2011-04-10 02:44:01 +00:00
|
|
|
|
{
|
|
|
|
|
Name = "RenameEpisodes",
|
|
|
|
|
Priority = ThreadPriority.Lowest
|
|
|
|
|
};
|
2011-02-22 06:22:40 +00:00
|
|
|
|
|
|
|
|
|
_renameThread.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Episode renaming already in progress. Ignoring request.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RenameProcessor()
|
|
|
|
|
{
|
|
|
|
|
while (_epsToRename.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var ep = _epsToRename.First();
|
|
|
|
|
_epsToRename.RemoveAt(0);
|
|
|
|
|
RenameFile(ep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-24 01:41:18 +00:00
|
|
|
|
private void RenameFile(EpisodeRenameModel erm)
|
2011-02-22 06:22:40 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//Update EpisodeFile if successful
|
2011-02-24 01:41:18 +00:00
|
|
|
|
Logger.Debug("Renaming Episode: {0}", Path.GetFileName(erm.EpisodeFile.Path));
|
2011-03-03 08:50:33 +00:00
|
|
|
|
var newName = EpisodeRenameHelper.GetNewName(erm);
|
2011-02-25 03:52:06 +00:00
|
|
|
|
var ext = Path.GetExtension(erm.EpisodeFile.Path);
|
|
|
|
|
var newFilename = erm.Folder + Path.DirectorySeparatorChar + newName + ext;
|
2011-02-22 06:22:40 +00:00
|
|
|
|
|
2011-02-24 01:41:18 +00:00
|
|
|
|
if (!_diskProvider.FolderExists(erm.Folder))
|
|
|
|
|
_diskProvider.CreateDirectory(erm.Folder);
|
2011-02-22 06:22:40 +00:00
|
|
|
|
|
2011-02-24 01:41:18 +00:00
|
|
|
|
if (erm.EpisodeFile.Path == newFilename)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_diskProvider.RenameFile(erm.EpisodeFile.Path, newFilename);
|
|
|
|
|
erm.EpisodeFile.Path = newFilename;
|
|
|
|
|
_mediaFileProvider.Update(erm.EpisodeFile);
|
2011-02-22 06:22:40 +00:00
|
|
|
|
|
2011-03-06 20:45:35 +00:00
|
|
|
|
if (erm.NewDownload)
|
2011-03-06 21:36:09 +00:00
|
|
|
|
_externalNotificationProvider.OnDownload(erm);
|
2011-03-06 20:45:35 +00:00
|
|
|
|
|
|
|
|
|
else
|
2011-03-06 21:36:09 +00:00
|
|
|
|
_externalNotificationProvider.OnRename(erm);
|
2011-02-22 06:22:40 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.DebugException(ex.Message, ex);
|
2011-02-24 01:41:18 +00:00
|
|
|
|
Logger.Warn("Unable to Rename Episode: {0}", Path.GetFileName(erm.EpisodeFile.Path));
|
2011-02-22 06:22:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|