diff --git a/server/RdtClient.Data/Data/DownloadData.cs b/server/RdtClient.Data/Data/DownloadData.cs index ad06880..f5e423d 100644 --- a/server/RdtClient.Data/Data/DownloadData.cs +++ b/server/RdtClient.Data/Data/DownloadData.cs @@ -9,12 +9,10 @@ namespace RdtClient.Data.Data public class DownloadData { private readonly DataContext _dataContext; - private readonly TorrentData _torrentData; - public DownloadData(DataContext dataContext, TorrentData torrentData) + public DownloadData(DataContext dataContext) { _dataContext = dataContext; - _torrentData = torrentData; } public async Task GetById(Guid downloadId) @@ -48,7 +46,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); return download; } @@ -62,7 +60,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateDownloadStarted(Guid downloadId, DateTimeOffset? dateTime) @@ -74,7 +72,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateDownloadFinished(Guid downloadId, DateTimeOffset? dateTime) @@ -91,7 +89,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateUnpackingQueued(Guid downloadId, DateTimeOffset? dateTime) @@ -103,7 +101,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateUnpackingStarted(Guid downloadId, DateTimeOffset? dateTime) @@ -115,7 +113,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateUnpackingFinished(Guid downloadId, DateTimeOffset? dateTime) @@ -127,7 +125,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateCompleted(Guid downloadId, DateTimeOffset? dateTime) @@ -139,7 +137,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateError(Guid downloadId, String error) @@ -151,7 +149,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task UpdateRetryCount(Guid downloadId, Int32 retryCount) @@ -163,7 +161,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task DeleteForTorrent(Guid torrentId) @@ -176,7 +174,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } public async Task Reset(Guid downloadId) @@ -198,7 +196,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - await _torrentData.VoidCache(); + await TorrentData.VoidCache(); } } } \ No newline at end of file diff --git a/server/RdtClient.Data/Data/SettingData.cs b/server/RdtClient.Data/Data/SettingData.cs index 043fafa..f12054b 100644 --- a/server/RdtClient.Data/Data/SettingData.cs +++ b/server/RdtClient.Data/Data/SettingData.cs @@ -5,12 +5,12 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using RdtClient.Data.Models.Data; +using RdtClient.Data.Models.Internal; namespace RdtClient.Data.Data { public class SettingData { - private static IList _settingCache; private static readonly SemaphoreSlim _settingCacheLock = new(1); private readonly DataContext _dataContext; @@ -20,20 +20,37 @@ namespace RdtClient.Data.Data _dataContext = dataContext; } + public static DbSettings Get { get; private set; } + + public async Task ResetCache() + { + var allSettings = await _dataContext.Settings.AsNoTracking().ToListAsync(); + + String GetString(String name) => allSettings.FirstOrDefault(m => m.SettingId == name)?.Value; + Int32 GetInt32(String name) => Int32.Parse(allSettings.FirstOrDefault(m => m.SettingId == name)?.Value ?? "0"); + + Get = new DbSettings + { + RealDebridApiKey = GetString("RealDebridApiKey"), + DownloadPath = GetString("DownloadPath"), + DownloadClient = GetString("DownloadClient"), + TempPath = GetString("TempPath"), + MappedPath = GetString("MappedPath"), + DownloadLimit = GetInt32("DownloadLimit"), + UnpackLimit = GetInt32("UnpackLimit"), + MinFileSize = GetInt32("MinFileSize"), + OnlyDownloadAvailableFiles = GetInt32("OnlyDownloadAvailableFiles"), + DownloadChunkCount = GetInt32("DownloadChunkCount"), + DownloadMaxSpeed = GetInt32("DownloadMaxSpeed"), + ProxyServer = GetString("ProxyServer"), + LogLevel = GetString("LogLevel"), + Categories = GetString("Categories") + }; + } + public async Task> GetAll() { - await _settingCacheLock.WaitAsync(); - - try - { - _settingCache ??= await _dataContext.Settings.AsNoTracking().ToListAsync(); - - return _settingCache; - } - finally - { - _settingCacheLock.Release(); - } + return await _dataContext.Settings.AsNoTracking().ToListAsync(); } public async Task Update(IList settings) @@ -56,7 +73,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - _settingCache = null; + await ResetCache(); } finally { @@ -81,7 +98,7 @@ namespace RdtClient.Data.Data await _dataContext.SaveChangesAsync(); - _settingCache = null; + await ResetCache(); } finally { diff --git a/server/RdtClient.Data/Data/TorrentData.cs b/server/RdtClient.Data/Data/TorrentData.cs index c411fd0..42aa5fb 100644 --- a/server/RdtClient.Data/Data/TorrentData.cs +++ b/server/RdtClient.Data/Data/TorrentData.cs @@ -202,7 +202,7 @@ namespace RdtClient.Data.Data await VoidCache(); } - public async Task VoidCache() + public static async Task VoidCache() { await _torrentCacheLock.WaitAsync(); diff --git a/server/RdtClient.Data/Models/Internal/DbSettings.cs b/server/RdtClient.Data/Models/Internal/DbSettings.cs new file mode 100644 index 0000000..d7e7d2e --- /dev/null +++ b/server/RdtClient.Data/Models/Internal/DbSettings.cs @@ -0,0 +1,22 @@ +using System; + +namespace RdtClient.Data.Models.Internal +{ + public class DbSettings + { + public String RealDebridApiKey { get; set; } + public String DownloadPath { get; set; } + public String DownloadClient { get; set; } + public String TempPath { get; set; } + public String MappedPath { get; set; } + public Int32 DownloadLimit { get; set; } + public Int32 UnpackLimit { get; set; } + public Int32 MinFileSize { get; set; } + public Int32 OnlyDownloadAvailableFiles { get; set; } + public Int32 DownloadChunkCount { get; set; } + public Int32 DownloadMaxSpeed { get; set; } + public String ProxyServer { get; set; } + public String LogLevel { get; set; } + public String Categories { get; set; } + } +} diff --git a/server/RdtClient.Service/DiConfig.cs b/server/RdtClient.Service/DiConfig.cs index 29a4d0e..751ea3d 100644 --- a/server/RdtClient.Service/DiConfig.cs +++ b/server/RdtClient.Service/DiConfig.cs @@ -14,6 +14,8 @@ namespace RdtClient.Service services.AddScoped(); services.AddScoped(); services.AddScoped(); + + services.AddHostedService(); } } } \ No newline at end of file diff --git a/server/RdtClient.Service/RdtClient.Service.csproj b/server/RdtClient.Service/RdtClient.Service.csproj index 4dde52b..a88574c 100644 --- a/server/RdtClient.Service/RdtClient.Service.csproj +++ b/server/RdtClient.Service/RdtClient.Service.csproj @@ -14,6 +14,7 @@ + diff --git a/server/RdtClient.Service/Services/DownloadClient.cs b/server/RdtClient.Service/Services/DownloadClient.cs index a0b32d3..350de64 100644 --- a/server/RdtClient.Service/Services/DownloadClient.cs +++ b/server/RdtClient.Service/Services/DownloadClient.cs @@ -1,10 +1,10 @@ using System; -using System.Collections.Generic; using System.IO; using System.Net; using System.Threading.Tasks; using Downloader; using RdtClient.Data.Models.Data; +using RdtClient.Data.Models.Internal; using RdtClient.Service.Helpers; namespace RdtClient.Service.Services @@ -34,7 +34,7 @@ namespace RdtClient.Service.Services public Int64 BytesTotal { get; private set; } public Int64 BytesDone { get; private set; } - public async Task Start(IList settings) + public async Task Start(DbSettings settings) { BytesDone = 0; BytesTotal = 0; @@ -56,11 +56,9 @@ namespace RdtClient.Service.Services var uri = new Uri(_download.Link); - await Task.Factory.StartNew(async delegate + await Task.Run(async delegate { - var downloadClientSetting = settings.GetString("DownloadClient"); - - switch (downloadClientSetting) + switch (settings.DownloadClient) { case "Simple": await DownloadSimple(uri, filePath); @@ -69,7 +67,7 @@ namespace RdtClient.Service.Services await DownloadMultiPart(filePath, settings); break; default: - throw new Exception($"Unknown download client {downloadClientSetting}"); + throw new Exception($"Unknown download client {settings.DownloadClient}"); } }); } @@ -126,30 +124,30 @@ namespace RdtClient.Service.Services } } - private async Task DownloadMultiPart(String filePath, IList settings) + private async Task DownloadMultiPart(String filePath, DbSettings settings) { try { - var settingTempPath = settings.GetString("TempPath"); + var settingTempPath = settings.TempPath; if (String.IsNullOrWhiteSpace(settingTempPath)) { settingTempPath = Path.GetTempPath(); } - var settingDownloadChunkCount = settings.GetNumber("DownloadChunkCount"); + var settingDownloadChunkCount = settings.DownloadChunkCount; if (settingDownloadChunkCount <= 0) { settingDownloadChunkCount = 1; } - var settingDownloadMaxSpeed = settings.GetNumber("DownloadMaxSpeed"); + var settingDownloadMaxSpeed = settings.DownloadMaxSpeed; if (settingDownloadMaxSpeed <= 0) { settingDownloadMaxSpeed = 0; } settingDownloadMaxSpeed = settingDownloadMaxSpeed * 1024 * 1024; - var settingProxyServer = settings.GetString("ProxyServer"); + var settingProxyServer = settings.ProxyServer; var downloadOpt = new DownloadConfiguration { diff --git a/server/RdtClient.Service/Services/QBittorrent.cs b/server/RdtClient.Service/Services/QBittorrent.cs index aae8bba..9e7e818 100644 --- a/server/RdtClient.Service/Services/QBittorrent.cs +++ b/server/RdtClient.Service/Services/QBittorrent.cs @@ -180,7 +180,7 @@ namespace RdtClient.Service.Services WebUiUsername = "" }; - var savePath = await AppDefaultSavePath(); + var savePath = AppDefaultSavePath(); preferences.SavePath = savePath; preferences.TempPath = $"{savePath}temp{Path.DirectorySeparatorChar}"; @@ -195,9 +195,9 @@ namespace RdtClient.Service.Services return preferences; } - public async Task AppDefaultSavePath() + public String AppDefaultSavePath() { - var downloadPath = await _settings.GetString("MappedPath"); + var downloadPath = Settings.Get.MappedPath; downloadPath = downloadPath.TrimEnd('\\') .TrimEnd('/'); @@ -208,7 +208,7 @@ namespace RdtClient.Service.Services public async Task> TorrentInfo() { - var savePath = await AppDefaultSavePath(); + var savePath = AppDefaultSavePath(); var results = new List(); @@ -337,7 +337,7 @@ namespace RdtClient.Service.Services public async Task TorrentProperties(String hash) { - var savePath = await AppDefaultSavePath(); + var savePath = AppDefaultSavePath(); var torrent = await _torrents.GetByHash(hash); @@ -388,11 +388,11 @@ namespace RdtClient.Service.Services SeedsTotal = 100, ShareRatio = 9999, TimeElapsed = (Int64) (DateTimeOffset.UtcNow - torrent.Added).TotalSeconds, - TotalDownloaded = bytesTotal, - TotalDownloadedSession = bytesTotal, + TotalDownloaded = bytesDone, + TotalDownloadedSession = bytesDone, TotalSize = bytesTotal, - TotalUploaded = bytesTotal, - TotalUploadedSession = bytesTotal, + TotalUploaded = bytesDone, + TotalUploadedSession = bytesDone, TotalWasted = 0, UpLimit = -1, UpSpeed = speed, @@ -416,22 +416,16 @@ namespace RdtClient.Service.Services public async Task TorrentsAddMagnet(String magnetLink, String category) { - var onlyDownloadAvailableFilesSetting = await _settings.GetNumber("OnlyDownloadAvailableFiles"); - var minFileSizeSetting = await _settings.GetNumber("MinFileSize"); + var downloadAction = Settings.Get.OnlyDownloadAvailableFiles == 1 ? TorrentDownloadAction.DownloadAvailableFiles : TorrentDownloadAction.DownloadAll; - var downloadAction = onlyDownloadAvailableFilesSetting == 1 ? TorrentDownloadAction.DownloadAvailableFiles : TorrentDownloadAction.DownloadAll; - - await _torrents.UploadMagnet(magnetLink, category, downloadAction, TorrentFinishedAction.None, minFileSizeSetting, null); + await _torrents.UploadMagnet(magnetLink, category, downloadAction, TorrentFinishedAction.None, Settings.Get.MinFileSize, null); } public async Task TorrentsAddFile(Byte[] fileBytes, String category) { - var onlyDownloadAvailableFilesSetting = await _settings.GetNumber("OnlyDownloadAvailableFiles"); - var minFileSizeSetting = await _settings.GetNumber("MinFileSize"); + var downloadAction = Settings.Get.OnlyDownloadAvailableFiles == 1 ? TorrentDownloadAction.DownloadAvailableFiles : TorrentDownloadAction.DownloadAll; - var downloadAction = onlyDownloadAvailableFilesSetting == 1 ? TorrentDownloadAction.DownloadAvailableFiles : TorrentDownloadAction.DownloadAll; - - await _torrents.UploadFile(fileBytes, category, downloadAction, TorrentFinishedAction.None, minFileSizeSetting, null); + await _torrents.UploadFile(fileBytes, category, downloadAction, TorrentFinishedAction.None, Settings.Get.MinFileSize, null); } public async Task TorrentsSetCategory(String hash, String category) @@ -448,9 +442,7 @@ namespace RdtClient.Service.Services .Distinct() .ToList(); - var categorySetting = await _settings.GetString("Categories"); - - var categories = categorySetting.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + var categories = Settings.Get.Categories.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); torrentsToGroup.AddRange(categories); @@ -471,7 +463,7 @@ namespace RdtClient.Service.Services public async Task CategoryCreate(String category) { - var categoriesSetting = await _settings.GetString("Categories"); + var categoriesSetting = Settings.Get.Categories; if (String.IsNullOrWhiteSpace(categoriesSetting)) { @@ -494,7 +486,7 @@ namespace RdtClient.Service.Services public async Task CategoryRemove(String category) { - var categoriesSetting = await _settings.GetString("Categories"); + var categoriesSetting = Settings.Get.Categories; if (String.IsNullOrWhiteSpace(categoriesSetting)) { diff --git a/server/RdtClient.Service/Services/Settings.cs b/server/RdtClient.Service/Services/Settings.cs index 8aadd3a..fc33774 100644 --- a/server/RdtClient.Service/Services/Settings.cs +++ b/server/RdtClient.Service/Services/Settings.cs @@ -2,11 +2,11 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; using System.Threading; using System.Threading.Tasks; using RdtClient.Data.Data; using RdtClient.Data.Models.Data; +using RdtClient.Data.Models.Internal; namespace RdtClient.Service.Services { @@ -19,6 +19,8 @@ namespace RdtClient.Service.Services _settingData = settingData; } + public static DbSettings Get => SettingData.Get; + public async Task> GetAll() { return await _settingData.GetAll(); @@ -34,32 +36,6 @@ namespace RdtClient.Service.Services await _settingData.UpdateString(key, value); } - public async Task GetString(String key) - { - var settings = await GetAll(); - var setting = settings.FirstOrDefault(m => m.SettingId == key); - - if (setting == null) - { - throw new Exception($"Setting with key {key} not found"); - } - - return setting.Value; - } - - public async Task GetNumber(String key) - { - var settings = await GetAll(); - var setting = settings.FirstOrDefault(m => m.SettingId == key); - - if (setting == null) - { - throw new Exception($"Setting with key {key} not found"); - } - - return Int32.Parse(setting.Value); - } - public async Task TestPath(String path) { if (String.IsNullOrWhiteSpace(path)) @@ -82,9 +58,7 @@ namespace RdtClient.Service.Services public async Task TestDownloadSpeed(CancellationToken cancellationToken) { - var downloadPath = await GetString("DownloadPath"); - - var settings = await GetAll(); + var downloadPath = Get.DownloadPath; var testFilePath = Path.Combine(downloadPath, "testDefault.rar"); @@ -104,7 +78,7 @@ namespace RdtClient.Service.Services var downloadClient = new DownloadClient(download, download.Torrent, downloadPath); - await downloadClient.Start(settings); + await downloadClient.Start(Get); while (!downloadClient.Finished) { @@ -136,7 +110,7 @@ namespace RdtClient.Service.Services public async Task TestWriteSpeed() { - var downloadPath = await GetString("DownloadPath"); + var downloadPath = Get.DownloadPath; var testFilePath = Path.Combine(downloadPath, "test.tmp"); @@ -161,7 +135,7 @@ namespace RdtClient.Service.Services { rnd.NextBytes(buffer); - fileStream.Write(buffer, 0, buffer.Length); + await fileStream.WriteAsync(buffer.AsMemory(0, buffer.Length)); } watch.Stop(); @@ -182,7 +156,7 @@ namespace RdtClient.Service.Services { try { - var tempPath = GetString("TempPath").Result; + var tempPath = Get.TempPath; if (!String.IsNullOrWhiteSpace(tempPath)) { @@ -199,5 +173,10 @@ namespace RdtClient.Service.Services // ignored } } + + public async Task ResetCache() + { + await _settingData.ResetCache(); + } } } diff --git a/server/RdtClient.Service/Services/SimpleDownloader.cs b/server/RdtClient.Service/Services/SimpleDownloader.cs index 6d062c3..50f783a 100644 --- a/server/RdtClient.Service/Services/SimpleDownloader.cs +++ b/server/RdtClient.Service/Services/SimpleDownloader.cs @@ -64,7 +64,7 @@ namespace RdtClient.Service.Services if (read > 0) { - fileStream.Write(buffer, 0, read); + await fileStream.WriteAsync(buffer.AsMemory(0, read)); BytesDone = fileStream.Length; BytesTotal = responseLength; diff --git a/server/RdtClient.Service/Services/Startup.cs b/server/RdtClient.Service/Services/Startup.cs new file mode 100644 index 0000000..3d5fafa --- /dev/null +++ b/server/RdtClient.Service/Services/Startup.cs @@ -0,0 +1,32 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace RdtClient.Service.Services +{ + public class Startup : IHostedService + { + private readonly IServiceProvider _serviceProvider; + + public Startup(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + using var scope = _serviceProvider.CreateScope(); + + var settings = scope.ServiceProvider.GetRequiredService(); + + await settings.ResetCache(); + + settings.Clean(); + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + } + +} diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index 4b2c934..376e26a 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Threading.Tasks; using System.Web; using RdtClient.Data.Enums; -using RdtClient.Service.Helpers; using Serilog; namespace RdtClient.Service.Services @@ -21,13 +20,11 @@ namespace RdtClient.Service.Services public static readonly ConcurrentDictionary ActiveUnpackClients = new(); private readonly Downloads _downloads; private readonly RemoteService _remoteService; - - private readonly Settings _settings; + private readonly Torrents _torrents; - public TorrentRunner(Settings settings, Torrents torrents, Downloads downloads, RemoteService remoteService) + public TorrentRunner(Torrents torrents, Downloads downloads, RemoteService remoteService) { - _settings = settings; _torrents = torrents; _downloads = downloads; _remoteService = remoteService; @@ -65,29 +62,26 @@ namespace RdtClient.Service.Services sw.Start(); Log.Debug("TorrentRunner Tick Start"); - - var settings = await _settings.GetAll(); - - var settingApiKey = settings.GetString("RealDebridApiKey"); - if (String.IsNullOrWhiteSpace(settingApiKey)) + + if (String.IsNullOrWhiteSpace(Settings.Get.RealDebridApiKey)) { Log.Debug($"No RealDebridApiKey set!"); return; } - var settingDownloadLimit = settings.GetNumber("DownloadLimit"); + var settingDownloadLimit = Settings.Get.DownloadLimit; if (settingDownloadLimit < 1) { settingDownloadLimit = 1; } - var settingUnpackLimit = settings.GetNumber("UnpackLimit"); + var settingUnpackLimit = Settings.Get.UnpackLimit; if (settingUnpackLimit < 1) { settingUnpackLimit = 1; } - var settingDownloadPath = settings.GetString("DownloadPath"); + var settingDownloadPath = Settings.Get.DownloadPath; if (String.IsNullOrWhiteSpace(settingDownloadPath)) { return; @@ -253,7 +247,7 @@ namespace RdtClient.Service.Services { Log.Debug($"Added download {download.DownloadId} to active downloads"); - await downloadClient.Start(settings); + await downloadClient.Start(Settings.Get); Log.Debug($"Download {download.DownloadId} started"); } diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index a4ca411..632bd63 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using MonoTorrent; @@ -21,19 +22,32 @@ namespace RdtClient.Service.Services private static readonly SemaphoreSlim RealDebridUpdateLock = new(1, 1); - private static RdNetClient _rdtNetClient; - private readonly Downloads _downloads; - private readonly Settings _settings; + private readonly IHttpClientFactory _httpClientFactory; private readonly TorrentData _torrentData; - public Torrents(TorrentData torrentData, Settings settings, Downloads downloads) + public Torrents(IHttpClientFactory httpClientFactory, TorrentData torrentData, Downloads downloads) { + _httpClientFactory = httpClientFactory; _torrentData = torrentData; - _settings = settings; _downloads = downloads; } + private RdNetClient GetRdNetClient() + { + var apiKey = Settings.Get.RealDebridApiKey; + + if (String.IsNullOrWhiteSpace(apiKey)) + { + throw new Exception("Real-Debrid API Key not set in the settings"); + } + + var rdtNetClient = new RdNetClient(null, _httpClientFactory.CreateClient()); + rdtNetClient.UseApiAuthentication(apiKey); + + return rdtNetClient; + } + public async Task> Get() { var torrents = await _torrentData.Get(); @@ -207,7 +221,7 @@ namespace RdtClient.Service.Services if (deleteLocalFiles) { - var downloadPath = await DownloadPath(torrent); + var downloadPath = DownloadPath(torrent); downloadPath = Path.Combine(downloadPath, torrent.RdName); if (Directory.Exists(downloadPath)) @@ -244,7 +258,7 @@ namespace RdtClient.Service.Services var torrent = await GetById(torrentId); var download = await _downloads.GetById(downloadId); - var downloadPath = await DownloadPath(torrent); + var downloadPath = DownloadPath(torrent); var filePath = DownloadHelper.GetDownloadPath(downloadPath, torrent, download); @@ -311,11 +325,6 @@ namespace RdtClient.Service.Services await _downloads.UpdateError(downloadId, null); } - public void Reset() - { - _rdtNetClient = null; - } - public async Task GetProfile() { var user = await GetRdNetClient().User.GetAsync(); @@ -472,25 +481,6 @@ namespace RdtClient.Service.Services await _torrentData.UpdateFilesSelected(torrentId, datetime); } - private RdNetClient GetRdNetClient() - { - if (_rdtNetClient == null) - { - var apiKey = _settings.GetString("RealDebridApiKey") - .Result; - - if (String.IsNullOrWhiteSpace(apiKey)) - { - throw new Exception("Real-Debrid API Key not set in the settings"); - } - - _rdtNetClient = new RdNetClient(); - _rdtNetClient.UseApiAuthentication(apiKey); - } - - return _rdtNetClient; - } - public async Task GetById(Guid torrentId) { var torrent = await _torrentData.GetById(torrentId); @@ -558,9 +548,9 @@ namespace RdtClient.Service.Services } } - private async Task DownloadPath(Torrent torrent) + private String DownloadPath(Torrent torrent) { - var settingDownloadPath = await _settings.GetString("DownloadPath"); + var settingDownloadPath = Settings.Get.DownloadPath; if (!String.IsNullOrWhiteSpace(torrent.Category)) { diff --git a/server/RdtClient.Service/Services/UnpackClient.cs b/server/RdtClient.Service/Services/UnpackClient.cs index d2e8be7..5a2e22a 100644 --- a/server/RdtClient.Service/Services/UnpackClient.cs +++ b/server/RdtClient.Service/Services/UnpackClient.cs @@ -50,7 +50,7 @@ namespace RdtClient.Service.Services throw new Exception("Invalid download path"); } - await Task.Factory.StartNew(async delegate + await Task.Run(async delegate { if (!_cancelled) { diff --git a/server/RdtClient.Web/Controllers/QBittorrentController.cs b/server/RdtClient.Web/Controllers/QBittorrentController.cs index 2d47927..fb7f421 100644 --- a/server/RdtClient.Web/Controllers/QBittorrentController.cs +++ b/server/RdtClient.Web/Controllers/QBittorrentController.cs @@ -125,9 +125,9 @@ namespace RdtClient.Web.Controllers [Route("app/defaultSavePath")] [HttpGet] [HttpPost] - public async Task> AppDefaultSavePath() + public ActionResult AppDefaultSavePath() { - var result = await _qBittorrent.AppDefaultSavePath(); + var result = _qBittorrent.AppDefaultSavePath(); return Ok(result); } diff --git a/server/RdtClient.Web/Controllers/SettingsController.cs b/server/RdtClient.Web/Controllers/SettingsController.cs index 7d6904c..3f1ff79 100644 --- a/server/RdtClient.Web/Controllers/SettingsController.cs +++ b/server/RdtClient.Web/Controllers/SettingsController.cs @@ -37,12 +37,8 @@ namespace RdtClient.Web.Controllers public async Task Update([FromBody] SettingsControllerUpdateRequest request) { await _settings.Update(request.Settings); - - _torrents.Reset(); - - var logLevelSetting = await _settings.GetString("LogLevel"); - - if (!Enum.TryParse(logLevelSetting, out var logLevel)) + + if (!Enum.TryParse(Settings.Get.LogLevel, out var logLevel)) { logLevel = LogEventLevel.Information; } diff --git a/server/RdtClient.Web/Startup.cs b/server/RdtClient.Web/Startup.cs index 6242eda..fc78227 100644 --- a/server/RdtClient.Web/Startup.cs +++ b/server/RdtClient.Web/Startup.cs @@ -91,7 +91,7 @@ namespace RdtClient.Web Service.DiConfig.Config(services); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger, Settings settings) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger) { if (env.IsDevelopment()) { @@ -133,8 +133,6 @@ namespace RdtClient.Web spa.Options.DefaultPage = "/index.html"; }); }); - - settings.Clean(); } } } \ No newline at end of file