1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-18 23:48:35 +02:00
Sonarr/src/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs

145 lines
5.9 KiB
C#
Raw Normal View History

2013-08-13 05:01:15 +03:00
using System;
using System.Collections.Generic;
using System.IO;
2013-07-19 08:23:04 +03:00
using System.Linq;
using NLog;
using NzbDrone.Common;
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;
using NzbDrone.Core.Organizer;
2013-07-19 08:23:04 +03:00
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.MediaFiles
{
public interface IRenameEpisodeFileService
{
List<RenameEpisodeFilePreview> GetRenamePreviews(int seriesId);
List<RenameEpisodeFilePreview> GetRenamePreviews(int seriesId, int seasonNumber);
}
public class RenameEpisodeFileService : IRenameEpisodeFileService,
IExecute<RenameFilesCommand>
2013-07-19 08:23:04 +03:00
{
private readonly ISeriesService _seriesService;
private readonly IMediaFileService _mediaFileService;
private readonly IMoveEpisodeFiles _episodeFileMover;
private readonly IEventAggregator _eventAggregator;
private readonly IEpisodeService _episodeService;
private readonly IBuildFileNames _filenameBuilder;
2013-07-19 08:23:04 +03:00
private readonly Logger _logger;
public RenameEpisodeFileService(ISeriesService seriesService,
IMediaFileService mediaFileService,
IMoveEpisodeFiles episodeFileMover,
IEventAggregator eventAggregator,
IEpisodeService episodeService,
IBuildFileNames filenameBuilder,
2013-07-19 08:23:04 +03:00
Logger logger)
{
_seriesService = seriesService;
_mediaFileService = mediaFileService;
_episodeFileMover = episodeFileMover;
_eventAggregator = eventAggregator;
_episodeService = episodeService;
_filenameBuilder = filenameBuilder;
2013-07-19 08:23:04 +03:00
_logger = logger;
}
public List<RenameEpisodeFilePreview> GetRenamePreviews(int seriesId)
{
var series = _seriesService.GetSeries(seriesId);
var episodes = _episodeService.GetEpisodeBySeries(seriesId);
var files = _mediaFileService.GetFilesBySeries(seriesId);
return GetPreviews(series, episodes, files)
.OrderBy(e => e.SeasonNumber)
.ThenBy(e => e.EpisodeNumbers.First())
.ToList();
}
public List<RenameEpisodeFilePreview> GetRenamePreviews(int seriesId, int seasonNumber)
{
var series = _seriesService.GetSeries(seriesId);
var episodes = _episodeService.GetEpisodesBySeason(seriesId, seasonNumber);
var files = _mediaFileService.GetFilesBySeason(seriesId, seasonNumber);
return GetPreviews(series, episodes, files)
.OrderBy(e => e.EpisodeNumbers.First()).ToList();
}
private IEnumerable<RenameEpisodeFilePreview> GetPreviews(Series series, List<Episode> episodes, List<EpisodeFile> files)
{
foreach (var file in files)
{
var episodesInFile = episodes.Where(e => e.EpisodeFileId == file.Id).ToList();
var seasonNumber = episodesInFile.First().SeasonNumber;
var newName = _filenameBuilder.BuildFilename(episodesInFile, series, file);
var newPath = _filenameBuilder.BuildFilePath(series, seasonNumber, newName, Path.GetExtension(file.Path));
if (!file.Path.PathEquals(newPath))
{
yield return new RenameEpisodeFilePreview
{
SeriesId = series.Id,
SeasonNumber = seasonNumber,
EpisodeNumbers = episodesInFile.Select(e => e.EpisodeNumber).ToList(),
EpisodeFileId = file.Id,
ExistingPath = GetRelativePath(series.Path, file.Path),
NewPath = GetRelativePath(series.Path, newPath)
};
}
}
}
private string GetRelativePath(string seriesPath, string path)
{
return path.Substring(seriesPath.Length + 1);
}
2013-07-19 08:23:04 +03:00
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(RenameFilesCommand message)
2013-07-19 08:23:04 +03:00
{
var series = _seriesService.GetSeries(message.SeriesId);
var episodeFiles = _mediaFileService.Get(message.Files);
2013-07-19 08:23:04 +03:00
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);
_logger.ProgressInfo("Selected Episode Files renamed for {0}", series.Title);
2013-07-19 08:23:04 +03:00
}
}
}