1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 07:52:37 +01:00
Sonarr/NzbDrone.Core/Providers/MediaFileProvider.cs

205 lines
7.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Ninject;
using NLog;
using NzbDrone.Core.Helpers;
2011-04-04 05:50:12 +02:00
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using PetaPoco;
namespace NzbDrone.Core.Providers
{
public class MediaFileProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ConfigProvider _configProvider;
private readonly IDatabase _database;
2011-06-20 05:25:04 +02:00
private readonly EpisodeProvider _episodeProvider;
[Inject]
2011-06-20 05:04:08 +02:00
public MediaFileProvider(EpisodeProvider episodeProvider, ConfigProvider configProvider, IDatabase database)
{
_episodeProvider = episodeProvider;
_configProvider = configProvider;
_database = database;
}
2011-06-20 05:25:04 +02:00
public MediaFileProvider()
{
}
2011-06-22 03:12:20 +02:00
public virtual int Add(EpisodeFile episodeFile)
{
return Convert.ToInt32(_database.Insert(episodeFile));
}
2011-05-18 07:32:23 +02:00
public virtual void Update(EpisodeFile episodeFile)
{
_database.Update(episodeFile);
2011-06-20 03:59:31 +02:00
}
2011-06-22 03:12:20 +02:00
public virtual void Delete(int episodeFileId)
{
_database.Delete(episodeFileId);
}
public virtual bool Exists(string path)
{
return _database.Exists<EpisodeFile>("WHERE Path =@0", Parser.NormalizePath(path));
}
2011-05-18 07:32:23 +02:00
public virtual EpisodeFile GetEpisodeFile(int episodeFileId)
{
return _database.Single<EpisodeFile>(episodeFileId);
}
2011-05-18 07:32:23 +02:00
public virtual List<EpisodeFile> GetEpisodeFiles()
{
return _database.Fetch<EpisodeFile>();
}
2011-06-19 22:43:33 +02:00
public virtual IList<EpisodeFile> GetSeriesFiles(int seriesId)
{
return _database.Fetch<EpisodeFile>("WHERE seriesId= @0", seriesId);
}
public virtual Tuple<int, int> GetEpisodeFilesCount(int seriesId)
{
var allEpisodes = _episodeProvider.GetEpisodeBySeries(seriesId).ToList();
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();
return new Tuple<int, int>(avilableEpisodes.Count, episodeTotal.Count);
}
2011-06-20 05:04:08 +02:00
public virtual FileInfo CalculateFilePath(Series series, int seasonNumber, string fileName, string extention)
{
2011-06-20 05:25:04 +02:00
string path = series.Path;
2011-06-20 05:04:08 +02:00
if (series.SeasonFolder)
{
var seasonFolder = _configProvider.SeasonFolderFormat
.Replace("%0s", seasonNumber.ToString("00"))
.Replace("%s", seasonNumber.ToString());
path = Path.Combine(path, seasonFolder);
}
2011-06-20 05:04:08 +02:00
path = Path.Combine(path, fileName + extention);
2011-06-20 05:04:08 +02:00
return new FileInfo(path);
}
public virtual int RepairLinks()
{
Logger.Debug("Verifying Episode>Episode file relationships.");
var updated = _database.Execute(@"UPDATE Episodes SET EpisodeFileId = 0
WHERE EpisodeFileId IN
(SELECT Episodes.EpisodeFileId FROM Episodes
LEFT OUTER JOIN EpisodeFiles
ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.EpisodeFileId > 0 AND EpisodeFiles.EpisodeFileId IS null)");
if (updated > 0)
{
Logger.Warn("Removed {0} invalid links to episode files.", updated);
}
return updated;
}
public virtual int DeleteOrphaned()
{
Logger.Debug("Deleting orphaned files.");
var updated = _database.Execute(@"DELETE FROM EpisodeFiles
WHERE EpisodeFileId IN
(SELECT EpisodeFiles.EpisodeFileId FROM EpisodeFiles
LEFT OUTER JOIN Episodes
ON EpisodeFiles.EpisodeFileId = Episodes.EpisodeFileId
WHERE Episodes.EpisodeFileId IS null)");
if (updated > 0)
{
Logger.Warn("Removed {0} orphaned files.", updated);
}
return updated;
}
2011-06-20 04:33:43 +02:00
public virtual string GetNewFilename(IList<Episode> episodes, string seriesTitle, QualityTypes quality)
{
var separatorStyle = EpisodeSortingHelper.GetSeparatorStyle(_configProvider.SeparatorStyle);
var numberStyle = EpisodeSortingHelper.GetNumberStyle(_configProvider.NumberStyle);
2011-06-20 05:25:04 +02:00
string episodeNames = episodes[0].Title;
2011-06-20 05:25:04 +02:00
string result = String.Empty;
2011-06-20 03:59:31 +02:00
if (_configProvider.SeriesName)
{
2011-06-20 04:33:43 +02:00
result += seriesTitle + separatorStyle.Pattern;
}
2011-06-20 03:59:31 +02:00
result += numberStyle.Pattern.Replace("%0e", String.Format("{0:00}", episodes[0].EpisodeNumber));
2011-06-20 03:59:31 +02:00
if (episodes.Count > 1)
{
2011-06-20 03:59:31 +02:00
var multiEpisodeStyle = EpisodeSortingHelper.GetMultiEpisodeStyle(_configProvider.MultiEpisodeStyle);
2011-06-20 03:59:31 +02:00
foreach (var episode in episodes.OrderBy(e => e.EpisodeNumber).Skip(1))
{
2011-06-20 03:59:31 +02:00
if (multiEpisodeStyle.Name == "Duplicate")
{
result += separatorStyle.Pattern + numberStyle.Pattern;
}
else
{
result += multiEpisodeStyle.Pattern;
}
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-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-20 03:59:31 +02:00
if (_configProvider.EpisodeName)
{
episodeNames = episodeNames.TrimEnd(' ', '+');
2011-06-20 03:59:31 +02:00
result += separatorStyle.Pattern + episodeNames;
}
2011-06-20 03:59:31 +02:00
if (_configProvider.AppendQuality)
result += String.Format(" [{0}]", quality);
2011-06-20 03:59:31 +02:00
if (_configProvider.ReplaceSpaces)
result = result.Replace(' ', '.');
2011-06-20 03:59:31 +02:00
Logger.Debug("New File Name is: {0}", result.Trim());
return CleanFilename(result.Trim());
}
public static string CleanFilename(string name)
{
string result = name;
string[] badCharacters = { "\\", "/", "<", ">", "?", "*", ":", "|", "\"" };
string[] goodCharacters = { "+", "+", "{", "}", "!", "@", "-", "#", "`" };
for (int i = 0; i < badCharacters.Length; i++)
result = result.Replace(badCharacters[i], goodCharacters[i]);
2011-06-20 03:59:31 +02:00
return result.Trim();
}
}
2011-04-10 04:44:01 +02:00
}