From 60e838cba7c44cc2b680c012fea3bcd700e0ced8 Mon Sep 17 00:00:00 2001 From: Flaminel Date: Tue, 25 Mar 2025 22:15:04 +0200 Subject: [PATCH] fixed Deluge processing for changing category --- .../DownloadClient/Deluge/DelugeService.cs | 204 ++++++++---------- 1 file changed, 91 insertions(+), 113 deletions(-) diff --git a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs index 66f32c5..7598b69 100644 --- a/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs +++ b/code/Infrastructure/Verticals/DownloadClient/Deluge/DelugeService.cs @@ -217,30 +217,42 @@ public class DelugeService : DownloadService, IDelugeService .ToList(); } - public override List? FilterDownloadsToBeCleanedAsync(List? downloads, List categories) - { - return downloads + public override List? FilterDownloadsToBeCleanedAsync(List? downloads, List categories) => + downloads ?.Cast() .Where(x => categories.Any(cat => cat.Name.Equals(x.Label, StringComparison.InvariantCultureIgnoreCase))) .Cast() .ToList(); - } - public override List? FilterDownloadsToChangeCategoryAsync(List? downloads, List categories) - { - throw new NotImplementedException(); - } + public override List? FilterDownloadsToChangeCategoryAsync(List? downloads, List categories) => + downloads + ?.Cast() + .Where(x => !string.IsNullOrEmpty(x.Hash)) + .Where(x => categories.Any(cat => cat.Equals(x.Label, StringComparison.InvariantCultureIgnoreCase))) + .Cast() + .ToList(); /// - public override async Task CleanDownloadsAsync(List downloads, List categoriesToClean, HashSet excludedHashes, + public override async Task CleanDownloadsAsync(List? downloads, List categoriesToClean, HashSet excludedHashes, IReadOnlyList ignoredDownloads) { + if (downloads?.Count is null or 0) + { + return; + } + foreach (TorrentStatus download in downloads) { if (string.IsNullOrEmpty(download.Hash)) { continue; } + + if (excludedHashes.Any(x => x.Equals(download.Hash, StringComparison.InvariantCultureIgnoreCase))) + { + _logger.LogDebug("skip | download is used by an arr | {name}", download.Name); + continue; + } if (ignoredDownloads.Count > 0 && download.ShouldIgnore(ignoredDownloads)) { @@ -255,12 +267,6 @@ public class DelugeService : DownloadService, IDelugeService { continue; } - - if (excludedHashes.Any(x => x.Equals(download.Hash, StringComparison.InvariantCultureIgnoreCase))) - { - _logger.LogDebug("skip | download is used by an arr | {name}", download.Name); - continue; - } if (!_downloadCleanerConfig.DeletePrivate && download.Private) { @@ -312,105 +318,77 @@ public class DelugeService : DownloadService, IDelugeService return; } - throw new NotImplementedException(); + if (!string.IsNullOrEmpty(_downloadCleanerConfig.UnlinkedIgnoredRootDir)) + { + _hardLinkFileService.PopulateFileCounts(_downloadCleanerConfig.UnlinkedIgnoredRootDir); + } + + foreach (TorrentStatus download in downloads.Cast()) + { + if (string.IsNullOrEmpty(download.Hash) || string.IsNullOrEmpty(download.Name) || string.IsNullOrEmpty(download.Label)) + { + continue; + } + + if (excludedHashes.Any(x => x.Equals(download.Hash, StringComparison.InvariantCultureIgnoreCase))) + { + _logger.LogDebug("skip | download is used by an arr | {name}", download.Name); + continue; + } - // if (_downloadCleanerConfig.NoHardLinksIgnoreRootDir) - // { - // downloads - // .Cast() - // .Select(x => - // { - // string? firstDir = GetRootWithFirstDirectory(x.DownloadPath); - // - // if (string.IsNullOrEmpty(firstDir)) - // { - // return string.Empty; - // } - // - // if (firstDir == Path.GetPathRoot(x.DownloadPath)) - // { - // return string.Empty; - // } - // - // return firstDir; - // }) - // .Where(x => !string.IsNullOrEmpty(x)) - // .Distinct() - // .ToList() - // .ForEach(x => - // { - // _logger.LogTrace("populating file counts from {dir}", x); - // - // if (!Directory.Exists(x)) - // { - // throw new ValidationException($"directory \"{x}\" does not exist"); - // } - // - // _hardLinkFileService.PopulateFileCounts(x); - // }); - // } - // - // foreach (TorrentStatus download in downloads.Cast()) - // { - // if (string.IsNullOrEmpty(download.Hash)) - // { - // _logger.LogDebug("skip | download hash is null for {name}", download.Name); - // continue; - // } - // - // if (excludedHashes.Any(x => x.Equals(download.Hash, StringComparison.InvariantCultureIgnoreCase))) - // { - // _logger.LogDebug("skip | download is used by an arr | {name}", download.Name); - // continue; - // } - // - // ContextProvider.Set("downloadName", download.Name); - // ContextProvider.Set("hash", download.Hash); - // - // DelugeContents? contents = null; - // try - // { - // contents = await _client.GetTorrentFiles(download.Hash); - // } - // catch (Exception exception) - // { - // _logger.LogDebug(exception, "failed to find torrent files for {name}", download.Name); - // continue; - // } - // - // bool hasHardlinks = false; - // - // ProcessFiles(contents?.Contents, (name, file) => - // { - // string filePath = Path.Combine(download.DownloadPath, file.Path); - // - // long hardlinkCount = _hardLinkFileService.GetHardLinkCount(filePath, _downloadCleanerConfig.NoHardLinksIgnoreRootDir); - // - // if (hardlinkCount < 0) - // { - // _logger.LogDebug("skip | could not get file properties | {name}", download.Name); - // hasHardlinks = true; - // return; - // } - // - // if (hardlinkCount > 0) - // { - // hasHardlinks = true; - // } - // }); - // - // if (hasHardlinks) - // { - // _logger.LogDebug("skip | download has hardlinks | {name}", download.Name); - // continue; - // } - // - // await _dryRunInterceptor.InterceptAsync(ChangeLabel, download.Hash, _downloadCleanerConfig.NoHardLinksCategory); - // - // _logger.LogInformation("category changed for {name}", download.Name); - // - // await _notifier.NotifyCategoryChanged(download.Label, _downloadCleanerConfig.NoHardLinksCategory); - // } + if (ignoredDownloads.Count > 0 && download.ShouldIgnore(ignoredDownloads)) + { + _logger.LogInformation("skip | download is ignored | {name}", download.Name); + continue; + } + + ContextProvider.Set("downloadName", download.Name); + ContextProvider.Set("hash", download.Hash); + + DelugeContents? contents = null; + try + { + contents = await _client.GetTorrentFiles(download.Hash); + } + catch (Exception exception) + { + _logger.LogDebug(exception, "failed to find torrent files for {name}", download.Name); + continue; + } + + bool hasHardlinks = false; + + ProcessFiles(contents?.Contents, (_, file) => + { + long hardlinkCount = _hardLinkFileService.GetHardLinkCount(file.Path, !string.IsNullOrEmpty(_downloadCleanerConfig.UnlinkedIgnoredRootDir)); + + if (hardlinkCount < 0) + { + _logger.LogDebug("skip | could not get file properties | {name}", download.Name); + hasHardlinks = true; + return; + } + + if (hardlinkCount > 0) + { + hasHardlinks = true; + } + }); + + if (hasHardlinks) + { + _logger.LogDebug("skip | download has hardlinks | {name}", download.Name); + continue; + } + + await _dryRunInterceptor.InterceptAsync(ChangeLabel, download.Hash, _downloadCleanerConfig.UnlinkedTargetCategory); + + _logger.LogInformation("category changed for {name}", download.Name); + + await _notifier.NotifyCategoryChanged(download.Label, _downloadCleanerConfig.UnlinkedTargetCategory); + + download.Label = _downloadCleanerConfig.UnlinkedTargetCategory; + } } ///