Changed how files are saved.

This commit is contained in:
Roger Far 2025-01-05 20:56:37 -07:00
parent d43774d9b5
commit d5ee5d209c

View file

@ -17,7 +17,6 @@ public static class DownloadHelper
var directory = RemoveInvalidPathChars(torrent.RdName); var directory = RemoveInvalidPathChars(torrent.RdName);
var uri = new Uri(fileUrl); var uri = new Uri(fileUrl);
var torrentPath = Path.Combine(downloadPath, directory);
var fileName = download.FileName; var fileName = download.FileName;
@ -30,31 +29,51 @@ public static class DownloadHelper
fileName = FileHelper.RemoveInvalidFileNameChars(fileName); 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 (matchingTorrentFiles.Count > 0)
{
var matchingTorrentFile = matchingTorrentFiles[0];
var subPath = Path.GetDirectoryName(matchingTorrentFile.Path);
if (!String.IsNullOrWhiteSpace(subPath))
{
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;
}
}
}
else if (torrent.Files.Count == 1)
{
var torrentFile = torrent.Files[0];
var subPath = Path.GetDirectoryName(torrentFile.Path);
if (!String.IsNullOrWhiteSpace(subPath)) if (!String.IsNullOrWhiteSpace(subPath))
{ {
subPath = subPath.Trim('/').Trim('\\'); subPath = subPath.Trim('/').Trim('\\');
torrentPath = Path.Combine(torrentPath, subPath); 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)) if (!String.IsNullOrWhiteSpace(torrentPath) && !Directory.Exists(torrentPath))
{ {
Directory.CreateDirectory(torrentPath); Directory.CreateDirectory(torrentPath);
} }
@ -66,52 +85,7 @@ public static class DownloadHelper
public static String? GetDownloadPath(Torrent torrent, Download download) public static String? GetDownloadPath(Torrent torrent, Download download)
{ {
var fileUrl = download.Link; return GetDownloadPath("", torrent, download);
if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null)
{
return null;
}
var uri = new Uri(fileUrl);
var torrentPath = RemoveInvalidPathChars(torrent.RdName);
var fileName = download.FileName;
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = uri.Segments.Last();
fileName = HttpUtility.UrlDecode(fileName);
}
var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();
if (matchingTorrentFiles.Count > 0)
{
var matchingTorrentFile = matchingTorrentFiles[0];
var subPath = Path.GetDirectoryName(matchingTorrentFile.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;
}
}
var filePath = Path.Combine(torrentPath, fileName);
return filePath;
} }
private static String RemoveInvalidPathChars(String path) private static String RemoveInvalidPathChars(String path)