sonarr-repo-only/NzbDrone.Core/MediaFiles/MediaFileService.cs

87 lines
2.8 KiB
C#
Raw Normal View History

2013-03-01 07:03:41 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-03-01 07:03:41 +00:00
using NLog;
using NzbDrone.Common.Messaging;
2013-03-01 07:03:41 +00:00
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Tv.Events;
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);
2013-03-01 07:03:41 +00:00
bool Exists(string path);
EpisodeFile GetFileByPath(string path);
List<EpisodeFile> GetFilesBySeries(int seriesId);
List<string> FilterExistingFiles(List<string> files, int seriesId);
2013-03-01 07:03:41 +00:00
}
public class MediaFileService : IMediaFileService, IHandleAsync<SeriesDeletedEvent>
2013-03-01 07:03:41 +00:00
{
private readonly IConfigService _configService;
private readonly IMessageAggregator _messageAggregator;
2013-03-01 07:03:41 +00:00
private readonly Logger _logger;
private readonly IMediaFileRepository _mediaFileRepository;
2013-06-02 19:29:00 +00:00
public MediaFileService(IMediaFileRepository mediaFileRepository, IConfigService configService, IMessageAggregator messageAggregator, Logger logger)
2013-03-01 07:03:41 +00:00
{
_mediaFileRepository = mediaFileRepository;
_configService = configService;
_messageAggregator = messageAggregator;
2013-03-01 07:03:41 +00:00
_logger = logger;
}
public EpisodeFile Add(EpisodeFile episodeFile)
{
var addedFile = _mediaFileRepository.Insert(episodeFile);
_messageAggregator.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)
2013-03-01 07:03:41 +00:00
{
_mediaFileRepository.Delete(episodeFile);
_messageAggregator.PublishEvent(new EpisodeFileDeletedEvent(episodeFile));
2013-03-01 07:03:41 +00:00
}
public bool Exists(string path)
{
2013-06-02 19:29:00 +00:00
return _mediaFileRepository.Exists(path);
2013-03-01 07:03:41 +00:00
}
public EpisodeFile GetFileByPath(string path)
{
return _mediaFileRepository.GetFileByPath(path.Normalize());
}
public List<EpisodeFile> GetFilesBySeries(int seriesId)
2013-03-01 07:03:41 +00:00
{
return _mediaFileRepository.GetFilesBySeries(seriesId);
}
public List<string> FilterExistingFiles(List<string> files, int seriesId)
{
var seriesFiles = GetFilesBySeries(seriesId);
if (!seriesFiles.Any()) return files;
return files.Select(f => f.Normalize()).Except(seriesFiles.Select(c => c.Path)).ToList();
}
public void HandleAsync(SeriesDeletedEvent message)
{
var files = GetFilesBySeries(message.Series.Id);
_mediaFileRepository.DeleteMany(files);
}
2013-03-01 07:03:41 +00:00
}
}