2020-02-09 02:35:16 +00:00
|
|
|
using System;
|
2017-07-10 20:58:44 +00:00
|
|
|
using System.IO;
|
2018-03-10 08:05:56 +00:00
|
|
|
using Jackett.Common.Indexers;
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
2017-07-10 20:58:44 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using NLog;
|
|
|
|
|
2018-03-10 08:05:56 +00:00
|
|
|
namespace Jackett.Common.Services
|
2017-07-10 20:58:44 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-05-12 19:58:48 +00:00
|
|
|
var configFilePath = GetIndexerConfigFilePath(indexer.Id);
|
2017-07-10 20:58:44 +00:00
|
|
|
File.Delete(configFilePath);
|
2019-08-26 07:05:25 +00:00
|
|
|
var configFilePathBak = configFilePath + ".bak";
|
|
|
|
if (File.Exists(configFilePathBak))
|
|
|
|
File.Delete(configFilePathBak);
|
2017-07-10 20:58:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 19:58:48 +00:00
|
|
|
public void Load(IIndexer indexer)
|
2017-07-10 20:58:44 +00:00
|
|
|
{
|
2020-05-12 19:58:48 +00:00
|
|
|
var configFilePath = GetIndexerConfigFilePath(indexer.Id);
|
2020-09-24 20:02:45 +00:00
|
|
|
if (!File.Exists(configFilePath))
|
|
|
|
return;
|
|
|
|
try
|
2017-07-10 20:58:44 +00:00
|
|
|
{
|
2020-09-24 20:02:45 +00:00
|
|
|
var fileStr = File.ReadAllText(configFilePath);
|
|
|
|
var jsonString = JToken.Parse(fileStr);
|
|
|
|
indexer.LoadFromSavedConfiguration(jsonString);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.Error($"Failed loading configuration for {indexer.DisplayName}, trying backup\n{e}");
|
|
|
|
var configFilePathBak = configFilePath + ".bak";
|
|
|
|
if (File.Exists(configFilePathBak))
|
|
|
|
try
|
2017-07-10 20:58:44 +00:00
|
|
|
{
|
2020-09-24 20:02:45 +00:00
|
|
|
var fileStrBak = File.ReadAllText(configFilePathBak);
|
|
|
|
var jsonStringBak = JToken.Parse(fileStrBak);
|
|
|
|
indexer.LoadFromSavedConfiguration(jsonStringBak);
|
|
|
|
logger.Info($"Successfully loaded backup config for {indexer.DisplayName}");
|
|
|
|
indexer.SaveConfig();
|
2017-07-10 20:58:44 +00:00
|
|
|
}
|
2020-09-24 20:02:45 +00:00
|
|
|
catch (Exception e2)
|
2017-07-10 20:58:44 +00:00
|
|
|
{
|
2020-09-24 20:02:45 +00:00
|
|
|
logger.Error($"Failed loading backup configuration for {indexer.DisplayName}, you must reconfigure this indexer\n{e2}");
|
2017-07-10 20:58:44 +00:00
|
|
|
}
|
2020-09-24 20:02:45 +00:00
|
|
|
else
|
|
|
|
logger.Error($"Failed loading backup configuration for {indexer.DisplayName} (no backup available), you must reconfigure this indexer\n{e}");
|
2017-07-10 20:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Save(IIndexer indexer, JToken obj)
|
|
|
|
{
|
|
|
|
lock (configWriteLock)
|
|
|
|
{
|
2020-09-24 20:02:45 +00:00
|
|
|
var uId = Guid.NewGuid().ToString("N");
|
2020-05-12 19:58:48 +00:00
|
|
|
var configFilePath = GetIndexerConfigFilePath(indexer.Id);
|
2017-07-10 20:58:44 +00:00
|
|
|
var configFilePathBak = configFilePath + ".bak";
|
2020-09-24 20:02:45 +00:00
|
|
|
var configFilePathTmp = configFilePath + "." + uId + ".tmp";
|
2017-07-10 20:58:44 +00:00
|
|
|
var content = obj.ToString();
|
|
|
|
|
2020-09-24 20:02:45 +00:00
|
|
|
logger.Debug($"Saving new config file: {configFilePathTmp}");
|
2017-07-10 20:58:44 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(content))
|
2020-09-24 20:02:45 +00:00
|
|
|
throw new Exception($"New config content for {indexer.Id} is empty, please report this bug.");
|
2017-07-10 20:58:44 +00:00
|
|
|
|
|
|
|
if (content.Contains("\x00"))
|
2020-09-24 20:02:45 +00:00
|
|
|
throw new Exception($"New config content for {indexer.Id} contains 0x00, please report this bug. Content: {content}");
|
2017-07-10 20:58:44 +00:00
|
|
|
|
|
|
|
// 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)
|
2020-09-24 20:02:45 +00:00
|
|
|
throw new Exception($"New config file {configFilePathTmp} is empty, please report this bug.");
|
2017-07-10 20:58:44 +00:00
|
|
|
|
|
|
|
// create backup file
|
|
|
|
File.Delete(configFilePathBak);
|
|
|
|
if (File.Exists(configFilePath))
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File.Move(configFilePath, configFilePathBak);
|
|
|
|
}
|
2020-09-24 20:02:45 +00:00
|
|
|
catch (IOException e)
|
2017-07-10 20:58:44 +00:00
|
|
|
{
|
2020-09-24 20:02:45 +00:00
|
|
|
logger.Error($"Error while moving {configFilePath} to {configFilePathBak}\n{e}");
|
2017-07-10 20:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// replace the actual config file
|
|
|
|
File.Delete(configFilePath);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File.Move(configFilePathTmp, configFilePath);
|
|
|
|
}
|
2020-09-24 20:02:45 +00:00
|
|
|
catch (IOException e)
|
2017-07-10 20:58:44 +00:00
|
|
|
{
|
2020-09-24 20:02:45 +00:00
|
|
|
logger.Error($"Error while moving {configFilePathTmp} to {configFilePath}\n{e}");
|
2017-07-10 20:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:58:48 +00:00
|
|
|
public string GetIndexerConfigFilePath(string indexerId)
|
|
|
|
=> Path.Combine(configService.GetIndexerConfigDir(), indexerId + ".json");
|
2017-07-10 20:58:44 +00:00
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
private readonly IConfigurationService configService;
|
|
|
|
private readonly Logger logger;
|
2017-07-10 20:58:44 +00:00
|
|
|
|
|
|
|
private static readonly object configWriteLock = new object();
|
|
|
|
}
|
|
|
|
}
|