Reverted some changes from the gaisberg PR.
This commit is contained in:
parent
d463e690c5
commit
5a8ab0789a
4 changed files with 83 additions and 151 deletions
|
|
@ -210,28 +210,18 @@ public class DownloadData
|
|||
await TorrentData.VoidCache();
|
||||
}
|
||||
|
||||
public async Task UpdateRemoteId(Guid downloadId, string remoteId)
|
||||
public async Task UpdateRemoteId(Guid downloadId, String remoteId)
|
||||
{
|
||||
await UpdateRemoteIdRange(new Dictionary<Guid, string>
|
||||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
{ downloadId, remoteId }
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public async Task UpdateRemoteIdRange(Dictionary<Guid, string> remoteIdRange)
|
||||
{
|
||||
foreach (var entry in remoteIdRange)
|
||||
{
|
||||
var dbDownload = await _dataContext.Downloads.FirstOrDefaultAsync(m => m.DownloadId == entry.Key);
|
||||
if (dbDownload == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
dbDownload.RemoteId = entry.Value;
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.RemoteId = remoteId;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,13 +23,11 @@ public class DownloadClient
|
|||
public Int64 BytesTotal { get; private set; }
|
||||
public Int64 BytesDone { get; private set; }
|
||||
|
||||
public DownloadClient(Download download, Torrent torrent, string destinationPath)
|
||||
public DownloadClient(Download download, Torrent torrent, String destinationPath)
|
||||
{
|
||||
_download = download;
|
||||
_torrent = torrent;
|
||||
_destinationPath = destinationPath;
|
||||
|
||||
Type = Settings.Get.DownloadClient.Client;
|
||||
}
|
||||
|
||||
public async Task<String?> Start()
|
||||
|
|
@ -54,7 +52,9 @@ public class DownloadClient
|
|||
|
||||
await FileHelper.Delete(filePath);
|
||||
|
||||
Downloader = Type switch
|
||||
Type = Settings.Get.DownloadClient.Client;
|
||||
|
||||
Downloader = Settings.Get.DownloadClient.Client switch
|
||||
{
|
||||
Data.Enums.DownloadClient.Internal => new InternalDownloader(_download.Link, filePath),
|
||||
Data.Enums.DownloadClient.Aria2c => new Aria2cDownloader(_download.RemoteId, _download.Link, filePath),
|
||||
|
|
|
|||
|
|
@ -82,11 +82,6 @@ public class Downloads
|
|||
await _downloadData.UpdateRemoteId(downloadId, remoteId);
|
||||
}
|
||||
|
||||
public async Task UpdateRemoteIdRange(Dictionary<Guid, String> updateDict)
|
||||
{
|
||||
await _downloadData.UpdateRemoteIdRange(updateDict);
|
||||
}
|
||||
|
||||
public async Task DeleteForTorrent(Guid torrentId)
|
||||
{
|
||||
await _downloadData.DeleteForTorrent(torrentId);
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ public class TorrentRunner
|
|||
private readonly Downloads _downloads;
|
||||
private readonly RemoteService _remoteService;
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly Dictionary<Guid, string> _aggregatedDownloadResults;
|
||||
|
||||
public TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Downloads downloads, RemoteService remoteService)
|
||||
{
|
||||
|
|
@ -30,7 +29,6 @@ public class TorrentRunner
|
|||
_torrents = torrents;
|
||||
_downloads = downloads;
|
||||
_remoteService = remoteService;
|
||||
_aggregatedDownloadResults = new Dictionary<Guid, string>();
|
||||
|
||||
_httpClient = new HttpClient
|
||||
{
|
||||
|
|
@ -89,18 +87,7 @@ public class TorrentRunner
|
|||
Log($"No RealDebridApiKey set in settings");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Settings.Get.DownloadClient.Client == Data.Enums.DownloadClient.Symlink)
|
||||
{
|
||||
var rcloneMountPath = Settings.Get.DownloadClient.RcloneMountPath;
|
||||
|
||||
if (!Directory.Exists(rcloneMountPath))
|
||||
{
|
||||
Log($"Rclone mount path ({rcloneMountPath}) was not found!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var settingDownloadLimit = Settings.Get.General.DownloadLimit;
|
||||
if (settingDownloadLimit < 1)
|
||||
{
|
||||
|
|
@ -323,15 +310,77 @@ public class TorrentRunner
|
|||
.OrderBy(m => m.DownloadQueued)
|
||||
.ToList();
|
||||
|
||||
_aggregatedDownloadResults.Clear();
|
||||
foreach (var download in queuedDownloads)
|
||||
{
|
||||
await ProcessDownload(download, torrent, settingDownloadPath, settingDownloadLimit);
|
||||
}
|
||||
if (_aggregatedDownloadResults.Count > 0)
|
||||
{
|
||||
await _downloads.UpdateRemoteIdRange(_aggregatedDownloadResults);
|
||||
Log($"Processing to download", download, torrent);
|
||||
|
||||
if (ActiveDownloadClients.Count >= settingDownloadLimit)
|
||||
{
|
||||
Log($"Not starting download because there are already the max number of downloads active", download, torrent);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ActiveDownloadClients.ContainsKey(download.DownloadId))
|
||||
{
|
||||
Log($"Not starting download because this download is already active", download, torrent);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Log($"Unrestricting links", download, torrent);
|
||||
|
||||
var downloadLink = await _torrents.UnrestrictLink(download.DownloadId);
|
||||
download.Link = downloadLink;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Cannot unrestrict link: {ex.Message}", ex.Message);
|
||||
|
||||
await _downloads.UpdateError(download.DownloadId, ex.Message);
|
||||
await _downloads.UpdateCompleted(download.DownloadId, DateTimeOffset.UtcNow);
|
||||
download.Error = ex.Message;
|
||||
download.Completed = DateTimeOffset.UtcNow;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
Log($"Marking download as started", download, torrent);
|
||||
|
||||
download.DownloadStarted = DateTime.UtcNow;
|
||||
await _downloads.UpdateDownloadStarted(download.DownloadId, download.DownloadStarted);
|
||||
|
||||
var downloadPath = settingDownloadPath;
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(torrent.Category))
|
||||
{
|
||||
downloadPath = Path.Combine(downloadPath, torrent.Category);
|
||||
}
|
||||
|
||||
Log($"Setting download path to {downloadPath}", download, torrent);
|
||||
|
||||
// Start the download process
|
||||
var downloadClient = new DownloadClient(download, torrent, downloadPath);
|
||||
|
||||
if (ActiveDownloadClients.TryAdd(download.DownloadId, downloadClient))
|
||||
{
|
||||
Log($"Starting download", download, torrent);
|
||||
|
||||
var remoteId = await downloadClient.Start();
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(remoteId) && download.RemoteId != remoteId)
|
||||
{
|
||||
Log($"Received ID {remoteId}", download, torrent);
|
||||
|
||||
await _downloads.UpdateRemoteId(download.DownloadId, remoteId);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($"No ID received", download, torrent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if there are any unpacks that are queued and can be started.
|
||||
|
|
@ -379,18 +428,8 @@ public class TorrentRunner
|
|||
continue;
|
||||
}
|
||||
|
||||
if (Settings.Get.DownloadClient.Client == Data.Enums.DownloadClient.Symlink)
|
||||
{
|
||||
Log("Lets not unzip with symlink downloader...");
|
||||
|
||||
await _downloads.UpdateError(download.DownloadId, "Will not unzip with SymlinkDownloader!");
|
||||
await _downloads.UpdateCompleted(download.DownloadId, DateTimeOffset.UtcNow);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if we have reached the download limit, if so queue the download, but don't start it.
|
||||
if (TorrentRunner.ActiveUnpackClients.Count >= settingUnpackLimit)
|
||||
// Check if we have reached the download limit, if so queue the download, but don't start it.
|
||||
if (TorrentRunner.ActiveUnpackClients.Count >= settingUnpackLimit)
|
||||
{
|
||||
Log($"Not starting unpack because there are already the max number of unpacks active", download, torrent);
|
||||
|
||||
|
|
@ -589,96 +628,4 @@ public class TorrentRunner
|
|||
|
||||
_logger.LogError(message);
|
||||
}
|
||||
|
||||
private async Task ProcessDownload(Download download, Torrent torrent, string settingDownloadPath, int settingDownloadLimit)
|
||||
{
|
||||
if (ActiveDownloadClients.Count >= settingDownloadLimit)
|
||||
{
|
||||
Log($"Not starting download because there are already the max number of downloads active", download, torrent);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (ActiveDownloadClients.ContainsKey(download.DownloadId))
|
||||
{
|
||||
Log($"Not starting download because this download is already active", download, torrent);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (download.Link == null)
|
||||
{
|
||||
Log($"Unrestricting links", download, torrent);
|
||||
|
||||
var downloadLink = await _torrents.UnrestrictLink(download.DownloadId);
|
||||
download.Link = downloadLink;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Cannot unrestrict link: {ex.Message}", ex.Message);
|
||||
|
||||
await _downloads.UpdateError(download.DownloadId, ex.Message);
|
||||
await _downloads.UpdateCompleted(download.DownloadId, DateTimeOffset.UtcNow);
|
||||
download.Error = ex.Message;
|
||||
download.Completed = DateTimeOffset.UtcNow;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Log($"Marking download as started", download, torrent);
|
||||
|
||||
download.DownloadStarted = DateTime.UtcNow;
|
||||
await _downloads.UpdateDownloadStarted(download.DownloadId, download.DownloadStarted);
|
||||
|
||||
var downloadPath = settingDownloadPath;
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(torrent.Category))
|
||||
{
|
||||
downloadPath = Path.Combine(downloadPath, torrent.Category);
|
||||
}
|
||||
|
||||
Log($"Setting download path to {downloadPath}", download, torrent);
|
||||
|
||||
var downloadClient = new DownloadClient(download, torrent, downloadPath);
|
||||
|
||||
if (downloadClient.Type == Data.Enums.DownloadClient.Symlink) // Check if the type is "Symlink"
|
||||
{
|
||||
// If it's Symlink type, start the download concurrently
|
||||
_ = Task.Run(async () => await StartDownload(download, torrent, downloadClient));
|
||||
}
|
||||
else
|
||||
{
|
||||
await StartDownload(download, torrent, downloadClient);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StartDownload(Download download, Torrent torrent, DownloadClient downloadClient)
|
||||
{
|
||||
if (ActiveDownloadClients.TryAdd(download.DownloadId, downloadClient) ||
|
||||
(downloadClient.Type == Data.Enums.DownloadClient.Symlink && download.RetryCount > 0))
|
||||
{
|
||||
Log($"Starting download", download, torrent);
|
||||
|
||||
var remoteId = await downloadClient.Start();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(remoteId) && download.RemoteId != remoteId)
|
||||
{
|
||||
Log($"Received ID {remoteId}", download, torrent);
|
||||
_aggregatedDownloadResults.Add(download.DownloadId, remoteId);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($"No ID received", download, torrent);
|
||||
// Lets us redo the download next cycle
|
||||
if (downloadClient.Type == Data.Enums.DownloadClient.Symlink)
|
||||
{
|
||||
Log($"Marking download as ended, so we can retry", download, torrent);
|
||||
download.DownloadStarted = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue