2013-03-01 10:03:41 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using NLog;
|
2013-04-24 04:56:00 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-03-01 10:03:41 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-03-07 07:34:56 +03:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2013-03-01 10:03:41 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2013-03-05 22:49:34 +03:00
|
|
|
using NzbDrone.Core.Tv.Events;
|
2013-03-01 10:03:41 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
{
|
|
|
|
public interface IMediaFileService
|
|
|
|
{
|
|
|
|
EpisodeFile Add(EpisodeFile episodeFile);
|
|
|
|
void Update(EpisodeFile episodeFile);
|
2013-03-07 07:34:56 +03:00
|
|
|
void Delete(EpisodeFile episodeFile);
|
2013-03-01 10:03:41 +03:00
|
|
|
bool Exists(string path);
|
|
|
|
EpisodeFile GetFileByPath(string path);
|
2013-03-07 00:20:33 +03:00
|
|
|
List<EpisodeFile> GetFilesBySeries(int seriesId);
|
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
|
|
|
{
|
|
|
|
private readonly IConfigService _configService;
|
2013-04-24 04:56:00 +03:00
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-03-01 10:03:41 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly IMediaFileRepository _mediaFileRepository;
|
|
|
|
|
2013-06-02 22:29:00 +03:00
|
|
|
public MediaFileService(IMediaFileRepository mediaFileRepository, IConfigService configService, IMessageAggregator messageAggregator, Logger logger)
|
2013-03-01 10:03:41 +03:00
|
|
|
{
|
|
|
|
_mediaFileRepository = mediaFileRepository;
|
|
|
|
_configService = configService;
|
2013-04-24 04:56:00 +03:00
|
|
|
_messageAggregator = messageAggregator;
|
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-04-27 05:03:34 +03:00
|
|
|
_messageAggregator.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-03-07 07:34:56 +03:00
|
|
|
public void Delete(EpisodeFile episodeFile)
|
2013-03-01 10:03:41 +03:00
|
|
|
{
|
2013-03-07 07:34:56 +03:00
|
|
|
_mediaFileRepository.Delete(episodeFile);
|
2013-04-27 05:03:34 +03:00
|
|
|
_messageAggregator.PublishEvent(new EpisodeFileDeletedEvent(episodeFile));
|
2013-03-01 10:03:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Exists(string path)
|
|
|
|
{
|
2013-06-02 22:29:00 +03:00
|
|
|
return _mediaFileRepository.Exists(path);
|
2013-03-01 10:03:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public EpisodeFile GetFileByPath(string path)
|
|
|
|
{
|
|
|
|
return _mediaFileRepository.GetFileByPath(path.Normalize());
|
|
|
|
}
|
|
|
|
|
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-03-07 00:20:33 +03:00
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
{
|
|
|
|
var files = GetFilesBySeries(message.Series.Id);
|
|
|
|
_mediaFileRepository.DeleteMany(files);
|
|
|
|
}
|
|
|
|
|
2013-04-30 03:04:14 +03:00
|
|
|
public FileInfo CalculateFilePath(Series series, int seasonNumber, string fileName, string extension)
|
2013-03-01 10:03:41 +03:00
|
|
|
{
|
|
|
|
string path = series.Path;
|
|
|
|
if (series.SeasonFolder)
|
|
|
|
{
|
|
|
|
var seasonFolder = _configService.SortingSeasonFolderFormat
|
2013-03-07 00:20:33 +03:00
|
|
|
.Replace("%0s", seasonNumber.ToString("00"))
|
|
|
|
.Replace("%s", seasonNumber.ToString());
|
2013-03-01 10:03:41 +03:00
|
|
|
|
|
|
|
path = Path.Combine(path, seasonFolder);
|
|
|
|
}
|
|
|
|
|
2013-04-30 03:04:14 +03:00
|
|
|
path = Path.Combine(path, fileName + extension);
|
2013-03-01 10:03:41 +03:00
|
|
|
|
|
|
|
return new FileInfo(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|