Fixed potential issue with duplicate categories.

This commit is contained in:
Roger Far 2021-08-05 11:29:48 -06:00
parent a8dc196dfe
commit 732e8d9691

View file

@ -198,27 +198,29 @@ namespace RdtClient.Service.Services
public String AppDefaultSavePath() public String AppDefaultSavePath()
{ {
var downloadPath = Settings.Get.MappedPath; var downloadPath = Settings.Get.MappedPath;
downloadPath = downloadPath.TrimEnd('\\') downloadPath = downloadPath.TrimEnd('\\')
.TrimEnd('/'); .TrimEnd('/');
downloadPath += Path.DirectorySeparatorChar; downloadPath += Path.DirectorySeparatorChar;
return downloadPath; return downloadPath;
} }
public async Task<IList<TorrentInfo>> TorrentInfo() public async Task<IList<TorrentInfo>> TorrentInfo()
{ {
var savePath = AppDefaultSavePath(); var savePath = AppDefaultSavePath();
var results = new List<TorrentInfo>(); var results = new List<TorrentInfo>();
var torrents = await _torrents.Get(); var torrents = await _torrents.Get();
var prio = 0; var prio = 0;
foreach (var torrent in torrents) foreach (var torrent in torrents)
{ {
var downloadPath = savePath; var downloadPath = savePath;
if (!String.IsNullOrWhiteSpace(torrent.Category)) if (!String.IsNullOrWhiteSpace(torrent.Category))
{ {
downloadPath = Path.Combine(downloadPath, torrent.Category); downloadPath = Path.Combine(downloadPath, torrent.Category);
@ -305,12 +307,13 @@ namespace RdtClient.Service.Services
{ {
result.State = "error"; result.State = "error";
} }
results.Add(result); results.Add(result);
} }
return results; return results;
} }
public async Task<IList<TorrentFileItem>> TorrentFileContents(String hash) public async Task<IList<TorrentFileItem>> TorrentFileContents(String hash)
{ {
var results = new List<TorrentFileItem>(); var results = new List<TorrentFileItem>();
@ -338,19 +341,19 @@ namespace RdtClient.Service.Services
public async Task<TorrentProperties> TorrentProperties(String hash) public async Task<TorrentProperties> TorrentProperties(String hash)
{ {
var savePath = AppDefaultSavePath(); var savePath = AppDefaultSavePath();
var torrent = await _torrents.GetByHash(hash); var torrent = await _torrents.GetByHash(hash);
if (torrent == null) if (torrent == null)
{ {
return null; return null;
} }
if (!String.IsNullOrWhiteSpace(torrent.Category)) if (!String.IsNullOrWhiteSpace(torrent.Category))
{ {
savePath = Path.Combine(savePath, torrent.Category); savePath = Path.Combine(savePath, torrent.Category);
} }
var bytesDone = torrent.RdProgress; var bytesDone = torrent.RdProgress;
var bytesTotal = torrent.RdSize; var bytesTotal = torrent.RdSize;
var speed = torrent.RdSpeed ?? 0; var speed = torrent.RdSpeed ?? 0;
@ -439,10 +442,11 @@ namespace RdtClient.Service.Services
var torrentsToGroup = torrents.Where(m => !String.IsNullOrWhiteSpace(m.Category)) var torrentsToGroup = torrents.Where(m => !String.IsNullOrWhiteSpace(m.Category))
.Select(m => m.Category.ToLower()) .Select(m => m.Category.ToLower())
.Distinct()
.ToList(); .ToList();
var categories = Settings.Get.Categories.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Distinct(); var categories = Settings.Get
.Categories
.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
torrentsToGroup.AddRange(categories); torrentsToGroup.AddRange(categories);
@ -450,7 +454,8 @@ namespace RdtClient.Service.Services
if (torrentsToGroup.Count > 0) if (torrentsToGroup.Count > 0)
{ {
results = torrentsToGroup.ToDictionary(m => m, results = torrentsToGroup.Distinct(StringComparer.CurrentCultureIgnoreCase)
.ToDictionary(m => m,
m => new TorrentCategory m => new TorrentCategory
{ {
Name = m, Name = m,
@ -471,7 +476,10 @@ namespace RdtClient.Service.Services
} }
else else
{ {
var categoryList = categoriesSetting.Split(",", StringSplitOptions.RemoveEmptyEntries).Distinct().ToList(); var categoryList = categoriesSetting
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Distinct(StringComparer.CurrentCultureIgnoreCase)
.ToList();
if (!categoryList.Contains(category)) if (!categoryList.Contains(category))
{ {
@ -493,7 +501,9 @@ namespace RdtClient.Service.Services
return; return;
} }
var categoryList = categoriesSetting.Split(",", StringSplitOptions.RemoveEmptyEntries).Distinct().ToList(); var categoryList = categoriesSetting.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Distinct(StringComparer.CurrentCultureIgnoreCase)
.ToList();
categoryList = categoryList.Where(m => m != category).ToList(); categoryList = categoryList.Where(m => m != category).ToList();
@ -502,4 +512,4 @@ namespace RdtClient.Service.Services
await _settings.UpdateString("Categories", categoriesSetting); await _settings.UpdateString("Categories", categoriesSetting);
} }
} }
} }