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

163 lines
6.6 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;
2014-03-13 23:12:42 +03:00
using NzbDrone.Core.Instrumentation.Extensions;
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>,
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;
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)
{
2014-06-25 10:09:16 +03:00
foreach (var f in files)
{
2014-06-25 10:09:16 +03:00
var file = f;
var episodesInFile = episodes.Where(e => e.EpisodeFileId == file.Id).ToList();
if (!episodesInFile.Any())
{
_logger.Warn("File ({0}) is not linked to any episodes", file.Path);
continue;
}
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,
2014-06-25 10:09:16 +03:00
ExistingPath = series.Path.GetRelativePath(file.Path),
NewPath = series.Path.GetRelativePath(newPath)
};
}
}
}
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
{
2014-03-13 23:12:42 +03:00
_logger.Debug("Renaming episode file: {0}", episodeFile);
_episodeFileMover.MoveEpisodeFile(episodeFile, series);
2013-07-19 08:23:04 +03:00
_mediaFileService.Update(episodeFile);
renamed.Add(episodeFile);
2014-03-13 23:12:42 +03:00
_logger.Debug("Renamed episode file: {0}", episodeFile);
2013-08-13 05:01:15 +03:00
}
catch (SameFilenameException ex)
{
2014-03-13 23:12:42 +03:00
_logger.Debug("File not renamed, source and destination are the same: {0}", ex.Filename);
2013-08-13 05:01:15 +03:00
}
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);
}
public void Execute(RenameSeriesCommand message)
{
2014-03-13 23:12:42 +03:00
_logger.Debug("Renaming all files for selected series");
var seriesToRename = _seriesService.GetSeries(message.SeriesIds);
foreach (var series in seriesToRename)
{
var episodeFiles = _mediaFileService.GetFilesBySeries(series.Id);
_logger.ProgressInfo("Renaming all files in series: {0}", series.Title);
RenameFiles(episodeFiles, series);
_logger.ProgressInfo("All episode files renamed for {0}", series.Title);
}
2013-07-19 08:23:04 +03:00
}
}
}