Skip to content

Commit

Permalink
#137 "Parent" proxy setting in app config now.
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkAlex committed May 18, 2019
1 parent 0331364 commit dbf64f8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
6 changes: 5 additions & 1 deletion MangaReader.Core/Account/ProxySetting.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net;
using MangaReader.Core.Services;

namespace MangaReader.Core.Account
{
Expand Down Expand Up @@ -32,6 +33,8 @@ public virtual IWebProxy GetProxy()
return SystemProxy.Value;
case ProxySettingType.Manual:
return new WebProxy(Address, true, null, new NetworkCredential(UserName, Password));
case ProxySettingType.Parent:
return MangaSettingCache.Get(typeof(IPlugin)).Proxy;
default:
throw new ArgumentOutOfRangeException();
}
Expand All @@ -52,6 +55,7 @@ public enum ProxySettingType
{
NoProxy,
System,
Manual
Manual,
Parent
}
}
7 changes: 4 additions & 3 deletions MangaReader.Core/Convertation/Config/From47To48.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MangaReader.Core.Convertation.Primitives;
using MangaReader.Core.NHibernate;
using MangaReader.Core.Services;
using MangaReader.Core.Services.Config;

namespace MangaReader.Core.Convertation.Config
{
Expand All @@ -14,14 +15,14 @@ protected override async Task ProtectedConvert(IProcess process)
{
using (var context = Repository.GetEntityContext())
{
var proxySetting = await context
var settingProxySetting = await context
.Get<ProxySetting>()
.Where(s => s.SettingType == ProxySettingType.System)
.Where(s => s.SettingType == ProxySettingType.Parent)
.SingleAsync().ConfigureAwait(false);
var settings = await context.Get<MangaSetting>().Where(s => s.ProxySetting == null).ToListAsync().ConfigureAwait(false);
foreach (var setting in settings)
{
setting.ProxySetting = proxySetting;
setting.ProxySetting = settingProxySetting;
}

await settings.SaveAll(context).ConfigureAwait(false);
Expand Down
3 changes: 2 additions & 1 deletion MangaReader.Core/NHibernate/DatabaseConfigMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public DatabaseConfigMap()
Map(x => x.Version);
Map(x => x.UniqueId);
Map(x => x.FolderNamingStrategy);
References(x => x.ProxySetting);
}
}
}
}
33 changes: 24 additions & 9 deletions MangaReader.Core/Services/Config/DatabaseConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using MangaReader.Core.Account;
Expand All @@ -22,6 +23,7 @@ public class DatabaseConfig : Entity.Entity
/// <summary>
/// Уникальный идентификатор базы данных.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Guid UniqueId
{
get
Expand All @@ -39,6 +41,11 @@ public Guid UniqueId

private Guid uniqueId;

/// <summary>
/// Настройки прокси.
/// </summary>
public ProxySetting ProxySetting { get; set; }

/// <summary>
/// Создать дефолтные настройки для новых типов.
/// </summary>
Expand All @@ -59,7 +66,7 @@ private static async Task CreateDefaultMangaSettings(RepositoryContext context)
Manga = plugin.MangaGuid,
MangaName = plugin.Name,
DefaultCompression = Compression.CompressionMode.Manga,
ProxySetting = await context.Get<ProxySetting>().SingleAsync(s => s.SettingType == ProxySettingType.System).ConfigureAwait(false),
ProxySetting = await context.Get<ProxySetting>().SingleAsync(s => s.SettingType == ProxySettingType.Parent).ConfigureAwait(false),
Login = await Login.Get(plugin.LoginType).ConfigureAwait(false)
};

Expand All @@ -70,18 +77,21 @@ private static async Task CreateDefaultMangaSettings(RepositoryContext context)
await MangaSettingCache.RevalidateCache().ConfigureAwait(false);
}

private static async Task CreateDefaultProxySettings(RepositoryContext context)
private static async Task<List<ProxySetting>> CreateDefaultProxySettings(RepositoryContext context)
{
var types = new[] { ProxySettingType.NoProxy, ProxySettingType.System };
var types = new[] { ProxySettingType.NoProxy, ProxySettingType.System, ProxySettingType.Parent };
var created = new List<ProxySetting>();
foreach (var settingType in types)
{
if (!await context.Get<ProxySetting>().AnyAsync(s => s.SettingType == settingType)
.ConfigureAwait(false))
if (!await context.Get<ProxySetting>().AnyAsync(s => s.SettingType == settingType).ConfigureAwait(false))
{
var noProxy = new ProxySetting(settingType);
await context.Save(noProxy).ConfigureAwait(false);
var proxy = new ProxySetting(settingType);
await context.Save(proxy).ConfigureAwait(false);
created.Add(proxy);
}
}

return created;
}

public DatabaseConfig()
Expand All @@ -92,10 +102,15 @@ public DatabaseConfig()

public static async Task Initialize()
{
await Repository.GetStateless<DatabaseConfig>().SingleOrCreate().ConfigureAwait(false);
using (var context = Repository.GetEntityContext("Initialize database config"))
{
await CreateDefaultProxySettings(context).ConfigureAwait(false);
var config = await Repository.GetStateless<DatabaseConfig>().SingleOrCreate().ConfigureAwait(false);
var proxySettings = await CreateDefaultProxySettings(context).ConfigureAwait(false);
if (config.ProxySetting == null)
{
config.ProxySetting = proxySettings.FirstOrDefault(s => s.SettingType == ProxySettingType.System);
await context.Save(config).ConfigureAwait(false);
}
await CreateDefaultMangaSettings(context).ConfigureAwait(false);
}
}
Expand Down
27 changes: 20 additions & 7 deletions MangaReader.Core/Services/MangaSettingCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MangaReader.Core.Account;
using MangaReader.Core.NHibernate;
using MangaReader.Core.Services.Config;

Expand Down Expand Up @@ -40,19 +41,31 @@ public static async Task RevalidateCache()

using (var context = Repository.GetEntityContext())
{
var config = await context.Get<DatabaseConfig>().SingleAsync().ConfigureAwait(false);
var parentSetting = config.ProxySetting;
if (parentSetting != null)
{
MangaSettingCache.Set(new MangaSettingCache()
{
Plugin = typeof(IPlugin),
Proxy = parentSetting.GetProxy()
});
}

var settings = await context.Get<MangaSetting>().Where(s => s.ProxySetting != null).ToListAsync().ConfigureAwait(false);
foreach (var setting in settings)
MangaSettingCache.Set(new MangaSettingCache(setting));
{
var plugin = ConfigStorage.Plugins.Single(p => p.MangaGuid == setting.Manga).GetType();
MangaSettingCache.Set(new MangaSettingCache
{
Plugin = plugin,
Proxy = setting.ProxySetting.SettingType == ProxySettingType.Parent ? Get(typeof(IPlugin)).Proxy : setting.ProxySetting.GetProxy()
});
}

foreach (var grouped in settings.GroupBy(s => s.ProxySetting.SettingType))
Log.Add($"Applied {grouped.Key} proxy to {string.Join(", ", grouped.Select(s => s.MangaName))}");
}
}

public MangaSettingCache(MangaSetting setting)
{
this.Plugin = ConfigStorage.Plugins.Single(p => p.MangaGuid == setting.Manga).GetType();
this.Proxy = setting.ProxySetting.GetProxy();
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.Entities/MangaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public async Task Download(MangaInfo mangaInfo)
}

manga = await Mangas.CreateFromWeb(new Uri(mangaInfo.Uri)).ConfigureAwait(false);
DirectoryHelpers.DeleteDirectory(manga.GetAbsoluteFolderPath());
sw.Start();
await manga.Download().ConfigureAwait(false);
}
Expand Down

0 comments on commit dbf64f8

Please sign in to comment.