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) public async Task<IList<String>?> GetDownloadLinks(Torrent torrent)
{ {
var torrentId = await GetClient().Torrents.GetHashInfoAsync(torrent.Hash, skipCache: true); 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)) if (downloadableFiles.Count == torrent.Files.Count && torrent.DownloadClient != Data.Enums.DownloadClient.Symlink)
.Select(file => $"https://torbox.app/fakedl/{torrentId?.Id}/{file.Id}") {
.ToList(); 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) public async Task<String> GetFileName(String downloadUrl)
@ -304,6 +312,7 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
using (HttpClient client = new()) using (HttpClient client = new())
{ {
client.Timeout = TimeSpan.FromSeconds(Settings.Get.Provider.Timeout);
var request = new HttpRequestMessage(HttpMethod.Head, uri); var request = new HttpRequestMessage(HttpMethod.Head, uri);
var response = await client.SendAsync(request); var response = await client.SendAsync(request);
if (response.Content.Headers.ContentDisposition != null) if (response.Content.Headers.ContentDisposition != null)
@ -314,6 +323,17 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
return fileName.Trim('"'); 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()); return HttpUtility.UrlDecode(uri.Segments.Last());
@ -336,6 +356,43 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
return Map(result!); 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) private void Log(String message, Torrent? torrent = null)
{ {
if (torrent != null) if (torrent != null)

View file

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

View file

@ -1,6 +1,7 @@
using System.Diagnostics; using System.Diagnostics;
using RdtClient.Data.Models.Data; using RdtClient.Data.Models.Data;
using RdtClient.Service.Helpers; using RdtClient.Service.Helpers;
using RdtClient.Service.Services.TorrentClients;
using SharpCompress.Archives; using SharpCompress.Archives;
using SharpCompress.Archives.Rar; using SharpCompress.Archives.Rar;
using SharpCompress.Archives.Zip; using SharpCompress.Archives.Zip;
@ -102,6 +103,11 @@ public class UnpackClient(Download download, String destinationPath)
await FileHelper.Delete(filePath); await FileHelper.Delete(filePath);
} }
if (_torrent.ClientKind == Data.Enums.Provider.TorBox)
{
TorBoxTorrentClient.MoveHashDirContents(extractPath, _torrent);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -113,6 +119,7 @@ public class UnpackClient(Download download, String destinationPath)
} }
} }
private static async Task<IList<String>> GetArchiveFiles(String filePath) private static async Task<IList<String>> GetArchiveFiles(String filePath)
{ {
await using Stream stream = File.OpenRead(filePath); await using Stream stream = File.OpenRead(filePath);
@ -161,7 +168,7 @@ public class UnpackClient(Download download, String destinationPath)
d => d =>
{ {
Debug.WriteLine(d); Debug.WriteLine(d);
Progess = (Int32) Math.Round(d); Progess = (Int32)Math.Round(d);
}, },
cancellationToken: cancellationToken); cancellationToken: cancellationToken);