1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-21 01:49:57 +02:00
Sonarr/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs

88 lines
3.4 KiB
C#
Raw Normal View History

2013-08-13 05:01:15 +03:00
using System;
using System.Collections.Generic;
2013-07-19 08:23:04 +03:00
using System.Linq;
using NLog;
2013-09-11 09:33:47 +03:00
using NzbDrone.Core.Instrumentation;
2013-07-19 08:23:04 +03:00
using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
2013-07-19 08:23:04 +03:00
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.MediaFiles
{
2013-07-19 09:18:57 +03:00
public class RenameEpisodeFileService : IExecute<RenameSeasonCommand>, IExecute<RenameSeriesCommand>
2013-07-19 08:23:04 +03:00
{
private readonly ISeriesService _seriesService;
private readonly IMediaFileService _mediaFileService;
private readonly IMoveEpisodeFiles _episodeFileMover;
private readonly IEventAggregator _eventAggregator;
2013-07-19 08:23:04 +03:00
private readonly Logger _logger;
public RenameEpisodeFileService(ISeriesService seriesService,
IMediaFileService mediaFileService,
IMoveEpisodeFiles episodeFileMover,
IEventAggregator eventAggregator,
2013-07-19 08:23:04 +03:00
Logger logger)
{
_seriesService = seriesService;
_mediaFileService = mediaFileService;
_episodeFileMover = episodeFileMover;
_eventAggregator = eventAggregator;
2013-07-19 08:23:04 +03:00
_logger = logger;
}
private void RenameFiles(List<EpisodeFile> episodeFiles, Series series)
{
var renamed = new List<EpisodeFile>();
2013-08-30 09:41:49 +03:00
foreach (var episodeFile in episodeFiles)
2013-07-19 08:23:04 +03:00
{
2013-08-13 05:01:15 +03:00
try
{
_logger.Trace("Renaming episode file: {0}", episodeFile);
2013-08-30 09:41:49 +03:00
episodeFile.Path = _episodeFileMover.MoveEpisodeFile(episodeFile, series);
2013-07-19 08:23:04 +03:00
_mediaFileService.Update(episodeFile);
renamed.Add(episodeFile);
2013-08-13 05:01:15 +03:00
_logger.Trace("Renamed episode file: {0}", episodeFile);
}
catch (SameFilenameException ex)
{
_logger.Trace("File not renamed, source and destination are the same: {0}", ex.Filename);
}
catch (Exception ex)
{
2013-08-30 09:41:49 +03:00
_logger.ErrorException("Failed to rename file: " + episodeFile.Path, ex);
2013-08-13 05:01:15 +03:00
}
2013-07-19 08:23:04 +03:00
}
if (renamed.Any())
{
_eventAggregator.PublishEvent(new SeriesRenamedEvent(series));
2013-07-19 08:23:04 +03:00
}
}
public void Execute(RenameSeasonCommand message)
{
var series = _seriesService.GetSeries(message.SeriesId);
var episodeFiles = _mediaFileService.GetFilesBySeason(message.SeriesId, message.SeasonNumber);
2013-09-11 09:33:47 +03:00
_logger.ProgressInfo("Renaming {0} files for {1} season {2}", episodeFiles.Count, series.Title, message.SeasonNumber);
2013-07-19 08:23:04 +03:00
RenameFiles(episodeFiles, series);
2013-09-11 09:33:47 +03:00
_logger.ProgressInfo("Episode Fies renamed for {0} season {1}", series.Title, message.SeasonNumber);
2013-07-19 08:23:04 +03:00
}
public void Execute(RenameSeriesCommand message)
{
var series = _seriesService.GetSeries(message.SeriesId);
var episodeFiles = _mediaFileService.GetFilesBySeries(message.SeriesId);
2013-09-11 09:33:47 +03:00
_logger.ProgressInfo("Renaming {0} files for {1}", episodeFiles.Count, series.Title);
2013-07-19 08:23:04 +03:00
RenameFiles(episodeFiles, series);
2013-09-11 09:33:47 +03:00
_logger.ProgressInfo("Episode Fies renamed for {0}", series.Title);
2013-07-19 08:23:04 +03:00
}
}
}