Lidarr/NzbDrone.Core/MediaFiles/MediaFileService.cs

84 lines
2.8 KiB
C#
Raw Normal View History

2013-03-01 07:03:41 +00:00
using System.Collections.Generic;
using System.Linq;
2013-03-01 07:03:41 +00:00
using NLog;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Tv.Events;
using NzbDrone.Common;
2013-03-01 07:03:41 +00:00
namespace NzbDrone.Core.MediaFiles
{
public interface IMediaFileService
{
EpisodeFile Add(EpisodeFile episodeFile);
void Update(EpisodeFile episodeFile);
void Delete(EpisodeFile episodeFile, bool forUpgrade = false);
List<EpisodeFile> GetFilesBySeries(int seriesId);
2013-07-19 05:23:04 +00:00
List<EpisodeFile> GetFilesBySeason(int seriesId, int seasonNumber);
List<string> FilterExistingFiles(List<string> files, int seriesId);
2013-08-21 06:46:23 +00:00
EpisodeFile Get(int id);
2013-03-01 07:03:41 +00:00
}
public class MediaFileService : IMediaFileService, IHandleAsync<SeriesDeletedEvent>
2013-03-01 07:03:41 +00:00
{
private readonly IEventAggregator _eventAggregator;
2013-03-01 07:03:41 +00:00
private readonly IMediaFileRepository _mediaFileRepository;
2013-07-19 05:23:04 +00:00
private readonly Logger _logger;
2013-03-01 07:03:41 +00:00
public MediaFileService(IMediaFileRepository mediaFileRepository, IEventAggregator eventAggregator, Logger logger)
2013-03-01 07:03:41 +00:00
{
_mediaFileRepository = mediaFileRepository;
_eventAggregator = eventAggregator;
2013-03-01 07:03:41 +00:00
_logger = logger;
}
public EpisodeFile Add(EpisodeFile episodeFile)
{
var addedFile = _mediaFileRepository.Insert(episodeFile);
_eventAggregator.PublishEvent(new EpisodeFileAddedEvent(addedFile));
return addedFile;
2013-03-01 07:03:41 +00:00
}
public void Update(EpisodeFile episodeFile)
{
_mediaFileRepository.Update(episodeFile);
}
public void Delete(EpisodeFile episodeFile, bool forUpgrade = false)
2013-03-01 07:03:41 +00:00
{
_mediaFileRepository.Delete(episodeFile);
_eventAggregator.PublishEvent(new EpisodeFileDeletedEvent(episodeFile, forUpgrade));
2013-03-01 07:03:41 +00:00
}
public List<EpisodeFile> GetFilesBySeries(int seriesId)
2013-03-01 07:03:41 +00:00
{
return _mediaFileRepository.GetFilesBySeries(seriesId);
}
2013-07-19 06:22:49 +00:00
public List<EpisodeFile> GetFilesBySeason(int seriesId, int seasonNumber)
{
return _mediaFileRepository.GetFilesBySeason(seriesId, seasonNumber);
}
public List<string> FilterExistingFiles(List<string> files, int seriesId)
{
var seriesFiles = GetFilesBySeries(seriesId).Select(f => f.Path.CleanFilePath()).ToList();
if (!seriesFiles.Any()) return files;
return files.Select(f => f.CleanFilePath()).Except(seriesFiles, new PathEqualityComparer()).ToList();
}
2013-08-21 06:46:23 +00:00
public EpisodeFile Get(int id)
{
return _mediaFileRepository.Get(id);
}
public void HandleAsync(SeriesDeletedEvent message)
{
var files = GetFilesBySeries(message.Series.Id);
_mediaFileRepository.DeleteMany(files);
}
2013-03-01 07:03:41 +00:00
}
}