From 274f98baa4b24c2c264d3fed6f3698fa58266396 Mon Sep 17 00:00:00 2001 From: elias Date: Sun, 3 Aug 2025 21:47:02 +0200 Subject: [PATCH] Test --- Aberwyn/Aberwyn.csproj | 1 + Aberwyn/Controllers/TorrentController.cs | 99 ++++++++++ Aberwyn/Models/TorrentInfo.cs | 29 +++ Aberwyn/Program.cs | 2 + Aberwyn/Views/Torrent/Index.cshtml | 225 +++++++++++++++++++++++ 5 files changed, 356 insertions(+) create mode 100644 Aberwyn/Controllers/TorrentController.cs create mode 100644 Aberwyn/Models/TorrentInfo.cs create mode 100644 Aberwyn/Views/Torrent/Index.cshtml diff --git a/Aberwyn/Aberwyn.csproj b/Aberwyn/Aberwyn.csproj index 96925a8..cf237ac 100644 --- a/Aberwyn/Aberwyn.csproj +++ b/Aberwyn/Aberwyn.csproj @@ -18,6 +18,7 @@ + diff --git a/Aberwyn/Controllers/TorrentController.cs b/Aberwyn/Controllers/TorrentController.cs new file mode 100644 index 0000000..9cabed5 --- /dev/null +++ b/Aberwyn/Controllers/TorrentController.cs @@ -0,0 +1,99 @@ +using Microsoft.AspNetCore.Mvc; + +public class TorrentController : Controller +{ + private readonly ITorrentService _torrentService; + private readonly ILogger _logger; + + public TorrentController(ITorrentService torrentService, ILogger logger) + { + _torrentService = torrentService; + _logger = logger; + } + + [HttpGet] + public IActionResult Index() + { + return View(new TorrentUploadViewModel()); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Upload(TorrentUploadViewModel model) + { + if (model.TorrentFile == null || model.TorrentFile.Length == 0) + { + ModelState.AddModelError("TorrentFile", "Vänligen välj en torrent-fil"); + return View("Index", model); + } + + if (!model.TorrentFile.FileName.EndsWith(".torrent", StringComparison.OrdinalIgnoreCase)) + { + ModelState.AddModelError("TorrentFile", "Endast .torrent filer är tillåtna"); + return View("Index", model); + } + + if (model.TorrentFile.Length > 10 * 1024 * 1024) // 10MB limit + { + ModelState.AddModelError("TorrentFile", "Filen är för stor (max 10MB)"); + return View("Index", model); + } + + try + { + // Parsa torrent-filen + var torrentInfo = await _torrentService.ParseTorrentAsync(model.TorrentFile); + + if (!string.IsNullOrEmpty(torrentInfo.ErrorMessage)) + { + ModelState.AddModelError("", torrentInfo.ErrorMessage); + return View("Index", model); + } + + // Försök hämta tracker-statistik + torrentInfo = await _torrentService.FetchTrackerStatsAsync(torrentInfo); + + model.TorrentInfo = torrentInfo; + model.ShowResults = true; + + return View("Index", model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Fel vid uppladdning av torrent"); + ModelState.AddModelError("", "Ett oväntat fel inträffade"); + return View("Index", model); + } + } + + [HttpPost] + [ValidateAntiForgeryToken] + public async Task RefreshStats(string infoHash, string scrapeUrl) + { + try + { + var torrentInfo = new TorrentInfo + { + InfoHash = infoHash, + ScrapeUrl = scrapeUrl, + InfoHashBytes = Convert.FromHexString(infoHash.Replace("%", "")) + }; + + var updatedInfo = await _torrentService.FetchTrackerStatsAsync(torrentInfo); + + return Json(new + { + success = updatedInfo.HasTrackerData, + seeders = updatedInfo.Seeders, + leechers = updatedInfo.Leechers, + completed = updatedInfo.Completed, + error = updatedInfo.ErrorMessage + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Fel vid uppdatering av tracker-stats"); + return Json(new { success = false, error = "Fel vid uppdatering" }); + } + } +} \ No newline at end of file diff --git a/Aberwyn/Models/TorrentInfo.cs b/Aberwyn/Models/TorrentInfo.cs new file mode 100644 index 0000000..3856d60 --- /dev/null +++ b/Aberwyn/Models/TorrentInfo.cs @@ -0,0 +1,29 @@ +public class TorrentInfo +{ + public string FileName { get; set; } + public string AnnounceUrl { get; set; } + public string ScrapeUrl { get; set; } + public string InfoHash { get; set; } + public byte[] InfoHashBytes { get; set; } + public long Size { get; set; } + public int Seeders { get; set; } = 0; + public int Leechers { get; set; } = 0; + public int Completed { get; set; } = 0; + public bool HasTrackerData { get; set; } = false; + public string ErrorMessage { get; set; } +} +public class TrackerInfo +{ + public string Name { get; set; } + public bool SupportsScraping { get; set; } + public bool RequiresAuth { get; set; } + public bool IsPrivate { get; set; } + public string Notes { get; set; } +} + +public class TorrentUploadViewModel +{ + public IFormFile TorrentFile { get; set; } + public TorrentInfo TorrentInfo { get; set; } + public bool ShowResults { get; set; } = false; +} \ No newline at end of file diff --git a/Aberwyn/Program.cs b/Aberwyn/Program.cs index c95c694..55e657f 100644 --- a/Aberwyn/Program.cs +++ b/Aberwyn/Program.cs @@ -64,6 +64,8 @@ catch (Exception ex) } +builder.Services.AddHttpClient(); +builder.Services.AddScoped(); // Add services to the container builder.Services.AddControllersWithViews() diff --git a/Aberwyn/Views/Torrent/Index.cshtml b/Aberwyn/Views/Torrent/Index.cshtml new file mode 100644 index 0000000..288e2ad --- /dev/null +++ b/Aberwyn/Views/Torrent/Index.cshtml @@ -0,0 +1,225 @@ +@model TorrentUploadViewModel +@{ + ViewData["Title"] = "Torrent Analyzer"; +} + +
+
+
+
+
+

+ Torrent Analyzer +

+

Ladda upp en torrent-fil för att se seeders/leechers

+
+
+ @if (!ViewData.ModelState.IsValid) + { +
+ @foreach (var error in ViewData.ModelState.Values.SelectMany(v => v.Errors)) + { +
@error.ErrorMessage
+ } +
+ } + +
+ @Html.AntiForgeryToken() +
+ + +
Endast .torrent filer, max 10MB
+
+ +
+ + @if (Model.ShowResults && Model.TorrentInfo != null) + { +
+
+

+ Torrent Information +

+ +
+
Filnamn:
+
@Model.TorrentInfo.FileName
+
+ +
+
Storlek:
+
@FormatFileSize(Model.TorrentInfo.Size)
+
+ +
+
Announce URL:
+
+ @Model.TorrentInfo.AnnounceUrl +
+
+ +
+
Info Hash:
+
+ @Model.TorrentInfo.InfoHash +
+
+ + @if (!string.IsNullOrEmpty(Model.TorrentInfo.ErrorMessage)) + { +
+ + Tracker-info: @Model.TorrentInfo.ErrorMessage +
+ } + else if (Model.TorrentInfo.HasTrackerData) + { +
+
+
+ Tracker Statistik +
+ +
+
+
+
+
+

@Model.TorrentInfo.Seeders

+ Seeders +
+
+
+
+

@Model.TorrentInfo.Leechers

+ Leechers +
+
+
+
+

@Model.TorrentInfo.Completed

+ Completed +
+
+
+
+
+ } + else + { +
+ + Tracker-statistik kunde inte hämtas. Detta kan bero på att trackern inte stöder scraping eller kräver autentisering. +
+ } +
+ } +
+
+
+
+
+ +@functions { + string FormatFileSize(long bytes) + { + if (bytes == 0) return "Okänd storlek"; + + string[] sizes = { "B", "KB", "MB", "GB", "TB" }; + int order = 0; + double size = bytes; + + while (size >= 1024 && order < sizes.Length - 1) + { + order++; + size = size / 1024; + } + + return $"{size:0.##} {sizes[order]}"; + } +} + + + + \ No newline at end of file