Jackett/src/Jackett/Services/ConfigurationService.cs

183 lines
5.6 KiB
C#
Raw Normal View History

2015-07-19 00:27:41 +00:00
using Newtonsoft.Json.Linq;
2015-07-19 13:22:50 +00:00
using NLog;
2015-07-19 00:27:41 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Jackett.Services
{
public interface IConfigurationService
{
string GetContentFolder();
string GetVersion();
string GetIndexerConfigDir();
string GetAppDataFolder();
string GetSonarrConfigFile();
2015-07-19 13:22:50 +00:00
T GetConfig<T>();
void SaveConfig<T>(T config);
string ApplicationFolder();
2015-07-19 00:27:41 +00:00
}
2015-07-19 16:35:56 +00:00
public class ConfigurationService : IConfigurationService
2015-07-19 00:27:41 +00:00
{
2015-07-19 13:22:50 +00:00
private ISerializeService serializeService;
private Logger logger;
public ConfigurationService(ISerializeService s, Logger l)
2015-07-19 00:27:41 +00:00
{
2015-07-19 13:22:50 +00:00
serializeService = s;
logger = l;
CreateOrMigrateSettings();
}
private void CreateOrMigrateSettings()
{
try
2015-07-19 00:27:41 +00:00
{
2015-07-19 13:22:50 +00:00
if (!Directory.Exists(GetAppDataFolder()))
{
Directory.CreateDirectory(GetAppDataFolder());
}
logger.Debug("App config/log directory: " + GetAppDataFolder());
2015-07-19 00:27:41 +00:00
}
2015-07-19 13:22:50 +00:00
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, "");
2015-07-19 16:35:56 +00:00
var destFolder = GetAppDataFolder() + path;
2015-07-19 13:22:50 +00:00
if (!Directory.Exists(Path.GetDirectoryName(destFolder)))
{
Directory.CreateDirectory(Path.GetDirectoryName(destFolder));
}
2015-07-19 16:35:56 +00:00
if (!File.Exists(destFolder))
{
File.Move(file, destFolder);
}
2015-07-19 13:22:50 +00:00
}
2015-07-19 16:35:56 +00:00
Directory.Delete(oldDir, true);
2015-07-19 13:22:50 +00:00
}
}
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));
}
2015-07-19 16:35:56 +00:00
catch (Exception e)
2015-07-19 00:27:41 +00:00
{
2015-07-19 13:22:50 +00:00
logger.Error(e, "Error reading config file " + fullPath);
return default(T);
2015-07-19 00:27:41 +00:00
}
}
2015-07-19 13:22:50 +00:00
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()
{
// If we are debugging we can use the non copied content.
string dir = Path.Combine(ApplicationFolder(), "Content"); ;
#if DEBUG
// When we are running in debug use the source files
var sourcePath = Path.GetFullPath(Path.Combine(ApplicationFolder(), "..\\..\\..\\Jackett\\Content"));
if (Directory.Exists(sourcePath))
2015-07-19 13:22:50 +00:00
{
dir = sourcePath;
2015-07-19 13:22:50 +00:00
}
#endif
2015-07-19 13:22:50 +00:00
return dir;
}
2015-07-19 00:27:41 +00:00
public string GetVersion()
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
public string GetAppDataFolder()
{
2015-07-19 13:22:50 +00:00
return GetAppDataFolderStatic();
}
/// <summary>
/// This is needed for the logger prior to ioc setup.
/// </summary>
/// <returns></returns>
public static string GetAppDataFolderStatic()
{
if (System.Environment.OSVersion.Platform == PlatformID.Unix)
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Jackett");
}
else
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Jackett");
}
2015-07-19 00:27:41 +00:00
}
public string GetIndexerConfigDir()
{
2015-07-19 16:35:56 +00:00
return Path.Combine(GetAppDataFolder(), "Indexers");
2015-07-19 00:27:41 +00:00
}
public string GetConfigFile()
{
return Path.Combine(GetAppDataFolder(), "config.json");
}
public string GetSonarrConfigFile()
{
return Path.Combine(GetAppDataFolder(), "sonarr_api.json");
}
}
}