[TB] Change DownloadHelper to use download.FileName

Also adds the UpdateFileName function to DownloadHelper & DownloadData to push FileName to DB
This commit is contained in:
Sam Heinz 2024-12-10 23:47:55 +10:00
parent 350185b702
commit 9d03c20aaa
4 changed files with 44 additions and 8 deletions

View file

@ -67,6 +67,23 @@ public class DownloadData(DataContext dataContext)
await TorrentData.VoidCache();
}
public async Task UpdateFileName(Guid downloadId, String fileName)
{
var dbDownload = await dataContext.Downloads
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
if (dbDownload == null)
{
return;
}
dbDownload.FileName = fileName;
await dataContext.SaveChangesAsync();
await TorrentData.VoidCache();
}
public async Task UpdateDownloadStarted(Guid downloadId, DateTimeOffset? dateTime)
{
var dbDownload = await dataContext.Downloads

View file

@ -5,11 +5,11 @@ namespace RdtClient.Service.Helpers;
public static class DownloadHelper
{
public static String? GetDownloadPath(String downloadPath, Torrent torrent, Download download, String? fileName = null)
public static String? GetDownloadPath(String downloadPath, Torrent torrent, Download download)
{
var fileUrl = download.Link;
if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null || fileName == null)
if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null)
{
return null;
}
@ -19,6 +19,8 @@ public static class DownloadHelper
var uri = new Uri(fileUrl);
var torrentPath = Path.Combine(downloadPath, directory);
var fileName = download.FileName;
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = uri.Segments.Last();
@ -66,11 +68,14 @@ public static class DownloadHelper
var uri = new Uri(fileUrl);
var torrentPath = RemoveInvalidPathChars(torrent.RdName);
var fileName = uri.Segments.Last();
var fileName = download.FileName;
fileName = HttpUtility.UrlDecode(fileName);
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = uri.Segments.Last();
fileName = FileHelper.RemoveInvalidFileNameChars(fileName);
fileName = HttpUtility.UrlDecode(fileName);
}
var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();

View file

@ -349,6 +349,9 @@ 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;
}
}
catch (Exception ex)

View file

@ -370,6 +370,19 @@ public class Torrents(
return unrestrictedLink;
}
public async Task<String> RetrieveFileName(Guid downloadId)
{
var download = await downloads.GetById(downloadId) ?? throw new($"Download with ID {downloadId} not found");
Log($"Unrestricting link", download, download.Torrent);
var fileName = await TorrentClient.GetFileName(download.Link!);
await downloads.UpdateFileName(downloadId, fileName);
return fileName;
}
public async Task<Profile> GetProfile()
{
var user = await TorrentClient.GetUser();
@ -547,9 +560,7 @@ public class Torrents(
var downloadPath = DownloadPath(download.Torrent!);
var fileName = await TorrentClient.GetFileName(download);
var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download, fileName);
var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download);
if (filePath != null)
{