Fixed category setting when adding a torrent from Sonarr or Radarr.

This commit is contained in:
Roger Far 2021-01-13 19:15:00 -07:00
parent 5245a9448f
commit 3598b1f218
7 changed files with 76 additions and 26 deletions

View file

@ -11,7 +11,7 @@ namespace RdtClient.Data.Data
Task<IList<Torrent>> Get(); Task<IList<Torrent>> Get();
Task<Torrent> GetById(Guid torrentId); Task<Torrent> GetById(Guid torrentId);
Task<Torrent> GetByHash(String hash); Task<Torrent> GetByHash(String hash);
Task<Torrent> Add(String realDebridId, String hash, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task<Torrent> Add(String realDebridId, String hash, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete);
Task UpdateRdData(Torrent torrent); Task UpdateRdData(Torrent torrent);
Task UpdateCategory(Guid torrentId, String category); Task UpdateCategory(Guid torrentId, String category);
Task UpdateComplete(Guid torrentId, DateTimeOffset datetime); Task UpdateComplete(Guid torrentId, DateTimeOffset datetime);
@ -77,7 +77,7 @@ namespace RdtClient.Data.Data
return dbTorrent; return dbTorrent;
} }
public async Task<Torrent> Add(String realDebridId, String hash, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) public async Task<Torrent> Add(String realDebridId, String hash, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete)
{ {
var torrent = new Torrent var torrent = new Torrent
{ {
@ -85,6 +85,7 @@ namespace RdtClient.Data.Data
Added = DateTimeOffset.UtcNow, Added = DateTimeOffset.UtcNow,
RdId = realDebridId, RdId = realDebridId,
Hash = hash.ToLower(), Hash = hash.ToLower(),
Category = category,
AutoDownload = autoDownload, AutoDownload = autoDownload,
AutoUnpack = autoUnpack, AutoUnpack = autoUnpack,
AutoDelete = autoDelete AutoDelete = autoDelete

View file

@ -20,6 +20,8 @@ namespace RdtClient.Service.Services
private readonly Download _download; private readonly Download _download;
private readonly String _destinationPath; private readonly String _destinationPath;
private readonly Torrent _torrent; private readonly Torrent _torrent;
private Boolean _cancelled = false;
private Int64 _bytesLastUpdate; private Int64 _bytesLastUpdate;
private DateTime _nextUpdate; private DateTime _nextUpdate;
@ -64,7 +66,10 @@ namespace RdtClient.Service.Services
await Task.Factory.StartNew(async delegate await Task.Factory.StartNew(async delegate
{ {
await Download(uri, filePath); if (!_cancelled)
{
await Download(uri, filePath);
}
}); });
} }
catch (Exception ex) catch (Exception ex)
@ -74,6 +79,11 @@ namespace RdtClient.Service.Services
} }
} }
public void Cancel()
{
_cancelled = true;
}
private async Task Download(Uri uri, String filePath) private async Task Download(Uri uri, String filePath)
{ {
try try
@ -94,7 +104,7 @@ namespace RdtClient.Service.Services
var timeout = DateTimeOffset.UtcNow.AddHours(1); var timeout = DateTimeOffset.UtcNow.AddHours(1);
while (timeout > DateTimeOffset.UtcNow) while (timeout > DateTimeOffset.UtcNow && !_cancelled)
{ {
try try
{ {
@ -111,7 +121,7 @@ namespace RdtClient.Service.Services
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write); await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write);
var buffer = new Byte[4096]; var buffer = new Byte[4096];
while (fileStream.Length < response.ContentLength) while (fileStream.Length < response.ContentLength && !_cancelled)
{ {
var read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length)); var read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length));

View file

@ -18,8 +18,8 @@ namespace RdtClient.Service.Services
Task<IList<TorrentFileItem>> TorrentFileContents(String hash); Task<IList<TorrentFileItem>> TorrentFileContents(String hash);
Task<TorrentProperties> TorrentProperties(String hash); Task<TorrentProperties> TorrentProperties(String hash);
Task TorrentsDelete(String hash, Boolean deleteFiles); Task TorrentsDelete(String hash, Boolean deleteFiles);
Task TorrentsAdd(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task TorrentsAddMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete);
Task TorrentsAddFile(Byte[] fileBytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task TorrentsAddFile(Byte[] fileBytes, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete);
Task TorrentsSetCategory(String hash, String category); Task TorrentsSetCategory(String hash, String category);
Task<IDictionary<String, TorrentCategory>> TorrentsCategories(); Task<IDictionary<String, TorrentCategory>> TorrentsCategories();
} }
@ -424,14 +424,14 @@ namespace RdtClient.Service.Services
await _torrents.Delete(torrent.TorrentId, true, true, deleteFiles); await _torrents.Delete(torrent.TorrentId, true, true, deleteFiles);
} }
public async Task TorrentsAdd(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) public async Task TorrentsAddMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete)
{ {
await _torrents.UploadMagnet(magnetLink, autoDownload, autoUnpack, autoDelete); await _torrents.UploadMagnet(magnetLink, category, autoDownload, autoUnpack, autoDelete);
} }
public async Task TorrentsAddFile(Byte[] fileBytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) public async Task TorrentsAddFile(Byte[] fileBytes, String category,Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete)
{ {
await _torrents.UploadFile(fileBytes, autoDownload, autoUnpack, autoDelete); await _torrents.UploadFile(fileBytes, category, autoDownload, autoUnpack, autoDelete);
} }
public async Task TorrentsSetCategory(String hash, String category) public async Task TorrentsSetCategory(String hash, String category)

View file

@ -20,8 +20,8 @@ namespace RdtClient.Service.Services
Task<Torrent> GetById(Guid torrentId); Task<Torrent> GetById(Guid torrentId);
Task<Torrent> GetByHash(String hash); Task<Torrent> GetByHash(String hash);
Task UpdateCategory(String hash, String category); Task UpdateCategory(String hash, String category);
Task UploadMagnet(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task UploadMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete);
Task UploadFile(Byte[] bytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task UploadFile(Byte[] bytes, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete);
Task SelectFiles(String torrentId, IList<String> fileIds); Task SelectFiles(String torrentId, IList<String> fileIds);
Task Delete(Guid torrentId, Boolean deleteData, Boolean deleteRdTorrent, Boolean deleteLocalFiles); Task Delete(Guid torrentId, Boolean deleteData, Boolean deleteRdTorrent, Boolean deleteLocalFiles);
Task Unrestrict(Guid torrentId); Task Unrestrict(Guid torrentId);
@ -134,22 +134,22 @@ namespace RdtClient.Service.Services
await _torrentData.UpdateCategory(torrent.TorrentId, category); await _torrentData.UpdateCategory(torrent.TorrentId, category);
} }
public async Task UploadMagnet(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) public async Task UploadMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete)
{ {
var magnet = MagnetLink.Parse(magnetLink); var magnet = MagnetLink.Parse(magnetLink);
var rdTorrent = await GetRdNetClient().AddTorrentMagnetAsync(magnetLink); var rdTorrent = await GetRdNetClient().AddTorrentMagnetAsync(magnetLink);
await Add(rdTorrent.Id, magnet.InfoHash.ToHex(), autoDownload, autoUnpack, autoDelete); await Add(rdTorrent.Id, magnet.InfoHash.ToHex(), category, autoDownload, autoUnpack, autoDelete);
} }
public async Task UploadFile(Byte[] bytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) public async Task UploadFile(Byte[] bytes, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete)
{ {
var torrent = await MonoTorrent.Torrent.LoadAsync(bytes); var torrent = await MonoTorrent.Torrent.LoadAsync(bytes);
var rdTorrent = await GetRdNetClient().AddTorrentFileAsync(bytes); var rdTorrent = await GetRdNetClient().AddTorrentFileAsync(bytes);
await Add(rdTorrent.Id, torrent.InfoHash.ToHex(), autoDownload, autoUnpack, autoDelete); await Add(rdTorrent.Id, torrent.InfoHash.ToHex(), category, autoDownload, autoUnpack, autoDelete);
} }
public async Task SelectFiles(String torrentId, IList<String> fileIds) public async Task SelectFiles(String torrentId, IList<String> fileIds)
@ -163,6 +163,19 @@ namespace RdtClient.Service.Services
if (torrent != null) if (torrent != null)
{ {
foreach (var download in torrent.Downloads)
{
if (TorrentRunner.ActiveUnpackClients.TryRemove(download.DownloadId, out var activeUnpack))
{
activeUnpack.Cancel();
}
if (TorrentRunner.ActiveDownloadClients.TryRemove(download.DownloadId, out var activeDownload))
{
activeDownload.Cancel();
}
}
if (deleteLocalFiles) if (deleteLocalFiles)
{ {
var downloadPath = await DownloadPath(torrent); var downloadPath = await DownloadPath(torrent);
@ -228,6 +241,11 @@ namespace RdtClient.Service.Services
{ {
return; return;
} }
if (TorrentRunner.ActiveDownloadClients.ContainsKey(downloadId))
{
return;
}
download.DownloadStarted = DateTimeOffset.UtcNow; download.DownloadStarted = DateTimeOffset.UtcNow;
await _downloads.UpdateDownloadStarted(download.DownloadId, download.DownloadStarted); await _downloads.UpdateDownloadStarted(download.DownloadId, download.DownloadStarted);
@ -276,6 +294,11 @@ namespace RdtClient.Service.Services
return; return;
} }
if (TorrentRunner.ActiveUnpackClients.ContainsKey(downloadId))
{
return;
}
download.UnpackingStarted = DateTimeOffset.UtcNow; download.UnpackingStarted = DateTimeOffset.UtcNow;
await _downloads.UpdateUnpackingStarted(download.DownloadId, download.UnpackingStarted); await _downloads.UpdateUnpackingStarted(download.DownloadId, download.UnpackingStarted);
@ -311,7 +334,7 @@ namespace RdtClient.Service.Services
return profile; return profile;
} }
private async Task Add(String rdTorrentId, String infoHash, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) private async Task Add(String rdTorrentId, String infoHash, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete)
{ {
var w = await SemaphoreSlim.WaitAsync(60000); var w = await SemaphoreSlim.WaitAsync(60000);
if (!w) if (!w)
@ -328,7 +351,7 @@ namespace RdtClient.Service.Services
return; return;
} }
var newTorrent = await _torrentData.Add(rdTorrentId, infoHash, autoDownload, autoUnpack, autoDelete); var newTorrent = await _torrentData.Add(rdTorrentId, infoHash, category, autoDownload, autoUnpack, autoDelete);
var rdTorrent = await GetRdNetClient().GetTorrentInfoAsync(rdTorrentId); var rdTorrent = await GetRdNetClient().GetTorrentInfoAsync(rdTorrentId);

View file

@ -22,6 +22,8 @@ namespace RdtClient.Service.Services
private readonly Download _download; private readonly Download _download;
private readonly String _destinationPath; private readonly String _destinationPath;
private readonly Torrent _torrent; private readonly Torrent _torrent;
private Boolean _cancelled = false;
private RarArchiveEntry _rarCurrentEntry; private RarArchiveEntry _rarCurrentEntry;
private Dictionary<String, Int64> _rarfileStatus; private Dictionary<String, Int64> _rarfileStatus;
@ -60,7 +62,10 @@ namespace RdtClient.Service.Services
await Task.Factory.StartNew(async delegate await Task.Factory.StartNew(async delegate
{ {
await Unpack(filePath); if (!_cancelled)
{
await Unpack(filePath);
}
}); });
} }
catch (Exception ex) catch (Exception ex)
@ -70,6 +75,11 @@ namespace RdtClient.Service.Services
} }
} }
public void Cancel()
{
_cancelled = true;
}
private async Task Unpack(String filePath) private async Task Unpack(String filePath)
{ {
try try
@ -101,6 +111,11 @@ namespace RdtClient.Service.Services
foreach (var entry in entries) foreach (var entry in entries)
{ {
if (_cancelled)
{
return;
}
_rarCurrentEntry = entry; _rarCurrentEntry = entry;
entry.WriteToDirectory(extractPath, entry.WriteToDirectory(extractPath,
@ -136,7 +151,7 @@ namespace RdtClient.Service.Services
Finished = true; Finished = true;
} }
} }
private void ArchiveOnCompressedBytesRead(Object sender, CompressedBytesReadEventArgs e) private void ArchiveOnCompressedBytesRead(Object sender, CompressedBytesReadEventArgs e)
{ {
if (_rarCurrentEntry == null) if (_rarCurrentEntry == null)

View file

@ -245,7 +245,7 @@ namespace RdtClient.Web.Controllers
foreach (var url in urls) foreach (var url in urls)
{ {
await _qBittorrent.TorrentsAdd(url.Trim(), true, true, false); await _qBittorrent.TorrentsAddMagnet(url.Trim(), request.Category, true, true, false);
} }
return Ok(); return Ok();
@ -265,10 +265,10 @@ namespace RdtClient.Web.Controllers
await file.CopyToAsync(target); await file.CopyToAsync(target);
var fileBytes = target.ToArray(); var fileBytes = target.ToArray();
await _qBittorrent.TorrentsAddFile(fileBytes, true, true, false); await _qBittorrent.TorrentsAddFile(fileBytes, request.Category, true, true, false);
} }
} }
if (request.Urls != null) if (request.Urls != null)
{ {
return await TorrentsAdd(request); return await TorrentsAdd(request);
@ -386,6 +386,7 @@ namespace RdtClient.Web.Controllers
public class QBTorrentsAddRequest public class QBTorrentsAddRequest
{ {
public String Urls { get; set; } public String Urls { get; set; }
public String Category { get; set; }
} }
public class QBTorrentsSetCategoryRequest public class QBTorrentsSetCategoryRequest

View file

@ -74,7 +74,7 @@ namespace RdtClient.Web.Controllers
var bytes = memoryStream.ToArray(); var bytes = memoryStream.ToArray();
await _torrents.UploadFile(bytes, formData.AutoDownload, formData.AutoUnpack, formData.AutoDelete); await _torrents.UploadFile(bytes, null, formData.AutoDownload, formData.AutoUnpack, formData.AutoDelete);
return Ok(); return Ok();
} }
@ -83,7 +83,7 @@ namespace RdtClient.Web.Controllers
[Route("UploadMagnet")] [Route("UploadMagnet")]
public async Task<ActionResult> UploadMagnet([FromBody] TorrentControllerUploadMagnetRequest request) public async Task<ActionResult> UploadMagnet([FromBody] TorrentControllerUploadMagnetRequest request)
{ {
await _torrents.UploadMagnet(request.MagnetLink, request.AutoDownload, request.AutoUnpack, request.AutoDelete); await _torrents.UploadMagnet(request.MagnetLink, null, request.AutoDownload, request.AutoUnpack, request.AutoDelete);
return Ok(); return Ok();
} }