mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 18:57:39 -07:00
Rename series added
This commit is contained in:
parent
c6d82bf98b
commit
637b101975
13 changed files with 233 additions and 19 deletions
16
NzbDrone.Core/MediaFiles/Commands/RenameSeasonCommand.cs
Normal file
16
NzbDrone.Core/MediaFiles/Commands/RenameSeasonCommand.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using NzbDrone.Common.Messaging;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles.Commands
|
||||
{
|
||||
public class RenameSeasonCommand : ICommand
|
||||
{
|
||||
public int SeriesId { get; private set; }
|
||||
public int SeasonNumber { get; private set; }
|
||||
|
||||
public RenameSeasonCommand(int seriesId, int seasonNumber)
|
||||
{
|
||||
SeriesId = seriesId;
|
||||
SeasonNumber = seasonNumber;
|
||||
}
|
||||
}
|
||||
}
|
14
NzbDrone.Core/MediaFiles/Commands/RenameSeriesCommand.cs
Normal file
14
NzbDrone.Core/MediaFiles/Commands/RenameSeriesCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using NzbDrone.Common.Messaging;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles.Commands
|
||||
{
|
||||
public class RenameSeriesCommand : ICommand
|
||||
{
|
||||
public int SeriesId { get; private set; }
|
||||
|
||||
public RenameSeriesCommand(int seriesId)
|
||||
{
|
||||
SeriesId = seriesId;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,6 +14,7 @@ namespace NzbDrone.Core.MediaFiles
|
|||
public interface IMoveEpisodeFiles
|
||||
{
|
||||
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile);
|
||||
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series);
|
||||
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
|
||||
}
|
||||
|
||||
|
@ -61,6 +62,15 @@ namespace NzbDrone.Core.MediaFiles
|
|||
return episodeFile;
|
||||
}
|
||||
|
||||
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series)
|
||||
{
|
||||
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
|
||||
var newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
|
||||
var destinationFilename = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
|
||||
|
||||
return MoveFile(episodeFile, destinationFilename);
|
||||
}
|
||||
|
||||
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
|
||||
{
|
||||
var newFileName = _buildFileNames.BuildFilename(localEpisode.Episodes, localEpisode.Series, episodeFile);
|
||||
|
|
15
NzbDrone.Core/MediaFiles/Events/SeriesRenamedEvent.cs
Normal file
15
NzbDrone.Core/MediaFiles/Events/SeriesRenamedEvent.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles.Events
|
||||
{
|
||||
public class SeriesRenamedEvent : IEvent
|
||||
{
|
||||
public Series Series { get; private set; }
|
||||
|
||||
public SeriesRenamedEvent(Series series)
|
||||
{
|
||||
Series = series;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ namespace NzbDrone.Core.MediaFiles
|
|||
{
|
||||
EpisodeFile GetFileByPath(string path);
|
||||
List<EpisodeFile> GetFilesBySeries(int seriesId);
|
||||
List<EpisodeFile> GetFilesBySeason(int seriesId, int seasonNumber);
|
||||
bool Exists(string path);
|
||||
}
|
||||
|
||||
|
@ -20,21 +21,26 @@ namespace NzbDrone.Core.MediaFiles
|
|||
{
|
||||
}
|
||||
|
||||
|
||||
public EpisodeFile GetFileByPath(string path)
|
||||
{
|
||||
return Query.SingleOrDefault(c => c.Path == path);
|
||||
}
|
||||
|
||||
public bool Exists(string path)
|
||||
{
|
||||
return Query.Any(c => c.Path == path);
|
||||
}
|
||||
|
||||
public List<EpisodeFile> GetFilesBySeries(int seriesId)
|
||||
{
|
||||
return Query.Where(c => c.SeriesId == seriesId).ToList();
|
||||
}
|
||||
|
||||
public List<EpisodeFile> GetFilesBySeason(int seriesId, int seasonNumber)
|
||||
{
|
||||
return Query.Where(c => c.SeriesId == seriesId)
|
||||
.AndWhere(c => c.SeasonNumber == seasonNumber)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public bool Exists(string path)
|
||||
{
|
||||
return Query.Any(c => c.Path == path);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,21 +17,19 @@ namespace NzbDrone.Core.MediaFiles
|
|||
bool Exists(string path);
|
||||
EpisodeFile GetFileByPath(string path);
|
||||
List<EpisodeFile> GetFilesBySeries(int seriesId);
|
||||
|
||||
List<EpisodeFile> GetFilesBySeason(int seriesId, int seasonNumber);
|
||||
List<string> FilterExistingFiles(List<string> files, int seriesId);
|
||||
}
|
||||
|
||||
public class MediaFileService : IMediaFileService, IHandleAsync<SeriesDeletedEvent>
|
||||
{
|
||||
private readonly IConfigService _configService;
|
||||
private readonly IMessageAggregator _messageAggregator;
|
||||
private readonly Logger _logger;
|
||||
private readonly IMediaFileRepository _mediaFileRepository;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public MediaFileService(IMediaFileRepository mediaFileRepository, IConfigService configService, IMessageAggregator messageAggregator, Logger logger)
|
||||
public MediaFileService(IMediaFileRepository mediaFileRepository, IMessageAggregator messageAggregator, Logger logger)
|
||||
{
|
||||
_mediaFileRepository = mediaFileRepository;
|
||||
_configService = configService;
|
||||
_messageAggregator = messageAggregator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
|
85
NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs
Normal file
85
NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs
Normal file
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.MediaFiles.Commands;
|
||||
using NzbDrone.Core.MediaFiles.Events;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles
|
||||
{
|
||||
public interface IRenameEpisodeFiles
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class RenameEpisodeFileService : IRenameEpisodeFiles, IExecute<RenameSeasonCommand>, IExecute<RenameSeriesCommand>
|
||||
{
|
||||
private readonly ISeriesService _seriesService;
|
||||
private readonly IMediaFileService _mediaFileService;
|
||||
private readonly IMoveEpisodeFiles _episodeFileMover;
|
||||
private readonly IMessageAggregator _messageAggregator;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RenameEpisodeFileService(ISeriesService seriesService,
|
||||
IMediaFileService mediaFileService,
|
||||
IMoveEpisodeFiles episodeFileMover,
|
||||
IMessageAggregator messageAggregator,
|
||||
Logger logger)
|
||||
{
|
||||
_seriesService = seriesService;
|
||||
_mediaFileService = mediaFileService;
|
||||
_episodeFileMover = episodeFileMover;
|
||||
_messageAggregator = messageAggregator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private void RenameFiles(List<EpisodeFile> episodeFiles, Series series)
|
||||
{
|
||||
var renamed = new List<EpisodeFile>();
|
||||
|
||||
foreach (var file in episodeFiles)
|
||||
{
|
||||
var episodeFile = file;
|
||||
|
||||
_logger.Trace("Renaming episode file: {0}", episodeFile);
|
||||
episodeFile = _episodeFileMover.MoveEpisodeFile(episodeFile, series);
|
||||
|
||||
if (episodeFile != null)
|
||||
{
|
||||
_mediaFileService.Update(episodeFile);
|
||||
renamed.Add(episodeFile);
|
||||
}
|
||||
|
||||
_logger.Trace("Renamed episode file: {0}", episodeFile);
|
||||
}
|
||||
|
||||
if (renamed.Any())
|
||||
{
|
||||
_messageAggregator.PublishEvent(new SeriesRenamedEvent(series));
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(RenameSeasonCommand message)
|
||||
{
|
||||
var series = _seriesService.GetSeries(message.SeriesId);
|
||||
var episodeFiles = _mediaFileService.GetFilesBySeason(message.SeriesId, message.SeasonNumber);
|
||||
|
||||
_logger.Info("Renaming {0} files for {1} season {2}", episodeFiles.Count, series.Title, message.SeasonNumber);
|
||||
RenameFiles(episodeFiles, series);
|
||||
_logger.Debug("Episode Fies renamed for {0} season {1}", series.Title, message.SeasonNumber);
|
||||
}
|
||||
|
||||
public void Execute(RenameSeriesCommand message)
|
||||
{
|
||||
var series = _seriesService.GetSeries(message.SeriesId);
|
||||
var episodeFiles = _mediaFileService.GetFilesBySeries(message.SeriesId);
|
||||
|
||||
_logger.Info("Renaming {0} files for {1}", episodeFiles.Count, series.Title);
|
||||
RenameFiles(episodeFiles, series);
|
||||
_logger.Debug("Episode Fies renamed for {0}", series.Title);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue