Refactor done

This commit is contained in:
KZ
2015-07-19 14:22:50 +01:00
parent e518a3b58e
commit 1359ad16f0
94 changed files with 1338 additions and 1407 deletions

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -19,22 +20,115 @@ namespace Jackett.Services
string GetAppDataFolder();
JObject ReadServerSettingsFile();
string GetSonarrConfigFile();
T GetConfig<T>();
void SaveConfig<T>(T config);
string ApplicationFolder();
}
public class ConfigurationService: IConfigurationService
{
private ISerializeService serializeService;
private Logger logger;
public ConfigurationService(ISerializeService s, Logger l)
{
serializeService = s;
logger = l;
CreateOrMigrateSettings();
}
private void CreateOrMigrateSettings()
{
try
{
if (!Directory.Exists(GetAppDataFolder()))
{
Directory.CreateDirectory(GetAppDataFolder());
}
logger.Debug("App config/log directory: " + GetAppDataFolder());
}
catch (Exception ex)
{
throw new Exception("Could not create settings directory. " + ex.Message);
}
try
{
string oldDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Jackett");
if (Directory.Exists(oldDir))
{
foreach (var file in Directory.GetFiles(oldDir, "*", SearchOption.AllDirectories))
{
var path = file.Replace(oldDir, "");
var destFolder = GetAppDataFolder()+ path;
if (!Directory.Exists(Path.GetDirectoryName(destFolder)))
{
Directory.CreateDirectory(Path.GetDirectoryName(destFolder));
}
File.Move(file, destFolder);
}
}
}
catch (Exception ex)
{
logger.Error("ERROR could not migrate settings directory " + ex);
}
}
public T GetConfig<T>()
{
var type = typeof(T);
var fullPath = Path.Combine(GetAppDataFolder(), type.Name + ".json");
try
{
if (!File.Exists(fullPath))
{
logger.Debug("Config file does not exist: " + fullPath);
return default(T);
}
return serializeService.DeSerialise<T>(File.ReadAllText(fullPath));
}
catch(Exception e)
{
logger.Error(e, "Error reading config file " + fullPath);
return default(T);
}
}
public void SaveConfig<T>(T config)
{
var type = typeof(T);
var fullPath = Path.Combine(GetAppDataFolder(), type.Name + ".json");
try
{
var json = serializeService.Serialise(config);
if (!Directory.Exists(GetAppDataFolder()))
Directory.CreateDirectory(GetAppDataFolder());
File.WriteAllText(fullPath, json);
}
catch (Exception e)
{
logger.Error(e, "Error reading config file " + fullPath);
}
}
public string ApplicationFolder()
{
return Path.GetDirectoryName(Application.ExecutablePath);
}
public string GetContentFolder()
{
var baseDir = Path.GetDirectoryName(Application.ExecutablePath);
// If we are debugging we can use the non copied content.
if (Debugger.IsAttached)
var dir = Path.Combine(ApplicationFolder(), "Content");
if (!Directory.Exists(dir))
{
return Path.Combine(baseDir, "..\\..\\..\\Jackett\\WebContent");
}
else
{
return Path.Combine(baseDir, "WebContent");
dir = Path.Combine(ApplicationFolder(), "..\\..\\..\\Jackett\\Content");
}
return dir;
}
public string GetVersion()
@@ -44,7 +138,16 @@ namespace Jackett.Services
public string GetAppDataFolder()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Jackett"); ;
return GetAppDataFolderStatic();
}
/// <summary>
/// This is needed for the logger prior to ioc setup.
/// </summary>
/// <returns></returns>
public static string GetAppDataFolderStatic()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Jackett");
}
public string GetIndexerConfigDir()