@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
using System.Xml ;
using System.Xml.Linq ;
using NzbDrone.Common.Cache ;
using NzbDrone.Common.Disk ;
using NzbDrone.Common.EnvironmentInfo ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Core.Configuration.Events ;
@ -48,15 +49,22 @@ namespace NzbDrone.Core.Configuration
public const string CONFIG_ELEMENT_NAME = "Config" ;
private readonly IEventAggregator _eventAggregator ;
private readonly IDiskProvider _diskProvider ;
private readonly ICached < string > _cache ;
private readonly string _configFile ;
private static readonly Regex HiddenCharacterRegex = new Regex ( "[^a-z0-9]" , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
public ConfigFileProvider ( IAppFolderInfo appFolderInfo , ICacheManager cacheManager , IEventAggregator eventAggregator )
private static readonly object Mutex = new object ( ) ;
public ConfigFileProvider ( IAppFolderInfo appFolderInfo ,
ICacheManager cacheManager ,
IEventAggregator eventAggregator ,
IDiskProvider diskProvider )
{
_cache = cacheManager . GetCache < string > ( GetType ( ) ) ;
_eventAggregator = eventAggregator ;
_diskProvider = diskProvider ;
_configFile = appFolderInfo . GetConfigPath ( ) ;
}
@ -247,8 +255,6 @@ namespace NzbDrone.Core.Configuration
{
return _cache . Get ( key , ( ) = >
{
EnsureDefaultConfigFile ( ) ;
var xDoc = LoadConfigFile ( ) ;
var config = xDoc . Descendants ( CONFIG_ELEMENT_NAME ) . Single ( ) ;
@ -274,8 +280,6 @@ namespace NzbDrone.Core.Configuration
public void SetValue ( string key , object value )
{
EnsureDefaultConfigFile ( ) ;
var valueString = value . ToString ( ) . Trim ( ) ;
var xDoc = LoadConfigFile ( ) ;
var config = xDoc . Descendants ( CONFIG_ELEMENT_NAME ) . Single ( ) ;
@ -296,7 +300,7 @@ namespace NzbDrone.Core.Configuration
_cache . Set ( key , valueString ) ;
xDoc. Save ( _configFile ) ;
SaveConfigFile( xDoc ) ;
}
public void SetValue ( string key , Enum value )
@ -308,18 +312,12 @@ namespace NzbDrone.Core.Configuration
{
if ( ! File . Exists ( _configFile ) )
{
var xDoc = new XDocument ( new XDeclaration ( "1.0" , "utf-8" , "yes" ) ) ;
xDoc . Add ( new XElement ( CONFIG_ELEMENT_NAME ) ) ;
xDoc . Save ( _configFile ) ;
SaveConfigDictionary ( GetConfigDictionary ( ) ) ;
}
}
private void DeleteOldValues ( )
{
EnsureDefaultConfigFile ( ) ;
var xDoc = LoadConfigFile ( ) ;
var config = xDoc . Descendants ( CONFIG_ELEMENT_NAME ) . Single ( ) ;
@ -336,14 +334,25 @@ namespace NzbDrone.Core.Configuration
}
}
xDoc. Save ( _configFile ) ;
SaveConfigFile( xDoc ) ;
}
private XDocument LoadConfigFile ( )
{
try
{
return XDocument . Load ( _configFile ) ;
lock ( Mutex )
{
if ( _diskProvider . FileExists ( _configFile ) )
{
return XDocument . Load ( _configFile ) ;
}
var xDoc = new XDocument ( new XDeclaration ( "1.0" , "utf-8" , "yes" ) ) ;
xDoc . Add ( new XElement ( CONFIG_ELEMENT_NAME ) ) ;
return xDoc ;
}
}
catch ( XmlException ex )
@ -352,6 +361,14 @@ namespace NzbDrone.Core.Configuration
}
}
private void SaveConfigFile ( XDocument xDoc )
{
lock ( Mutex )
{
_diskProvider . WriteAllText ( _configFile , xDoc . ToString ( ) ) ;
}
}
private string GenerateApiKey ( )
{
return Guid . NewGuid ( ) . ToString ( ) . Replace ( "-" , "" ) ;
@ -359,6 +376,7 @@ namespace NzbDrone.Core.Configuration
public void HandleAsync ( ApplicationStartedEvent message )
{
EnsureDefaultConfigFile ( ) ;
DeleteOldValues ( ) ;
}