[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:
parent
350185b702
commit
9d03c20aaa
4 changed files with 44 additions and 8 deletions
|
|
@ -67,6 +67,23 @@ public class DownloadData(DataContext dataContext)
|
||||||
await TorrentData.VoidCache();
|
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)
|
public async Task UpdateDownloadStarted(Guid downloadId, DateTimeOffset? dateTime)
|
||||||
{
|
{
|
||||||
var dbDownload = await dataContext.Downloads
|
var dbDownload = await dataContext.Downloads
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,11 @@ namespace RdtClient.Service.Helpers;
|
||||||
|
|
||||||
public static class DownloadHelper
|
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;
|
var fileUrl = download.Link;
|
||||||
|
|
||||||
if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null || fileName == null)
|
if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -19,6 +19,8 @@ public static class DownloadHelper
|
||||||
var uri = new Uri(fileUrl);
|
var uri = new Uri(fileUrl);
|
||||||
var torrentPath = Path.Combine(downloadPath, directory);
|
var torrentPath = Path.Combine(downloadPath, directory);
|
||||||
|
|
||||||
|
var fileName = download.FileName;
|
||||||
|
|
||||||
if (String.IsNullOrWhiteSpace(fileName))
|
if (String.IsNullOrWhiteSpace(fileName))
|
||||||
{
|
{
|
||||||
fileName = uri.Segments.Last();
|
fileName = uri.Segments.Last();
|
||||||
|
|
@ -66,11 +68,14 @@ public static class DownloadHelper
|
||||||
var uri = new Uri(fileUrl);
|
var uri = new Uri(fileUrl);
|
||||||
var torrentPath = RemoveInvalidPathChars(torrent.RdName);
|
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();
|
var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -349,6 +349,9 @@ public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Dow
|
||||||
|
|
||||||
var downloadLink = await torrents.UnrestrictLink(download.DownloadId);
|
var downloadLink = await torrents.UnrestrictLink(download.DownloadId);
|
||||||
download.Link = downloadLink;
|
download.Link = downloadLink;
|
||||||
|
|
||||||
|
var fileName = await torrents.RetrieveFileName(download.DownloadId);
|
||||||
|
download.FileName = fileName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
||||||
|
|
@ -370,6 +370,19 @@ public class Torrents(
|
||||||
return unrestrictedLink;
|
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()
|
public async Task<Profile> GetProfile()
|
||||||
{
|
{
|
||||||
var user = await TorrentClient.GetUser();
|
var user = await TorrentClient.GetUser();
|
||||||
|
|
@ -547,9 +560,7 @@ public class Torrents(
|
||||||
|
|
||||||
var downloadPath = DownloadPath(download.Torrent!);
|
var downloadPath = DownloadPath(download.Torrent!);
|
||||||
|
|
||||||
var fileName = await TorrentClient.GetFileName(download);
|
var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download);
|
||||||
|
|
||||||
var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download, fileName);
|
|
||||||
|
|
||||||
if (filePath != null)
|
if (filePath != null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue