Changed how files are saved.
This commit is contained in:
parent
d43774d9b5
commit
d5ee5d209c
1 changed files with 34 additions and 60 deletions
|
|
@ -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;
|
||||
|
||||
|
|
@ -30,31 +29,51 @@ 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 (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))
|
||||
{
|
||||
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))
|
||||
if (!String.IsNullOrWhiteSpace(torrentPath) && !Directory.Exists(torrentPath))
|
||||
{
|
||||
Directory.CreateDirectory(torrentPath);
|
||||
}
|
||||
|
|
@ -66,52 +85,7 @@ public static class DownloadHelper
|
|||
|
||||
public static String? GetDownloadPath(Torrent torrent, Download download)
|
||||
{
|
||||
var fileUrl = download.Link;
|
||||
|
||||
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;
|
||||
return GetDownloadPath("", torrent, download);
|
||||
}
|
||||
|
||||
private static String RemoveInvalidPathChars(String path)
|
||||
|
|
|
|||
Loading…
Reference in a new issue