mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
Feature/netcore preparation (#2072)
* Use platform detection that works on mono 4.6+ * Move to use package reference for restoring nuget packages. * DateTimeRoutines does not have Nuget packages that support .NET Standard (and therefore .NET Core). We will have to include them for now until we can get rid of this dependency. * Start spliting some interfaces into their own files - this will help by allowing us to split them out in the future into a seperate project so the actual implementations can stay within their respective architectures when required * Move out common libraries * Few more tidy up tasks to get things working with .NET Standard * Restructure the solution layout * Encoding work to reduce rework later on platforms without Windows codepages (or require compliance with RFC1345) * Move folder structure around to have more natural layout of the solutions * DI server configuration to get rid of "temporary" hack and dependency circle for serverservice * Make all encoding consistent to match the expected encoding casing for earlier versions of mono.
This commit is contained in:

committed by
flightlevel

parent
47a2ffa313
commit
571c52a0f2
149
src/Jackett.Common/Services/IndexerConfigurationService.cs
Normal file
149
src/Jackett.Common/Services/IndexerConfigurationService.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Jackett.Indexers;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
using Jackett.Services.Interfaces;
|
||||
|
||||
namespace Jackett.Services
|
||||
{
|
||||
|
||||
public class IndexerConfigurationService : IIndexerConfigurationService
|
||||
{
|
||||
|
||||
//public override void LoadFromSavedConfiguration(JToken jsonConfig)
|
||||
//{
|
||||
// if (jsonConfig is JObject)
|
||||
// {
|
||||
// configData.CookieHeader.Value = jsonConfig.Value<string>("cookies");
|
||||
// configData.IncludeRaw.Value = jsonConfig.Value<bool>("raws");
|
||||
// IsConfigured = true;
|
||||
// SaveConfig();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// base.LoadFromSavedConfiguration(jsonConfig);
|
||||
//}
|
||||
|
||||
public IndexerConfigurationService(IConfigurationService configService, Logger logger)
|
||||
{
|
||||
this.configService = configService;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public void Delete(IIndexer indexer)
|
||||
{
|
||||
var configFilePath = GetIndexerConfigFilePath(indexer);
|
||||
File.Delete(configFilePath);
|
||||
}
|
||||
|
||||
public void Load(IIndexer idx)
|
||||
{
|
||||
var configFilePath = GetIndexerConfigFilePath(idx);
|
||||
if (File.Exists(configFilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileStr = File.ReadAllText(configFilePath);
|
||||
var jsonString = JToken.Parse(fileStr);
|
||||
idx.LoadFromSavedConfiguration(jsonString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "Failed loading configuration for {0}, trying backup", idx.DisplayName);
|
||||
var configFilePathBak = configFilePath + ".bak";
|
||||
if (File.Exists(configFilePathBak))
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileStrBak = File.ReadAllText(configFilePathBak);
|
||||
var jsonStringBak = JToken.Parse(fileStrBak);
|
||||
idx.LoadFromSavedConfiguration(jsonStringBak);
|
||||
logger.Info("Successfully loaded backup config for {0}", idx.DisplayName);
|
||||
idx.SaveConfig();
|
||||
}
|
||||
catch (Exception exbak)
|
||||
{
|
||||
logger.Error(exbak, "Failed loading backup configuration for {0}, you must reconfigure this indexer", idx.DisplayName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error(ex, "Failed loading backup configuration for {0} (no backup available), you must reconfigure this indexer", idx.DisplayName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Save(IIndexer indexer, JToken obj)
|
||||
{
|
||||
lock (configWriteLock)
|
||||
{
|
||||
var uID = Guid.NewGuid().ToString("N");
|
||||
var configFilePath = GetIndexerConfigFilePath(indexer);
|
||||
var configFilePathBak = configFilePath + ".bak";
|
||||
var configFilePathTmp = configFilePath + "." + uID + ".tmp";
|
||||
var content = obj.ToString();
|
||||
|
||||
logger.Debug(string.Format("Saving new config file: {0}", configFilePathTmp));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(content))
|
||||
{
|
||||
throw new Exception(string.Format("New config content for {0} is empty, please report this bug.", indexer.ID));
|
||||
}
|
||||
|
||||
if (content.Contains("\x00"))
|
||||
{
|
||||
throw new Exception(string.Format("New config content for {0} contains 0x00, please report this bug. Content: {1}", indexer.ID, content));
|
||||
}
|
||||
|
||||
// make sure the config directory exists
|
||||
if (!Directory.Exists(configService.GetIndexerConfigDir()))
|
||||
Directory.CreateDirectory(configService.GetIndexerConfigDir());
|
||||
|
||||
// create new temporary config file
|
||||
File.WriteAllText(configFilePathTmp, content);
|
||||
var fileInfo = new FileInfo(configFilePathTmp);
|
||||
if (fileInfo.Length == 0)
|
||||
{
|
||||
throw new Exception(string.Format("New config file {0} is empty, please report this bug.", configFilePathTmp));
|
||||
}
|
||||
|
||||
// create backup file
|
||||
File.Delete(configFilePathBak);
|
||||
if (File.Exists(configFilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Move(configFilePath, configFilePathBak);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.Error(string.Format("Error while moving {0} to {1}: {2}", configFilePath, configFilePathBak, ex.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
// replace the actual config file
|
||||
File.Delete(configFilePath);
|
||||
try
|
||||
{
|
||||
File.Move(configFilePathTmp, configFilePath);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.Error(string.Format("Error while moving {0} to {1}: {2}", configFilePathTmp, configFilePath, ex.ToString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string GetIndexerConfigFilePath(IIndexer indexer)
|
||||
{
|
||||
return Path.Combine(configService.GetIndexerConfigDir(), indexer.ID + ".json");
|
||||
}
|
||||
|
||||
private IConfigurationService configService;
|
||||
private Logger logger;
|
||||
|
||||
private static readonly object configWriteLock = new object();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user