Skip to content

Commit

Permalink
Changed how files are saved.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerfar committed Jan 6, 2025
1 parent d43774d commit d5ee5d2
Showing 1 changed file with 34 additions and 60 deletions.
94 changes: 34 additions & 60 deletions server/RdtClient.Service/Helpers/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static class DownloadHelper
var directory = RemoveInvalidPathChars(torrent.RdName);

var uri = new Uri(fileUrl);
var torrentPath = Path.Combine(downloadPath, directory);

var fileName = download.FileName;

Expand All @@ -30,90 +29,65 @@ public static class DownloadHelper

fileName = FileHelper.RemoveInvalidFileNameChars(fileName);

var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();
var torrentPath = downloadPath;

if (matchingTorrentFiles.Count > 0)
if (torrent.Files.Count > 1)
{
var matchingTorrentFile = matchingTorrentFiles[0];
torrentPath = Path.Combine(downloadPath, directory);

var subPath = Path.GetDirectoryName(matchingTorrentFile.Path);
var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();

if (!String.IsNullOrWhiteSpace(subPath))
if (matchingTorrentFiles.Count > 0)
{
subPath = subPath.Trim('/').Trim('\\');

torrentPath = Path.Combine(torrentPath, subPath);
} else if (torrent.Files.Count == 1)
{
if (directory != fileName)
{
throw new($"Torrent path {torrentPath} does not match file name {fileName}. This is a requirement for single file torrents.");
}

return torrentPath;
}
}

if (!Directory.Exists(torrentPath))
{
Directory.CreateDirectory(torrentPath);
}

var filePath = Path.Combine(torrentPath, fileName);

return filePath;
}

public static String? GetDownloadPath(Torrent torrent, Download download)
{
var fileUrl = download.Link;

if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null)
{
return null;
}
var matchingTorrentFile = matchingTorrentFiles[0];

var uri = new Uri(fileUrl);
var torrentPath = RemoveInvalidPathChars(torrent.RdName);
var subPath = Path.GetDirectoryName(matchingTorrentFile.Path);

var fileName = download.FileName;
if (!String.IsNullOrWhiteSpace(subPath))
{
subPath = subPath.Trim('/').Trim('\\');

if (String.IsNullOrWhiteSpace(fileName))
{
fileName = uri.Segments.Last();
torrentPath = Path.Combine(torrentPath, subPath);
}
else if (torrent.Files.Count == 1)
{
if (directory != fileName)
{
throw new($"Torrent path {torrentPath} does not match file name {fileName}. This is a requirement for single file torrents.");
}

fileName = HttpUtility.UrlDecode(fileName);
return torrentPath;
}
}
}

var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();

if (matchingTorrentFiles.Count > 0)
else if (torrent.Files.Count == 1)
{
var matchingTorrentFile = matchingTorrentFiles[0];

var subPath = Path.GetDirectoryName(matchingTorrentFile.Path);
var torrentFile = torrent.Files[0];
var subPath = Path.GetDirectoryName(torrentFile.Path);

if (!String.IsNullOrWhiteSpace(subPath))
{
subPath = subPath.Trim('/').Trim('\\');

torrentPath = Path.Combine(torrentPath, subPath);
} else if (torrent.Files.Count == 1)
{
if (torrentPath != fileName)
{
throw new($"Torrent path {torrentPath} does not match file name {fileName}. This is a requirement for single file torrents.");
}

return torrentPath;
}
}

if (!String.IsNullOrWhiteSpace(torrentPath) && !Directory.Exists(torrentPath))
{
Directory.CreateDirectory(torrentPath);
}

var filePath = Path.Combine(torrentPath, fileName);

return filePath;
}

public static String? GetDownloadPath(Torrent torrent, Download download)
{
return GetDownloadPath("", torrent, download);
}

private static String RemoveInvalidPathChars(String path)
{
return String.Concat(path.Split(Path.GetInvalidPathChars()));
Expand Down

0 comments on commit d5ee5d2

Please sign in to comment.