1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/MediaFiles/MediaFileService.cs

101 lines
3.4 KiB
C#
Raw Normal View History

2013-03-01 08:03:41 +01:00
using System.Collections.Generic;
using System.IO;
using NLog;
using NzbDrone.Common.Eventing;
2013-03-01 08:03:41 +01:00
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles.Events;
2013-03-01 08:03:41 +01:00
using NzbDrone.Core.Tv;
using NzbDrone.Core.Tv.Events;
2013-03-01 08:03:41 +01:00
namespace NzbDrone.Core.MediaFiles
{
public interface IMediaFileService
{
EpisodeFile Add(EpisodeFile episodeFile);
void Update(EpisodeFile episodeFile);
void Delete(EpisodeFile episodeFile);
2013-03-01 08:03:41 +01:00
bool Exists(string path);
EpisodeFile GetFileByPath(string path);
List<EpisodeFile> GetFilesBySeries(int seriesId);
List<EpisodeFile> GetFilesBySeason(int seriesId, int seasonNumber);
2013-03-01 08:03:41 +01:00
}
public class MediaFileService : IMediaFileService, IHandleAsync<SeriesDeletedEvent>
2013-03-01 08:03:41 +01:00
{
private readonly IConfigService _configService;
private readonly IEpisodeService _episodeService;
private readonly IEventAggregator _eventAggregator;
2013-03-01 08:03:41 +01:00
private readonly Logger _logger;
private readonly IMediaFileRepository _mediaFileRepository;
public MediaFileService(IMediaFileRepository mediaFileRepository, IConfigService configService, IEpisodeService episodeService, IEventAggregator eventAggregator, Logger logger)
2013-03-01 08:03:41 +01:00
{
_mediaFileRepository = mediaFileRepository;
_configService = configService;
_episodeService = episodeService;
_eventAggregator = eventAggregator;
2013-03-01 08:03:41 +01:00
_logger = logger;
}
public EpisodeFile Add(EpisodeFile episodeFile)
{
var addedFile = _mediaFileRepository.Insert(episodeFile);
_eventAggregator.Publish(new EpisodeFileAddedEvent(addedFile));
return addedFile;
2013-03-01 08:03:41 +01:00
}
public void Update(EpisodeFile episodeFile)
{
_mediaFileRepository.Update(episodeFile);
}
public void Delete(EpisodeFile episodeFile)
2013-03-01 08:03:41 +01:00
{
_mediaFileRepository.Delete(episodeFile);
_eventAggregator.Publish(new EpisodeFileDeletedEvent(episodeFile));
2013-03-01 08:03:41 +01:00
}
public bool Exists(string path)
{
return GetFileByPath(path) != null;
}
public EpisodeFile GetFileByPath(string path)
{
return _mediaFileRepository.GetFileByPath(path.Normalize());
}
public List<EpisodeFile> GetFilesBySeries(int seriesId)
2013-03-01 08:03:41 +01:00
{
return _mediaFileRepository.GetFilesBySeries(seriesId);
}
public List<EpisodeFile> GetFilesBySeason(int seriesId, int seasonNumber)
2013-03-01 08:03:41 +01:00
{
return _mediaFileRepository.GetFilesBySeason(seriesId, seasonNumber);
}
public void HandleAsync(SeriesDeletedEvent message)
{
var files = GetFilesBySeries(message.Series.Id);
_mediaFileRepository.DeleteMany(files);
}
2013-03-01 08:03:41 +01:00
public FileInfo CalculateFilePath(Series series, int seasonNumber, string fileName, string extention)
{
string path = series.Path;
if (series.SeasonFolder)
{
var seasonFolder = _configService.SortingSeasonFolderFormat
.Replace("%0s", seasonNumber.ToString("00"))
.Replace("%s", seasonNumber.ToString());
2013-03-01 08:03:41 +01:00
path = Path.Combine(path, seasonFolder);
}
path = Path.Combine(path, fileName + extention);
return new FileInfo(path);
}
}
}