Radarr/src/NzbDrone.Core/Configuration/ConfigService.cs

522 lines
15 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2018-04-21 04:59:31 +00:00
using System.Globalization;
using System.Linq;
2010-10-05 06:21:18 +00:00
using NLog;
using NzbDrone.Common.EnsureThat;
2019-12-22 22:08:53 +00:00
using NzbDrone.Common.Http.Proxy;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events;
2020-05-26 01:55:10 +00:00
using NzbDrone.Core.Languages;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Messaging.Events;
2020-04-03 00:57:36 +00:00
using NzbDrone.Core.MetadataSource.SkyHook.Resource;
using NzbDrone.Core.Qualities;
2019-12-22 22:08:53 +00:00
using NzbDrone.Core.Security;
2010-09-23 03:19:47 +00:00
2013-02-24 06:48:52 +00:00
namespace NzbDrone.Core.Configuration
2010-09-23 03:19:47 +00:00
{
public enum ConfigKey
{
DownloadedMoviesFolder
}
2013-02-24 06:48:52 +00:00
public class ConfigService : IConfigService
2010-09-23 03:19:47 +00:00
{
2013-02-24 06:48:52 +00:00
private readonly IConfigRepository _repository;
private readonly IEventAggregator _eventAggregator;
2013-02-24 06:48:52 +00:00
private readonly Logger _logger;
private static Dictionary<string, string> _cache;
public ConfigService(IConfigRepository repository, IEventAggregator eventAggregator, Logger logger)
2010-09-23 03:19:47 +00:00
{
2013-02-24 06:48:52 +00:00
_repository = repository;
_eventAggregator = eventAggregator;
2013-02-24 06:48:52 +00:00
_logger = logger;
_cache = new Dictionary<string, string>();
2010-09-23 03:19:47 +00:00
}
private Dictionary<string, object> AllWithDefaults()
{
var dict = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
var type = GetType();
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
var value = propertyInfo.GetValue(this, null);
dict.Add(propertyInfo.Name, value);
}
return dict;
}
public void SaveConfigDictionary(Dictionary<string, object> configValues)
{
var allWithDefaults = AllWithDefaults();
foreach (var configValue in configValues)
{
object currentValue;
allWithDefaults.TryGetValue(configValue.Key, out currentValue);
2019-12-22 22:08:53 +00:00
if (currentValue == null || configValue.Value == null)
{
continue;
}
var equal = configValue.Value.ToString().Equals(currentValue.ToString());
if (!equal)
{
SetValue(configValue.Key, configValue.Value.ToString());
}
}
_eventAggregator.PublishEvent(new ConfigSavedEvent());
}
public bool IsDefined(string key)
{
return _repository.Get(key.ToLower()) != null;
}
2018-11-23 07:03:32 +00:00
public bool AutoUnmonitorPreviouslyDownloadedMovies
{
2018-11-23 07:03:32 +00:00
get { return GetValueBoolean("AutoUnmonitorPreviouslyDownloadedMovies"); }
set { SetValue("AutoUnmonitorPreviouslyDownloadedMovies", value); }
}
public int Retention
{
get { return GetValueInt("Retention", 0); }
set { SetValue("Retention", value); }
}
public string RecycleBin
{
get { return GetValue("RecycleBin", string.Empty); }
set { SetValue("RecycleBin", value); }
}
public int RecycleBinCleanupDays
{
get { return GetValueInt("RecycleBinCleanupDays", 7); }
set { SetValue("RecycleBinCleanupDays", value); }
}
public int RssSyncInterval
{
get { return GetValueInt("RssSyncInterval", 60); }
set { SetValue("RssSyncInterval", value); }
}
public int AvailabilityDelay
{
get { return GetValueInt("AvailabilityDelay", 0); }
set { SetValue("AvailabilityDelay", value); }
}
Min availability (#816) * availability specification to prevent downloading titles before their release * pull inCinamas status out of js handlebars and set it in SkyHook * minor code improvement * add incinemas to footer * typo * another typo * release date handling * still print cinema date out for announced titles * revert a minor change from before since its unnecessary * early implementation of minimumAvailability --> when does radarr consider a movie "available" should be specified by user default to "Physical release?" this isn't functional yet, but it has a skeleton + comments. I dont know how to have the minimumavailability attribute default to something or to have it actually populate the Movieinfo object could use some help with that * adding another comment for another location that might need to be updated to handle minimumAvailability * the implementation is now function; however, i still need to specify default values for minimumAvailability * missed these changes in the previous commit * fix rounded corners on new field in editmovie dialog * add minimum availability specification to the addMovie page * minor adjustment from last commit * handle the case where minimumavailability has never yet been set nullstring.. if its never been set, default to Released (Physical/Web) represented by integer value 3 * minAvailability specification on NetImport lists * add support for min availability to the movie editor * use enum MovieStatusType values directly makes for cleaner code * need to fix up the migration forgot in last commit * cleaning up code, proper case * erroneous code added in this feature needed to be removed * update "Wanted" page to take into account minimumAvailability * implement preDB minimumAvailability as default.. behaves same as Physical/Web a few comments with TODO for when preDB is implemented * minor adjustment * remove some unused code (leave commented for now) * improve code for minimumavailability and add option for availabilitydelay (but doesnt do anything yet) * improve isAvailable method * clean up and fix helper info on indexer configuration page * add buttons in Wanted/Missing view
2017-02-23 05:03:48 +00:00
public int ImportListSyncInterval
{
get { return GetValueInt("ImportListSyncInterval", 24); }
set { SetValue("ImportListSyncInterval", value); }
}
public string ListSyncLevel
{
get { return GetValue("ListSyncLevel", "disabled"); }
set { SetValue("ListSyncLevel", value); }
}
public string ImportExclusions
{
get { return GetValue("ImportExclusions", string.Empty); }
set { SetValue("ImportExclusions", value); }
}
2020-04-03 00:57:36 +00:00
public TMDbCountryCode CertificationCountry
{
get { return GetValueEnum("CertificationCountry", TMDbCountryCode.US); }
set { SetValue("CertificationCountry", value); }
}
public int MaximumSize
{
get { return GetValueInt("MaximumSize", 0); }
set { SetValue("MaximumSize", value); }
}
2017-02-25 21:38:52 +00:00
public int MinimumAge
{
get { return GetValueInt("MinimumAge", 0); }
set { SetValue("MinimumAge", value); }
}
public ProperDownloadTypes DownloadPropersAndRepacks
{
get { return GetValueEnum("DownloadPropersAndRepacks", ProperDownloadTypes.PreferAndUpgrade); }
set { SetValue("DownloadPropersAndRepacks", value); }
}
public bool EnableCompletedDownloadHandling
{
get { return GetValueBoolean("EnableCompletedDownloadHandling", true); }
set { SetValue("EnableCompletedDownloadHandling", value); }
}
public bool PreferIndexerFlags
{
get { return GetValueBoolean("PreferIndexerFlags", false); }
set { SetValue("PreferIndexerFlags", value); }
}
public bool AllowHardcodedSubs
{
get { return GetValueBoolean("AllowHardcodedSubs", false); }
set { SetValue("AllowHardcodedSubs", value); }
}
public string WhitelistedHardcodedSubs
{
get { return GetValue("WhitelistedHardcodedSubs", ""); }
set { SetValue("WhitelistedHardcodedSubs", value); }
}
public bool AutoRedownloadFailed
{
get { return GetValueBoolean("AutoRedownloadFailed", true); }
set { SetValue("AutoRedownloadFailed", value); }
}
2018-11-23 07:03:32 +00:00
public bool CreateEmptyMovieFolders
{
get { return GetValueBoolean("CreateEmptyMovieFolders", false); }
set { SetValue("CreateEmptyMovieFolders", value); }
}
public bool DeleteEmptyFolders
{
2018-11-23 07:03:32 +00:00
get { return GetValueBoolean("DeleteEmptyFolders", false); }
2018-11-23 07:03:32 +00:00
set { SetValue("DeleteEmptyFolders", value); }
}
public FileDateType FileDate
{
get { return GetValueEnum("FileDate", FileDateType.None); }
set { SetValue("FileDate", value); }
}
public string DownloadClientWorkingFolders
{
get { return GetValue("DownloadClientWorkingFolders", "_UNPACK_|_FAILED_"); }
set { SetValue("DownloadClientWorkingFolders", value); }
}
public int CheckForFinishedDownloadInterval
{
get { return GetValueInt("CheckForFinishedDownloadInterval", 1); }
set { SetValue("CheckForFinishedDownloadInterval", value); }
}
public int DownloadClientHistoryLimit
{
get { return GetValueInt("DownloadClientHistoryLimit", 60); }
set { SetValue("DownloadClientHistoryLimit", value); }
}
public bool SkipFreeSpaceCheckWhenImporting
{
get { return GetValueBoolean("SkipFreeSpaceCheckWhenImporting", false); }
set { SetValue("SkipFreeSpaceCheckWhenImporting", value); }
}
public int MinimumFreeSpaceWhenImporting
{
get { return GetValueInt("MinimumFreeSpaceWhenImporting", 100); }
set { SetValue("MinimumFreeSpaceWhenImporting", value); }
}
public bool CopyUsingHardlinks
{
get { return GetValueBoolean("CopyUsingHardlinks", true); }
set { SetValue("CopyUsingHardlinks", value); }
}
public bool EnableMediaInfo
{
get { return GetValueBoolean("EnableMediaInfo", true); }
set { SetValue("EnableMediaInfo", value); }
}
public bool ImportExtraFiles
{
get { return GetValueBoolean("ImportExtraFiles", false); }
set { SetValue("ImportExtraFiles", value); }
}
public string ExtraFileExtensions
{
get { return GetValue("ExtraFileExtensions", "srt"); }
set { SetValue("ExtraFileExtensions", value); }
}
public bool AutoRenameFolders
{
get { return GetValueBoolean("AutoRenameFolders", false); }
set { SetValue("AutoRenameFolders", value); }
}
2018-11-23 07:03:32 +00:00
public RescanAfterRefreshType RescanAfterRefresh
{
get { return GetValueEnum("RescanAfterRefresh", RescanAfterRefreshType.Always); }
set { SetValue("RescanAfterRefresh", value); }
}
public bool SetPermissionsLinux
2014-01-26 08:57:14 +00:00
{
get { return GetValueBoolean("SetPermissionsLinux", false); }
set { SetValue("SetPermissionsLinux", value); }
}
public string ChmodFolder
{
get { return GetValue("ChmodFolder", "755"); }
set { SetValue("ChmodFolder", value); }
}
public string ChownGroup
{
get { return GetValue("ChownGroup", ""); }
set { SetValue("ChownGroup", value); }
}
public int FirstDayOfWeek
{
2018-04-21 04:59:31 +00:00
get { return GetValueInt("FirstDayOfWeek", (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek); }
set { SetValue("FirstDayOfWeek", value); }
}
public string CalendarWeekColumnHeader
{
get { return GetValue("CalendarWeekColumnHeader", "ddd M/D"); }
set { SetValue("CalendarWeekColumnHeader", value); }
}
public MovieRuntimeFormatType MovieRuntimeFormat
{
get { return GetValueEnum("MovieRuntimeFormat", MovieRuntimeFormatType.HoursMinutes); }
set { SetValue("MovieRuntimeFormat", value); }
}
public string ShortDateFormat
{
get { return GetValue("ShortDateFormat", "MMM D YYYY"); }
set { SetValue("ShortDateFormat", value); }
}
public string LongDateFormat
{
get { return GetValue("LongDateFormat", "dddd, MMMM D YYYY"); }
set { SetValue("LongDateFormat", value); }
}
public string TimeFormat
{
get { return GetValue("TimeFormat", "h(:mm)a"); }
set { SetValue("TimeFormat", value); }
}
public bool ShowRelativeDates
{
get { return GetValueBoolean("ShowRelativeDates", true); }
set { SetValue("ShowRelativeDates", value); }
}
public bool EnableColorImpairedMode
{
get { return GetValueBoolean("EnableColorImpairedMode", false); }
set { SetValue("EnableColorImpairedMode", value); }
}
2020-05-26 01:55:10 +00:00
public int MovieInfoLanguage
{
get { return GetValueInt("MovieInfoLanguage", (int)Language.English); }
set { SetValue("MovieInfoLanguage", value); }
}
public int UILanguage
{
get { return GetValueInt("UILanguage", (int)Language.English); }
set { SetValue("UILanguage", value); }
}
2015-01-02 18:57:51 +00:00
public bool CleanupMetadataImages
{
get { return GetValueBoolean("CleanupMetadataImages", true); }
set { SetValue("CleanupMetadataImages", value); }
}
public string PlexClientIdentifier => GetValue("PlexClientIdentifier", Guid.NewGuid().ToString(), true);
2016-12-09 06:54:15 +00:00
public string RijndaelPassphrase => GetValue("RijndaelPassphrase", Guid.NewGuid().ToString(), true);
2015-01-26 02:03:21 +00:00
2016-12-09 06:54:15 +00:00
public string HmacPassphrase => GetValue("HmacPassphrase", Guid.NewGuid().ToString(), true);
2015-01-26 02:03:21 +00:00
2016-12-09 06:54:15 +00:00
public string RijndaelSalt => GetValue("RijndaelSalt", Guid.NewGuid().ToString(), true);
2015-01-26 02:03:21 +00:00
2016-12-09 06:54:15 +00:00
public string HmacSalt => GetValue("HmacSalt", Guid.NewGuid().ToString(), true);
2015-01-26 02:03:21 +00:00
2016-12-09 06:54:15 +00:00
public bool ProxyEnabled => GetValueBoolean("ProxyEnabled", false);
2016-12-09 06:54:15 +00:00
public ProxyType ProxyType => GetValueEnum<ProxyType>("ProxyType", ProxyType.Http);
2016-12-09 06:54:15 +00:00
public string ProxyHostname => GetValue("ProxyHostname", string.Empty);
2016-12-09 06:54:15 +00:00
public int ProxyPort => GetValueInt("ProxyPort", 8080);
2016-12-09 06:54:15 +00:00
public string ProxyUsername => GetValue("ProxyUsername", string.Empty);
2016-12-09 06:54:15 +00:00
public string ProxyPassword => GetValue("ProxyPassword", string.Empty);
2016-12-09 06:54:15 +00:00
public string ProxyBypassFilter => GetValue("ProxyBypassFilter", string.Empty);
2016-12-09 06:54:15 +00:00
public bool ProxyBypassLocalAddresses => GetValueBoolean("ProxyBypassLocalAddresses", true);
2018-11-23 07:03:32 +00:00
public string BackupFolder => GetValue("BackupFolder", "Backups");
public int BackupInterval => GetValueInt("BackupInterval", 7);
public int BackupRetention => GetValueInt("BackupRetention", 28);
public CertificateValidationType CertificateValidation =>
GetValueEnum("CertificateValidation", CertificateValidationType.Enabled);
private string GetValue(string key)
{
return GetValue(key, string.Empty);
2010-09-23 03:19:47 +00:00
}
private bool GetValueBoolean(string key, bool defaultValue = false)
{
2011-06-17 02:27:10 +00:00
return Convert.ToBoolean(GetValue(key, defaultValue));
}
private int GetValueInt(string key, int defaultValue = 0)
{
2012-12-31 21:09:43 +00:00
return Convert.ToInt32(GetValue(key, defaultValue));
}
private T GetValueEnum<T>(string key, T defaultValue)
2013-05-14 05:22:51 +00:00
{
return (T)Enum.Parse(typeof(T), GetValue(key, defaultValue), true);
}
public string GetValue(string key, object defaultValue, bool persist = false)
2010-09-23 03:19:47 +00:00
{
key = key.ToLowerInvariant();
Ensure.That(key, () => key).IsNotNullOrWhiteSpace();
EnsureCache();
string dbValue;
2010-09-23 03:19:47 +00:00
if (_cache.TryGetValue(key, out dbValue) && dbValue != null && !string.IsNullOrEmpty(dbValue))
{
return dbValue;
}
2010-09-23 03:19:47 +00:00
2015-03-25 00:39:40 +00:00
_logger.Trace("Using default config value for '{0}' defaultValue:'{1}'", key, defaultValue);
2012-01-25 03:09:49 +00:00
if (persist)
2013-02-24 06:48:52 +00:00
{
2012-01-25 03:09:49 +00:00
SetValue(key, defaultValue.ToString());
2013-02-24 06:48:52 +00:00
}
return defaultValue.ToString();
2010-09-23 03:19:47 +00:00
}
private void SetValue(string key, bool value)
{
SetValue(key, value.ToString());
}
private void SetValue(string key, int value)
{
SetValue(key, value.ToString());
}
private void SetValue(string key, Enum value)
{
SetValue(key, value.ToString().ToLower());
}
private void SetValue(string key, string value)
2010-09-23 03:19:47 +00:00
{
2013-03-06 18:41:13 +00:00
key = key.ToLowerInvariant();
2010-09-24 05:37:48 +00:00
2014-03-13 20:12:42 +00:00
_logger.Trace("Writing Setting to database. Key:'{0}' Value:'{1}'", key, value);
2015-11-26 20:05:37 +00:00
_repository.Upsert(key, value);
ClearCache();
}
private void EnsureCache()
{
2013-02-24 06:48:52 +00:00
lock (_cache)
{
2013-02-24 06:48:52 +00:00
if (!_cache.Any())
2011-06-23 06:56:17 +00:00
{
var all = _repository.All();
_cache = all.ToDictionary(c => c.Key.ToLower(), c => c.Value);
2011-06-23 06:56:17 +00:00
}
}
2010-09-23 03:19:47 +00:00
}
private static void ClearCache()
{
2013-02-24 06:48:52 +00:00
lock (_cache)
{
2013-02-24 06:48:52 +00:00
_cache = new Dictionary<string, string>();
}
}
2010-09-23 03:19:47 +00:00
}
}