Merge pull request #749 from asylumexp/fix/tb-download-as-zip

[TB] Add downloading via zip functionality
This commit is contained in:
Cucumberrbob 2025-03-16 15:34:55 +00:00 committed by GitHub
commit b4bb775617
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 76 additions and 18 deletions

View file

@ -287,10 +287,18 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
public async Task<IList<String>?> GetDownloadLinks(Torrent torrent)
{
var torrentId = await GetClient().Torrents.GetHashInfoAsync(torrent.Hash, skipCache: true);
var downloadableFiles = torrent.Files.Where(file => fileFilter.IsDownloadable(torrent, file.Path, file.Bytes)).ToList();
return torrent.Files.Where(file => fileFilter.IsDownloadable(torrent, file.Path, file.Bytes))
.Select(file => $"https://torbox.app/fakedl/{torrentId?.Id}/{file.Id}")
.ToList();
if (downloadableFiles.Count == torrent.Files.Count && torrent.DownloadClient != Data.Enums.DownloadClient.Symlink)
{
logger.LogDebug("Downloading files from TorBox as a zip.");
return [$"https://torbox.app/fakedl/{torrentId?.Id}/zip"];
}
else
{
logger.LogDebug("Downloading files from TorBox individually.");
return downloadableFiles.Select(file => $"https://torbox.app/fakedl/{torrentId?.Id}/{file.Id}").ToList();
}
}
public async Task<String> GetFileName(String downloadUrl)
@ -304,6 +312,7 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
using (HttpClient client = new())
{
client.Timeout = TimeSpan.FromSeconds(Settings.Get.Provider.Timeout);
var request = new HttpRequestMessage(HttpMethod.Head, uri);
var response = await client.SendAsync(request);
if (response.Content.Headers.ContentDisposition != null)
@ -314,6 +323,17 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
return fileName.Trim('"');
}
}
else
{
if (response.Content.Headers.ContentType?.MediaType == "application/zip")
{
return $"download-{new Random().Next(1, 10001)}.zip";
}
else
{
logger.LogDebug($"Failed to get filename for URI {downloadUrl}");
}
}
}
return HttpUtility.UrlDecode(uri.Segments.Last());
@ -335,7 +355,44 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
return Map(result!);
}
public static void MoveHashDirContents(String extractPath, Torrent _torrent)
{
var hashDir = Path.Combine(extractPath, _torrent.Hash);
if (Directory.Exists(hashDir))
{
var innerFolder = Directory.GetDirectories(hashDir)[0];
var moveDir = extractPath;
if (!extractPath.EndsWith(_torrent.RdName!))
{
moveDir = hashDir;
}
foreach (var file in Directory.GetFiles(innerFolder))
{
var destFile = Path.Combine(moveDir, Path.GetFileName(file));
File.Move(file, destFile);
}
foreach (var dir in Directory.GetDirectories(innerFolder))
{
var destDir = Path.Combine(moveDir, Path.GetFileName(dir));
Directory.Move(dir, destDir);
}
if (!extractPath.Contains(_torrent.RdName!))
{
Directory.Delete(innerFolder, true);
}
else
{
Directory.Delete(hashDir, true);
}
}
}
private void Log(String message, Torrent? torrent = null)
{
if (torrent != null)

View file

@ -8,7 +8,6 @@ using RdtClient.Service.Services.Downloaders;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text.Json;
using System.Web;
namespace RdtClient.Service.Services;
@ -447,13 +446,8 @@ public class TorrentRunner(ILogger<TorrentRunner> logger, Torrents torrents, Dow
// Check if the unpacking process is even needed
var uri = new Uri(download.Link);
var fileName = uri.Segments.Last();
fileName = HttpUtility.UrlDecode(fileName);
Log($"Found file name {fileName}", download, torrent);
var extension = Path.GetExtension(fileName);
var extension = Path.GetExtension(download.FileName);
if ((extension != ".rar" && extension != ".zip") ||
torrent.DownloadClient == Data.Enums.DownloadClient.Symlink)

View file

@ -1,6 +1,7 @@
using System.Diagnostics;
using RdtClient.Data.Models.Data;
using RdtClient.Service.Helpers;
using RdtClient.Service.Services.TorrentClients;
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Archives.Zip;
@ -10,13 +11,13 @@ namespace RdtClient.Service.Services;
public class UnpackClient(Download download, String destinationPath)
{
public Boolean Finished { get; private set; }
public String? Error { get; private set; }
public Int32 Progess { get; private set; }
private readonly Torrent _torrent = download.Torrent ?? throw new($"Torrent is null");
private readonly CancellationTokenSource _cancellationTokenSource = new();
public void Start()
@ -69,13 +70,13 @@ public class UnpackClient(Download download, String destinationPath)
if (archiveEntries.Any(m => m.Contains(".r00")))
{
extractPathTemp = Path.Combine(extractPath, Guid.NewGuid().ToString());
if (!Directory.Exists(extractPathTemp))
{
Directory.CreateDirectory(extractPathTemp);
}
}
if (extractPathTemp != null)
{
Extract(filePath, extractPathTemp, cancellationToken);
@ -102,6 +103,11 @@ public class UnpackClient(Download download, String destinationPath)
await FileHelper.Delete(filePath);
}
if (_torrent.ClientKind == Data.Enums.Provider.TorBox)
{
TorBoxTorrentClient.MoveHashDirContents(extractPath, _torrent);
}
}
catch (Exception ex)
{
@ -113,6 +119,7 @@ public class UnpackClient(Download download, String destinationPath)
}
}
private static async Task<IList<String>> GetArchiveFiles(String filePath)
{
await using Stream stream = File.OpenRead(filePath);
@ -161,10 +168,10 @@ public class UnpackClient(Download download, String destinationPath)
d =>
{
Debug.WriteLine(d);
Progess = (Int32) Math.Round(d);
Progess = (Int32)Math.Round(d);
},
cancellationToken: cancellationToken);
archive.Dispose();
GC.Collect();