From 629babd9bd28ffae25091378bab18b290523698c Mon Sep 17 00:00:00 2001 From: Roger Far Date: Sat, 16 May 2020 20:21:00 -0600 Subject: [PATCH] Fixed extracting when the path in the rar file has the torrent as a subdirectory. --- .../Services/DownloadManager.cs | 41 +++++++++++++++---- .../RdtClient.Service/Services/TaskRunner.cs | 5 +-- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/server/RdtClient.Service/Services/DownloadManager.cs b/server/RdtClient.Service/Services/DownloadManager.cs index d3f4f97..cef813f 100644 --- a/server/RdtClient.Service/Services/DownloadManager.cs +++ b/server/RdtClient.Service/Services/DownloadManager.cs @@ -36,24 +36,33 @@ namespace RdtClient.Service.Services private DownloadManager ActiveDownload => TaskRunner.ActiveDownloads[Download.DownloadId]; - public async Task Start(String destinationFolderPath, String rootfolderPath) + public async Task Start(String destinationFolderPath, String torrentName, DownloadStatus status) { - ActiveDownload.NewStatus = DownloadStatus.Downloading; ActiveDownload.BytesDownloaded = 0; ActiveDownload.BytesSize = 0; ActiveDownload.Speed = 0; - _bytesLastUpdate = 0; - _nextUpdate = DateTime.UtcNow.AddSeconds(1); - var fileUrl = Download.Link; var uri = new Uri(fileUrl); - var filePath = Path.Combine(destinationFolderPath, uri.Segments.Last()); + var torrentPath = Path.Combine(destinationFolderPath, torrentName); + var filePath = Path.Combine(torrentPath, uri.Segments.Last()); - if (!Directory.Exists(destinationFolderPath)) + if (status == DownloadStatus.Unpacking) { - Directory.CreateDirectory(destinationFolderPath); + await Extract(filePath, destinationFolderPath, torrentName); + + return; + } + + ActiveDownload.NewStatus = DownloadStatus.Downloading; + + _bytesLastUpdate = 0; + _nextUpdate = DateTime.UtcNow.AddSeconds(1); + + if (!Directory.Exists(torrentPath)) + { + Directory.CreateDirectory(torrentPath); } var webRequest = WebRequest.Create(fileUrl); @@ -105,6 +114,11 @@ namespace RdtClient.Service.Services ActiveDownload.Speed = 0; ActiveDownload.BytesDownloaded = ActiveDownload.BytesSize; + await Extract(filePath, destinationFolderPath, torrentName); + } + + private async Task Extract(String filePath, String destinationFolderPath, String torrentName) + { try { if (filePath.EndsWith(".rar")) @@ -124,11 +138,20 @@ namespace RdtClient.Service.Services _rarCurrentEntry = null; archive.CompressedBytesRead += ArchiveOnCompressedBytesRead; + var extractPath = destinationFolderPath; + + if (!entries.Any(m => m.Key.StartsWith(torrentName))) + { + extractPath = Path.Combine(destinationFolderPath, torrentName); + } + foreach (var entry in entries) { + + _rarCurrentEntry = entry; - entry.WriteToDirectory(rootfolderPath, + entry.WriteToDirectory(extractPath, new ExtractionOptions { ExtractFullPath = true, diff --git a/server/RdtClient.Service/Services/TaskRunner.cs b/server/RdtClient.Service/Services/TaskRunner.cs index 4198137..b5859b7 100644 --- a/server/RdtClient.Service/Services/TaskRunner.cs +++ b/server/RdtClient.Service/Services/TaskRunner.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Concurrent; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -116,10 +115,8 @@ namespace RdtClient.Service.Services if (ActiveDownloads.TryAdd(download.DownloadId, downloadManager)) { - var folderPath = Path.Combine(destinationFolderPath, download.Torrent.RdName); - downloadManager.Download = download; - await downloadManager.Start(folderPath, destinationFolderPath); + await downloadManager.Start(destinationFolderPath, download.Torrent.RdName, download.Status); } }); }