Jackett/src/Jackett.Common/Models/IndexerConfig/ConfigurationData.cs

215 lines
8.3 KiB
C#
Raw Normal View History

2020-02-09 02:35:16 +00:00
using System;
2015-04-13 06:25:21 +00:00
using System.Collections.Generic;
using System.Linq;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using Newtonsoft.Json.Linq;
2015-04-13 06:25:21 +00:00
namespace Jackett.Common.Models.IndexerConfig
2015-04-13 06:25:21 +00:00
{
public class ConfigurationData
2015-04-13 06:25:21 +00:00
{
private const string _PasswordReplacement = "|||%%PREVJACKPASSWD%%|||";
protected readonly Dictionary<string, Item> _dynamics = new Dictionary<string, Item>(); // list for dynamic items
public ConfigurationData() {}
2015-08-07 19:09:13 +00:00
public ConfigurationData(JToken json, IProtectionService ps) => LoadValuesFromJson(json, ps);
public HiddenItem CookieHeader { get; } = new HiddenItem {Name = "CookieHeader"};
public HiddenItem LastError { get; } = new HiddenItem {Name = "LastError"};
public StringItem SiteLink { get; } = new StringItem {Name = "Site Link"};
public void AddDynamic(string id, Item item) => _dynamics[id] = item;
public Item GetDynamic(string id)
2015-04-13 06:25:21 +00:00
{
_dynamics.TryGetValue(id, out var item);
return item;
2015-04-13 06:25:21 +00:00
}
public Item GetDynamicByName(string name) =>
_dynamics.Values.FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.InvariantCultureIgnoreCase));
private Item[] GetItems(bool forDisplay)
{
var properties = GetType().GetProperties().Where(p => p.CanRead)
.Where(p => p.PropertyType.IsSubclassOf(typeof(Item)))
.Select(p => (Item)p.GetValue(this)).ToList();
// remove/insert Site Link manualy to make sure it shows up first
properties.Remove(SiteLink);
properties.Insert(0, SiteLink);
properties.AddRange(_dynamics.Values);
if (!forDisplay)
properties.RemoveAll(property => property is ImageItem);
return properties.ToArray();
}
2020-02-09 02:35:16 +00:00
public void LoadValuesFromJson(JToken json, IProtectionService ps = null)
2015-04-13 06:25:21 +00:00
{
if (json == null)
return;
var arr = (JArray)json;
foreach (var item in GetItems(false))
2015-04-13 06:25:21 +00:00
{
var arrItem = arr.FirstOrDefault(f => f.Value<string>("id") == item.ID);
if (arrItem == null)
continue;
switch (item)
2015-04-13 06:25:21 +00:00
{
case HiddenItem hiddenItem:
hiddenItem.Value = arrItem.Value<string>("value");
break;
case BoolItem boolItem:
boolItem.Value = arrItem.Value<bool>("value");
break;
case CheckboxItem checkboxItem:
var values = arrItem.Value<JArray>("values");
if (values != null)
checkboxItem.Values = values.Values<string>().ToArray();
break;
case SelectItem selectItem:
selectItem.Value = arrItem.Value<string>("value");
break;
case RecaptchaItem recaptcha:
recaptcha.Value = arrItem.Value<string>("value");
recaptcha.Cookie = arrItem.Value<string>("cookie");
recaptcha.Version = arrItem.Value<string>("version");
recaptcha.Challenge = arrItem.Value<string>("challenge");
break;
case StringItem sItem:
2015-08-07 19:09:13 +00:00
var newValue = arrItem.Value<string>("value");
if (string.Equals(item.Name, "password", StringComparison.InvariantCultureIgnoreCase))
{
if (newValue != _PasswordReplacement)
2015-08-07 19:09:13 +00:00
{
sItem.Value = newValue;
if (ps != null)
sItem.Value = ps.UnProtect(newValue);
}
}
else
2015-08-07 19:09:13 +00:00
sItem.Value = newValue;
break;
2015-04-13 06:25:21 +00:00
}
}
}
2015-08-07 19:09:13 +00:00
public JToken ToJson(IProtectionService ps, bool forDisplay = true)
2015-04-13 06:25:21 +00:00
{
var items = GetItems(forDisplay);
2015-04-13 06:25:21 +00:00
var jArray = new JArray();
foreach (var item in items)
{
var jObject = new JObject
{
["id"] = item.ID,
["name"] = item.Name
};
switch (item)
2015-04-13 06:25:21 +00:00
{
case RecaptchaItem recaptcha:
jObject["sitekey"] = recaptcha.SiteKey;
jObject["version"] = recaptcha.Version;
break;
case StringItem stringItem:
var value = stringItem.Value;
// if we change this logic we've to change the MigratedFromDPAPI() logic too, #2114 is realted
if (string.Equals(stringItem.Name, "password", StringComparison.InvariantCultureIgnoreCase))
2015-08-07 19:09:13 +00:00
{
if (string.IsNullOrEmpty(value))
value = string.Empty;
else if (forDisplay)
value = _PasswordReplacement;
2015-08-07 19:09:13 +00:00
else if (ps != null)
value = ps.Protect(value);
}
2015-08-07 19:09:13 +00:00
jObject["value"] = value;
2015-04-13 06:25:21 +00:00
break;
case BoolItem boolItem:
jObject["value"] = boolItem.Value;
2015-04-13 06:25:21 +00:00
break;
case CheckboxItem checkboxItem:
jObject["values"] = new JArray(checkboxItem.Values);
jObject["options"] = new JObject();
foreach (var option in checkboxItem.Options)
jObject["options"][option.Key] = option.Value;
break;
case SelectItem selectItem:
jObject["value"] = selectItem.Value;
jObject["options"] = new JObject();
foreach (var option in selectItem.Options)
jObject["options"][option.Key] = option.Value;
break;
case ImageItem imageItem:
var dataUri = DataUrlUtils.BytesToDataUrl(imageItem.Value, "image/jpeg");
2015-04-13 06:25:21 +00:00
jObject["value"] = dataUri;
break;
}
2015-04-13 06:25:21 +00:00
jArray.Add(jObject);
}
2015-04-13 06:25:21 +00:00
return jArray;
}
public class BoolItem : Item
{
public bool Value { get; set; }
}
public class CheckboxItem : Item
{
public CheckboxItem(Dictionary<string, string> options) => Options = options;
public Dictionary<string, string> Options { get; }
public string[] Values { get; set; }
}
2018-06-26 15:47:19 +00:00
public class DisplayItem : StringItem
2015-04-13 06:25:21 +00:00
{
public DisplayItem(string value) => Value = value;
2015-04-13 06:25:21 +00:00
}
public class HiddenItem : StringItem
{
public HiddenItem(string value = "") => Value = value;
}
public class ImageItem : Item
2015-04-18 06:05:30 +00:00
{
public byte[] Value { get; set; }
2015-04-18 06:05:30 +00:00
}
public class Item
2015-04-13 06:25:21 +00:00
{
public string ID => Name.Replace(" ", "").ToLower();
public string Name { get; set; }
2015-04-13 06:25:21 +00:00
}
public class RecaptchaItem : StringItem
{
public RecaptchaItem() => Version = "2";
public string Challenge { get; set; }
public string Version { get; set; }
2015-04-13 06:25:21 +00:00
}
public class SelectItem : Item
{
public SelectItem(Dictionary<string, string> options) => Options = options;
public Dictionary<string, string> Options { get; }
public string Value { get; set; }
}
public class StringItem : Item
{
public string Cookie { get; set; }
public string SiteKey { get; set; }
public string Value { get; set; }
}
2015-04-13 06:25:21 +00:00
}
}