From ccce4f5cc0d43888c28fc771dba6febfff05bba4 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Wed, 18 Sep 2024 01:26:48 +0300 Subject: [PATCH] New: Show warning in queue if download contains executable or archive file and no audio file was detected (#5106) * Improve handling of releases without audio files New: Show warning in queue if download contains executable or archive file and no audio file was detected (cherry picked from commit b15b6a079846b21cac8476820fce9cde81732291) * New: Add additional archive exentions (cherry picked from commit 750a9353f82da4e016bee25e0c625cd6d8613b57) --------- Co-authored-by: Mark McDowall --- .../DownloadedTracksImportServiceFixture.cs | 68 +++++++++++++++++++ .../Download/CompletedDownloadService.cs | 12 ++++ .../DownloadedTracksImportService.cs | 32 ++++++++- .../MediaFiles/FileExtensions.cs | 36 ++++++++++ 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/NzbDrone.Core/MediaFiles/FileExtensions.cs diff --git a/src/NzbDrone.Core.Test/MediaFiles/DownloadedTracksImportServiceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/DownloadedTracksImportServiceFixture.cs index 555a7492c..9b433b553 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/DownloadedTracksImportServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/DownloadedTracksImportServiceFixture.cs @@ -336,6 +336,74 @@ public void should_not_delete_folder_if_importmode_copy() DiskProvider.FolderExists(_subFolders[0]).Should().BeTrue(); } + [Test] + public void should_return_rejection_if_nothing_imported_and_contains_rar_file() + { + GivenValidArtist(); + + var path = @"C:\Test\Unsorted\Artist.Title-Album.Title.2017-Lidarr".AsOsAgnostic(); + var imported = new List>(); + + Mocker.GetMock() + .Setup(s => s.FolderExists(path)) + .Returns(true); + + Mocker.GetMock() + .Setup(s => s.GetDirectoryInfo(It.IsAny())) + .Returns(DiskProvider.GetDirectoryInfo(path)); + + Mocker.GetMock() + .Setup(v => v.GetImportDecisions(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(imported); + + Mocker.GetMock() + .Setup(s => s.Import(It.IsAny>>(), true, null, ImportMode.Auto)) + .Returns(imported.Select(i => new ImportResult(i)).ToList()); + + Mocker.GetMock() + .Setup(s => s.GetFiles(It.IsAny(), true)) + .Returns(new[] { _audioFiles.First().Replace(".ext", ".rar") }); + + var result = Subject.ProcessPath(path); + + result.Count.Should().Be(1); + result.First().Result.Should().Be(ImportResultType.Rejected); + } + + [Test] + public void should_return_rejection_if_nothing_imported_and_contains_executable_file() + { + GivenValidArtist(); + + var path = @"C:\Test\Unsorted\Artist.Title-Album.Title.2017-Lidarr".AsOsAgnostic(); + var imported = new List>(); + + Mocker.GetMock() + .Setup(s => s.FolderExists(path)) + .Returns(true); + + Mocker.GetMock() + .Setup(s => s.GetDirectoryInfo(It.IsAny())) + .Returns(DiskProvider.GetDirectoryInfo(path)); + + Mocker.GetMock() + .Setup(v => v.GetImportDecisions(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(imported); + + Mocker.GetMock() + .Setup(s => s.Import(It.IsAny>>(), true, null, ImportMode.Auto)) + .Returns(imported.Select(i => new ImportResult(i)).ToList()); + + Mocker.GetMock() + .Setup(s => s.GetFiles(It.IsAny(), true)) + .Returns(new[] { _audioFiles.First().Replace(".ext", ".exe") }); + + var result = Subject.ProcessPath(path); + + result.Count.Should().Be(1); + result.First().Result.Should().Be(ImportResultType.Rejected); + } + private void VerifyNoImport() { Mocker.GetMock().Verify(c => c.Import(It.IsAny>>(), true, null, ImportMode.Auto), diff --git a/src/NzbDrone.Core/Download/CompletedDownloadService.cs b/src/NzbDrone.Core/Download/CompletedDownloadService.cs index 0f27bda99..eca048e78 100644 --- a/src/NzbDrone.Core/Download/CompletedDownloadService.cs +++ b/src/NzbDrone.Core/Download/CompletedDownloadService.cs @@ -135,6 +135,18 @@ public void Import(TrackedDownload trackedDownload) return; } + if (importResults.Count == 1) + { + var firstResult = importResults.First(); + + if (firstResult.Result == ImportResultType.Rejected && firstResult.ImportDecision.Item == null) + { + trackedDownload.Warn(new TrackedDownloadStatusMessage(firstResult.Errors.First(), new List())); + + return; + } + } + var statusMessages = new List { new TrackedDownloadStatusMessage("One or more albums expected in this release were not imported or missing", new List()) diff --git a/src/NzbDrone.Core/MediaFiles/DownloadedTracksImportService.cs b/src/NzbDrone.Core/MediaFiles/DownloadedTracksImportService.cs index bf64d64fa..b3ea932d8 100644 --- a/src/NzbDrone.Core/MediaFiles/DownloadedTracksImportService.cs +++ b/src/NzbDrone.Core/MediaFiles/DownloadedTracksImportService.cs @@ -177,10 +177,12 @@ private List ProcessFolder(IDirectoryInfo directoryInfo, ImportMod if (_artistService.ArtistPathExists(directoryInfo.FullName)) { _logger.Warn("Unable to process folder that is mapped to an existing artist"); - return new List(); + return new List + { + RejectionResult("Import path is mapped to an artist folder") + }; } - var cleanedUpName = GetCleanedUpFolderName(directoryInfo.Name); var folderInfo = Parser.Parser.ParseAlbumTitle(directoryInfo.Name); var audioFiles = _diskScanService.FilterFiles(directoryInfo.FullName, _diskScanService.GetAudioFiles(directoryInfo.FullName)); @@ -240,6 +242,10 @@ private List ProcessFolder(IDirectoryInfo directoryInfo, ImportMod _logger.Debug(e, "Unable to delete folder after importing: {0}", e.Message); } } + else if (importResults.Empty()) + { + importResults.AddIfNotNull(CheckEmptyResultForIssue(directoryInfo.FullName)); + } return importResults; } @@ -341,6 +347,28 @@ private ImportResult UnknownArtistResult(string message, string audioFile = null return new ImportResult(new ImportDecision(localTrack, new Rejection("Unknown Artist")), message); } + private ImportResult RejectionResult(string message) + { + return new ImportResult(new ImportDecision(null, new Rejection(message)), message); + } + + private ImportResult CheckEmptyResultForIssue(string folder) + { + var files = _diskProvider.GetFiles(folder, true).ToList(); + + if (files.Any(file => FileExtensions.ExecutableExtensions.Contains(Path.GetExtension(file)))) + { + return RejectionResult("Caution: Found executable file"); + } + + if (files.Any(file => FileExtensions.ArchiveExtensions.Contains(Path.GetExtension(file)))) + { + return RejectionResult("Found archive file, might need to be extracted"); + } + + return null; + } + private void LogInaccessiblePathError(string path) { if (_runtimeInfo.IsWindowsService) diff --git a/src/NzbDrone.Core/MediaFiles/FileExtensions.cs b/src/NzbDrone.Core/MediaFiles/FileExtensions.cs new file mode 100644 index 000000000..77d787645 --- /dev/null +++ b/src/NzbDrone.Core/MediaFiles/FileExtensions.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; + +namespace NzbDrone.Core.MediaFiles +{ + internal static class FileExtensions + { + private static List _archiveExtensions = new List + { + ".7z", + ".bz2", + ".gz", + ".r00", + ".rar", + ".tar.bz2", + ".tar.gz", + ".tar", + ".tb2", + ".tbz2", + ".tgz", + ".zip", + ".zipx" + }; + + private static List _executableExtensions = new List + { + ".exe", + ".bat", + ".cmd", + ".sh" + }; + + public static HashSet ArchiveExtensions => new HashSet(_archiveExtensions, StringComparer.OrdinalIgnoreCase); + public static HashSet ExecutableExtensions => new HashSet(_executableExtensions, StringComparer.OrdinalIgnoreCase); + } +}