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