using System.Diagnostics; using System.Reflection; using Aria2NET; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using RdtClient.Data.Data; using RdtClient.Data.Models.Data; using RdtClient.Data.Models.Internal; using RdtClient.Service.Helpers; using RdtClient.Service.Services; using RdtClient.Service.Services.Downloaders; using DownloadClient = RdtClient.Data.Enums.DownloadClient; namespace RdtClient.Web.Controllers; [Authorize(Policy = "AuthSetting")] [Route("Api/Settings")] public class SettingsController(ISettings settings, Torrents torrents) : Controller { [HttpGet] [Route("")] public ActionResult Get() { var result = settings.GetAll(); return Ok(result); } [HttpPut] [Route("")] public async Task Update([FromBody] IList? settings1) { if (settings1 == null) { return BadRequest(); } await settings.Update(settings1); return Ok(); } [HttpGet] [Route("Profile")] public async Task> Profile() { var profile = await torrents.GetProfile(); return Ok(profile); } [HttpGet] [Route("Version")] public ActionResult Version() { var version = Assembly.GetExecutingAssembly().GetName().Version!; return Ok(new { Version = version }); } [HttpPost] [Route("TestPath")] public async Task TestPath([FromBody] SettingsControllerTestPathRequest? request) { if (request == null) { return BadRequest(); } if (String.IsNullOrEmpty(request.Path)) { return BadRequest("Invalid path"); } var path = request.Path.TrimEnd('/').TrimEnd('\\'); if (!Directory.Exists(path)) { throw new($"Path {path} does not exist"); } var testFile = $"{path}/test.txt"; await System.IO.File.WriteAllTextAsync(testFile, "RealDebridClient Test File, you can remove this file."); await FileHelper.Delete(testFile); return Ok(); } [HttpGet] [Route("TestDownloadSpeed")] public async Task TestDownloadSpeed(CancellationToken cancellationToken) { var downloadPath = settings.Current.DownloadClient.DownloadPath; var testFilePath = Path.Combine(downloadPath, "testDefault.rar"); await FileHelper.Delete(testFilePath); var download = new Download { Link = "https://34.download.real-debrid.com/speedtest/testDefault.rar", Torrent = new() { DownloadClient = settings.Current.DownloadClient.Client == DownloadClient.Symlink ? DownloadClient.Bezzad : settings.Current.DownloadClient.Client, RdName = "testDefault.rar" } }; var downloadClient = new Service.Services.DownloadClient(download, download.Torrent, downloadPath, null); await downloadClient.Start(); var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) }; while (!downloadClient.Finished) { await Task.Delay(1000, CancellationToken.None); if (cancellationToken.IsCancellationRequested) { await downloadClient.Cancel(); } if (downloadClient.Downloader is Aria2cDownloader aria2Downloader) { var aria2NetClient = new Aria2NetClient(settings.Current.DownloadClient.Aria2cUrl, settings.Current.DownloadClient.Aria2cSecret, httpClient, 1); var allDownloads = await aria2NetClient.TellAllAsync(cancellationToken); await aria2Downloader.Update(allDownloads); } if (downloadClient.BytesDone > 1024 * 1024 * 50) { await downloadClient.Cancel(); break; } } await FileHelper.Delete(testFilePath); return Ok(downloadClient.Speed); } [HttpGet] [Route("TestWriteSpeed")] public async Task TestWriteSpeed() { var downloadPath = settings.Current.DownloadClient.DownloadPath; var testFilePath = Path.Combine(downloadPath, "test.tmp"); await FileHelper.Delete(testFilePath); const Int32 testFileSize = 64 * 1024 * 1024; var watch = new Stopwatch(); watch.Start(); var rnd = new Random(); await using var fileStream = new FileStream(testFilePath, FileMode.Create, FileAccess.Write, FileShare.Write); var buffer = new Byte[64 * 1024]; while (fileStream.Length < testFileSize) { rnd.NextBytes(buffer); await fileStream.WriteAsync(buffer.AsMemory(0, buffer.Length)); } watch.Stop(); var writeSpeed = fileStream.Length / watch.Elapsed.TotalSeconds; fileStream.Close(); await FileHelper.Delete(testFilePath); return Ok(writeSpeed); } [HttpPost] [Route("TestAria2cConnection")] public async Task> TestAria2cConnection([FromBody] SettingsControllerTestAria2cConnectionRequest? request) { if (request == null) { return BadRequest(); } if (String.IsNullOrEmpty(request.Url)) { return BadRequest("Invalid Url"); } var client = new Aria2NetClient(request.Url, request.Secret); var version = await client.GetVersionAsync(); return Ok(version); } } public class SettingsControllerTestPathRequest { public String? Path { get; set; } } public class SettingsControllerTestAria2cConnectionRequest { public String? Url { get; set; } public String? Secret { get; set; } }