Use richer GetDownloadInfos instead of GetDownloadLinks

*Most* debrid providers know the filename as soon as they know the restricted links. By allowing them to tell the program this at an earlier stage, we don't need the `GetFileName` call for most debrid providers
This commit is contained in:
Cucumberrbob 2025-03-11 12:12:16 +00:00
parent a366328472
commit e776236270
No known key found for this signature in database
GPG key ID: 2B935C47401C3614
7 changed files with 46 additions and 18 deletions

View file

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using RdtClient.Data.Models.Data;
using Download = RdtClient.Data.Models.Data.Download;
namespace RdtClient.Data.Data;
@ -29,13 +30,14 @@ public class DownloadData(DataContext dataContext)
.FirstOrDefaultAsync(m => m.TorrentId == torrentId && m.Path == path);
}
public async Task<Download> Add(Guid torrentId, String path)
public async Task<Download> Add(Guid torrentId, DownloadInfo downloadInfo)
{
var download = new Download
{
DownloadId = Guid.NewGuid(),
TorrentId = torrentId,
Path = path,
FileName = downloadInfo.FileName,
Path = downloadInfo.RestrictedLink,
Added = DateTimeOffset.UtcNow,
DownloadQueued = DateTimeOffset.UtcNow,
RetryCount = 0

View file

@ -26,7 +26,7 @@ public class Download
public DateTimeOffset? Completed { get; set; }
public Int32 RetryCount { get; set; }
public String? Error { get; set; }
public String? RemoteId { get; set; }
@ -41,4 +41,20 @@ public class Download
[NotMapped]
public Int64 Speed { get; set; }
}
/// <summary>
/// Used to create <see cref="Download"/>s
/// </summary>
public class DownloadInfo
{
/// <summary>
/// The name of the file. Should not include directory.
/// If the filename is not known, set tn null and `GetFileName` will be called with the unrestricted link.
/// </summary>
public required String? FileName;
/// <summary>
/// The restricted link to download this download. If the debrid serice in question does not have restricted links, use either a fake or the unrestricted link
/// </summary>
public required String RestrictedLink;
}

View file

@ -1,4 +1,5 @@
using RdtClient.Data.Data;
using RdtClient.Data.Models.Data;
using Download = RdtClient.Data.Models.Data.Download;
namespace RdtClient.Service.Services;
@ -20,9 +21,9 @@ public class Downloads(DownloadData downloadData) : IDownloads
return await downloadData.Get(torrentId, path);
}
public async Task<Download> Add(Guid torrentId, String path)
public async Task<Download> Add(Guid torrentId, DownloadInfo downloadInfo)
{
return await downloadData.Add(torrentId, path);
return await downloadData.Add(torrentId, downloadInfo);
}
public async Task UpdateUnrestrictedLink(Guid downloadId, String unrestrictedLink)

View file

@ -7,7 +7,7 @@ public interface IDownloads
Task<List<Download>> GetForTorrent(Guid torrentId);
Task<Download?> GetById(Guid downloadId);
Task<Download?> Get(Guid torrentId, String path);
Task<Download> Add(Guid torrentId, String path);
Task<Download> Add(Guid torrentId, DownloadInfo downloadInfo);
Task UpdateUnrestrictedLink(Guid downloadId, String unrestrictedLink);
Task UpdateFileName(Guid downloadId, String fileName);
Task UpdateDownloadStarted(Guid downloadId, DateTimeOffset? dateTime);

View file

@ -14,6 +14,12 @@ public interface ITorrentClient
Task Delete(String torrentId);
Task<String> Unrestrict(String link);
Task<Torrent> UpdateData(Torrent torrent, TorrentClientTorrent? torrentClientTorrent);
Task<IList<String>?> GetDownloadLinks(Torrent torrent);
Task<String> GetFileName(String downloadUrl);
Task<IList<DownloadInfo>?> GetDownloadInfos(Torrent torrent);
/// <summary>
/// To be called only when <see cref="Data.Models.Data.Download" />.<see cref="Data.Models.Data.Download.FileName" /> is not set by
/// <see cref="GetDownloadInfos" />
/// </summary>
/// <param name="download">The download to get the filename of</param>
/// <returns>The filename of the download</returns>
Task<String> GetFileName(Download download);
}

View file

@ -363,8 +363,11 @@ public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Dow
var downloadLink = await torrents.UnrestrictLink(download.DownloadId);
download.Link = downloadLink;
var fileName = await torrents.RetrieveFileName(download.DownloadId);
download.FileName = fileName;
if (download.FileName == null)
{
var fileName = await torrents.RetrieveFileName(download.DownloadId);
download.FileName = fileName;
}
}
}
catch (Exception ex)

View file

@ -235,14 +235,14 @@ public class Torrents(
return;
}
var downloadLinks = await TorrentClient.GetDownloadLinks(torrent);
var downloadInfos = await TorrentClient.GetDownloadInfos(torrent);
if (downloadLinks == null)
if (downloadInfos == null)
{
return;
}
if (downloadLinks.Count == 0)
if (downloadInfos.Count == 0)
{
logger.LogInformation("All files excluded by filters (IncludeRegex: {includeRegex}, ExcludeRegex: {excludeRegex}, DownloadMinSize: {downloadMinSize} {torrentInfo}",
torrent.IncludeRegex,
@ -256,14 +256,14 @@ public class Torrents(
return;
}
foreach (var downloadLink in downloadLinks)
foreach (var downloadInfo in downloadInfos)
{
// Make sure downloads don't get added multiple times
var downloadExists = await downloads.Get(torrent.TorrentId, downloadLink);
var downloadExists = await downloads.Get(torrent.TorrentId, downloadInfo.RestrictedLink);
if (downloadExists == null && !String.IsNullOrWhiteSpace(downloadLink))
if (downloadExists == null && !String.IsNullOrWhiteSpace(downloadInfo.RestrictedLink))
{
await downloads.Add(torrent.TorrentId, downloadLink);
await downloads.Add(torrent.TorrentId, downloadInfo);
}
}
}
@ -396,7 +396,7 @@ public class Torrents(
Log($"Retrieving filename for", download, download.Torrent);
var fileName = await TorrentClient.GetFileName(download.Link!);
var fileName = await TorrentClient.GetFileName(download!);
await downloads.UpdateFileName(downloadId, fileName);