99 lines
3.1 KiB
C#
99 lines
3.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
|
|
public class TorrentController : Controller
|
|
{
|
|
private readonly ITorrentService _torrentService;
|
|
private readonly ILogger<TorrentController> _logger;
|
|
|
|
public TorrentController(ITorrentService torrentService, ILogger<TorrentController> logger)
|
|
{
|
|
_torrentService = torrentService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Index()
|
|
{
|
|
return View(new TorrentUploadViewModel());
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> 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<IActionResult> 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" });
|
|
}
|
|
}
|
|
} |