2011-06-17 04:48:24 +02:00
|
|
|
using System;
|
2010-10-21 03:49:23 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2011-06-14 03:23:04 +02:00
|
|
|
using Ninject;
|
2010-10-21 03:49:23 +02:00
|
|
|
using NLog;
|
2011-06-07 08:29:07 +02:00
|
|
|
using NzbDrone.Core.Helpers;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-04-04 05:50:12 +02:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2010-10-21 03:49:23 +02:00
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-07 08:29:07 +02:00
|
|
|
using NzbDrone.Core.Repository.Quality;
|
2011-06-17 05:29:39 +02:00
|
|
|
using PetaPoco;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2011-04-10 03:34:36 +02:00
|
|
|
public class MediaFileProvider
|
2010-10-21 03:49:23 +02:00
|
|
|
{
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-10 04:44:01 +02:00
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
2011-06-07 08:29:07 +02:00
|
|
|
private readonly ConfigProvider _configProvider;
|
2011-06-17 05:29:39 +02:00
|
|
|
private readonly IDatabase _database;
|
2010-10-21 03:49:23 +02:00
|
|
|
|
2011-06-14 03:23:04 +02:00
|
|
|
[Inject]
|
2011-06-20 05:04:08 +02:00
|
|
|
public MediaFileProvider(EpisodeProvider episodeProvider, ConfigProvider configProvider, IDatabase database)
|
2010-10-21 03:49:23 +02:00
|
|
|
{
|
|
|
|
_episodeProvider = episodeProvider;
|
2011-06-07 08:29:07 +02:00
|
|
|
_configProvider = configProvider;
|
2011-06-17 05:29:39 +02:00
|
|
|
_database = database;
|
2010-10-21 03:49:23 +02:00
|
|
|
}
|
|
|
|
|
2011-05-18 07:32:23 +02:00
|
|
|
public MediaFileProvider() { }
|
2011-04-10 04:28:54 +02:00
|
|
|
|
2011-03-03 09:50:33 +01:00
|
|
|
|
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
|
|
|
|
2011-04-25 23:23:02 +02:00
|
|
|
|
2011-05-30 05:56:49 +02:00
|
|
|
|
2011-04-25 23:23:02 +02:00
|
|
|
|
2010-10-24 09:46:58 +02:00
|
|
|
|
2011-05-18 07:32:23 +02:00
|
|
|
public virtual void Update(EpisodeFile episodeFile)
|
2011-02-18 07:49:23 +01:00
|
|
|
{
|
2011-06-17 05:29:39 +02:00
|
|
|
_database.Update(episodeFile);
|
2011-06-20 03:59:31 +02:00
|
|
|
}
|
2011-02-18 07:49:23 +01:00
|
|
|
|
2011-05-18 07:32:23 +02:00
|
|
|
public virtual EpisodeFile GetEpisodeFile(int episodeFileId)
|
2011-02-18 07:49:23 +01:00
|
|
|
{
|
2011-06-19 01:03:58 +02:00
|
|
|
return _database.Single<EpisodeFile>(episodeFileId);
|
2011-02-18 07:49:23 +01:00
|
|
|
}
|
|
|
|
|
2011-05-18 07:32:23 +02:00
|
|
|
public virtual List<EpisodeFile> GetEpisodeFiles()
|
2011-02-22 07:22:40 +01:00
|
|
|
{
|
2011-06-17 05:29:39 +02:00
|
|
|
return _database.Fetch<EpisodeFile>();
|
2011-02-22 07:22:40 +01:00
|
|
|
}
|
|
|
|
|
2011-06-19 22:43:33 +02:00
|
|
|
public virtual IList<EpisodeFile> GetSeriesFiles(int seriesId)
|
2011-02-24 01:40:11 +01:00
|
|
|
{
|
2011-06-17 05:29:39 +02:00
|
|
|
return _database.Fetch<EpisodeFile>("WHERE seriesId= @0", seriesId);
|
2011-02-24 01:40:11 +01:00
|
|
|
}
|
|
|
|
|
2011-06-03 05:04:39 +02:00
|
|
|
public virtual Tuple<int, int> GetEpisodeFilesCount(int seriesId)
|
|
|
|
{
|
2011-06-15 01:39:09 +02:00
|
|
|
var allEpisodes = _episodeProvider.GetEpisodeBySeries(seriesId).ToList();
|
2011-06-03 05:04:39 +02:00
|
|
|
|
2011-06-15 01:39:09 +02:00
|
|
|
var episodeTotal = allEpisodes.Where(e => !e.Ignored && e.AirDate <= DateTime.Today && e.AirDate.Year > 1900).ToList();
|
|
|
|
var avilableEpisodes = episodeTotal.Where(e => e.EpisodeFileId > 0).ToList();
|
2011-06-03 05:04:39 +02:00
|
|
|
|
2011-06-15 01:39:09 +02:00
|
|
|
return new Tuple<int, int>(avilableEpisodes.Count, episodeTotal.Count);
|
2011-06-03 05:04:39 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 05:04:08 +02:00
|
|
|
public virtual FileInfo CalculateFilePath(Series series, int seasonNumber, string fileName, string extention)
|
2011-06-07 08:29:07 +02:00
|
|
|
{
|
2011-06-20 05:04:08 +02:00
|
|
|
var path = series.Path;
|
|
|
|
if (series.SeasonFolder)
|
2011-06-07 08:29:07 +02:00
|
|
|
{
|
2011-06-20 05:04:08 +02:00
|
|
|
path = Path.Combine(path, "Season " + seasonNumber);
|
2011-06-07 08:29:07 +02:00
|
|
|
}
|
2011-06-07 23:19:11 +02:00
|
|
|
|
2011-06-20 05:04:08 +02:00
|
|
|
path = Path.Combine(path, fileName + extention);
|
2011-06-07 23:19:11 +02:00
|
|
|
|
2011-06-20 05:04:08 +02:00
|
|
|
return new FileInfo(path);
|
2011-06-07 08:29:07 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 05:04:08 +02:00
|
|
|
|
2011-06-20 04:33:43 +02:00
|
|
|
public virtual string GetNewFilename(IList<Episode> episodes, string seriesTitle, QualityTypes quality)
|
2011-06-07 08:29:07 +02:00
|
|
|
{
|
|
|
|
var separatorStyle = EpisodeSortingHelper.GetSeparatorStyle(_configProvider.SeparatorStyle);
|
|
|
|
var numberStyle = EpisodeSortingHelper.GetNumberStyle(_configProvider.NumberStyle);
|
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
var episodeNames = episodes[0].Title;
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
var result = String.Empty;
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
if (_configProvider.SeriesName)
|
2011-06-07 08:29:07 +02:00
|
|
|
{
|
2011-06-20 04:33:43 +02:00
|
|
|
result += seriesTitle + separatorStyle.Pattern;
|
2011-06-07 08:29:07 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
result += numberStyle.Pattern.Replace("%0e", String.Format("{0:00}", episodes[0].EpisodeNumber));
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
if (episodes.Count > 1)
|
2011-06-07 08:29:07 +02:00
|
|
|
{
|
2011-06-20 03:59:31 +02:00
|
|
|
var multiEpisodeStyle = EpisodeSortingHelper.GetMultiEpisodeStyle(_configProvider.MultiEpisodeStyle);
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
foreach (var episode in episodes.OrderBy(e => e.EpisodeNumber).Skip(1))
|
2011-06-07 08:29:07 +02:00
|
|
|
{
|
2011-06-20 03:59:31 +02:00
|
|
|
if (multiEpisodeStyle.Name == "Duplicate")
|
|
|
|
{
|
|
|
|
result += separatorStyle.Pattern + numberStyle.Pattern;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result += multiEpisodeStyle.Pattern;
|
|
|
|
}
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
result = result.Replace("%0e", String.Format("{0:00}", episode.EpisodeNumber));
|
|
|
|
episodeNames += String.Format(" + {0}", episode.Title);
|
|
|
|
}
|
2011-06-07 08:29:07 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
result = result
|
|
|
|
.Replace("%s", String.Format("{0}", episodes.First().SeasonNumber))
|
|
|
|
.Replace("%0s", String.Format("{0:00}", episodes.First().SeasonNumber))
|
|
|
|
.Replace("%x", numberStyle.EpisodeSeparator)
|
|
|
|
.Replace("%p", separatorStyle.Pattern);
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
if (_configProvider.EpisodeName)
|
2011-06-07 08:29:07 +02:00
|
|
|
{
|
|
|
|
episodeNames = episodeNames.TrimEnd(' ', '+');
|
2011-06-20 03:59:31 +02:00
|
|
|
result += separatorStyle.Pattern + episodeNames;
|
2011-06-07 08:29:07 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
if (_configProvider.AppendQuality)
|
|
|
|
result += String.Format(" [{0}]", quality);
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
if (_configProvider.ReplaceSpaces)
|
|
|
|
result = result.Replace(' ', '.');
|
2011-06-07 08:29:07 +02:00
|
|
|
|
2011-06-20 03:59:31 +02:00
|
|
|
Logger.Debug("New File Name is: {0}", result.Trim());
|
|
|
|
return result.Trim();
|
2011-06-07 08:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-21 03:49:23 +02:00
|
|
|
}
|
2011-04-10 04:44:01 +02:00
|
|
|
}
|