2024-09-17 10:46:38 +02:00
|
|
|
using Logging;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace BittorrentDriver.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class TorrentController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ILog log = new ConsoleLog();
|
|
|
|
|
private readonly TorrentTracker tracker = new TorrentTracker();
|
|
|
|
|
private readonly Transmission transmission;
|
|
|
|
|
|
|
|
|
|
public TorrentController()
|
|
|
|
|
{
|
|
|
|
|
transmission = new Transmission(log);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut("tracker")]
|
|
|
|
|
public string StartTracker([FromBody] int port)
|
|
|
|
|
{
|
|
|
|
|
Log("Starting tracker...");
|
|
|
|
|
return tracker.Start(port);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 13:44:06 +02:00
|
|
|
[HttpPut("daemon")]
|
|
|
|
|
public string StartDaemon([FromBody] int peerPort)
|
|
|
|
|
{
|
|
|
|
|
Log("Starting daemon...");
|
|
|
|
|
return transmission.StartDaemon(peerPort);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 10:46:38 +02:00
|
|
|
[HttpPost("create")]
|
|
|
|
|
public string CreateTorrent([FromBody] CreateTorrentInput input)
|
|
|
|
|
{
|
2024-09-17 13:44:06 +02:00
|
|
|
Log("Creating torrent file...");
|
2024-09-17 10:46:38 +02:00
|
|
|
return transmission.CreateNew(input.Size, input.TrackerUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("download")]
|
|
|
|
|
public string DownloadTorrent([FromBody] string torrentBase64)
|
|
|
|
|
{
|
2024-09-17 13:44:06 +02:00
|
|
|
Log("Downloading torrent...");
|
2024-09-17 10:46:38 +02:00
|
|
|
return transmission.Download(torrentBase64);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Log(string v)
|
|
|
|
|
{
|
|
|
|
|
log.Log(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CreateTorrentInput
|
|
|
|
|
{
|
|
|
|
|
public int Size { get; set; }
|
|
|
|
|
public string TrackerUrl { get; set; } = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|