From 9e7f0c859f91332e9377a0ea279d4385cc6d0a53 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 11 Feb 2018 09:01:41 -0800 Subject: [PATCH] New: Opt-in to delete empty series/season folders --- .../Config/MediaManagementConfigResource.cs | 4 +++- .../Configuration/ConfigService.cs | 9 +++++++- .../Configuration/IConfigService.cs | 3 ++- .../MediaFiles/DiskScanService.cs | 12 +++++++--- .../MediaFiles/MediaFileDeletionService.cs | 21 +++++++++++++---- .../Sorting/SortingViewTemplate.hbs | 23 +++++++++++++++++++ 6 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/NzbDrone.Api/Config/MediaManagementConfigResource.cs b/src/NzbDrone.Api/Config/MediaManagementConfigResource.cs index fb6ef94e5..b10d8209f 100644 --- a/src/NzbDrone.Api/Config/MediaManagementConfigResource.cs +++ b/src/NzbDrone.Api/Config/MediaManagementConfigResource.cs @@ -1,4 +1,4 @@ -using NzbDrone.Api.REST; +using NzbDrone.Api.REST; using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles; @@ -10,6 +10,7 @@ namespace NzbDrone.Api.Config public string RecycleBin { get; set; } public bool AutoDownloadPropers { get; set; } public bool CreateEmptySeriesFolders { get; set; } + public bool DeleteEmptyFolders { get; set; } public FileDateType FileDate { get; set; } public bool SetPermissionsLinux { get; set; } @@ -35,6 +36,7 @@ namespace NzbDrone.Api.Config RecycleBin = model.RecycleBin, AutoDownloadPropers = model.AutoDownloadPropers, CreateEmptySeriesFolders = model.CreateEmptySeriesFolders, + DeleteEmptyFolders = model.DeleteEmptyFolders, FileDate = model.FileDate, SetPermissionsLinux = model.SetPermissionsLinux, diff --git a/src/NzbDrone.Core/Configuration/ConfigService.cs b/src/NzbDrone.Core/Configuration/ConfigService.cs index 3da6a13a3..d85151f6d 100644 --- a/src/NzbDrone.Core/Configuration/ConfigService.cs +++ b/src/NzbDrone.Core/Configuration/ConfigService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -161,6 +161,13 @@ namespace NzbDrone.Core.Configuration set { SetValue("CreateEmptySeriesFolders", value); } } + public bool DeleteEmptyFolders + { + get { return GetValueBoolean("DeleteEmptyFolders", false); } + + set { SetValue("DeleteEmptyFolders", value); } + } + public FileDateType FileDate { get { return GetValueEnum("FileDate", FileDateType.None); } diff --git a/src/NzbDrone.Core/Configuration/IConfigService.cs b/src/NzbDrone.Core/Configuration/IConfigService.cs index 9f781becd..7b4080148 100644 --- a/src/NzbDrone.Core/Configuration/IConfigService.cs +++ b/src/NzbDrone.Core/Configuration/IConfigService.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using NzbDrone.Core.MediaFiles; using NzbDrone.Common.Http.Proxy; @@ -28,6 +28,7 @@ namespace NzbDrone.Core.Configuration string RecycleBin { get; set; } bool AutoDownloadPropers { get; set; } bool CreateEmptySeriesFolders { get; set; } + bool DeleteEmptyFolders { get; set; } FileDateType FileDate { get; set; } bool SkipFreeSpaceCheckWhenImporting { get; set; } bool CopyUsingHardlinks { get; set; } diff --git a/src/NzbDrone.Core/MediaFiles/DiskScanService.cs b/src/NzbDrone.Core/MediaFiles/DiskScanService.cs index 38eee0547..d4f3b97a9 100644 --- a/src/NzbDrone.Core/MediaFiles/DiskScanService.cs +++ b/src/NzbDrone.Core/MediaFiles/DiskScanService.cs @@ -191,10 +191,16 @@ namespace NzbDrone.Core.MediaFiles private void RemoveEmptySeriesFolder(string path) { - if (_diskProvider.GetFiles(path, SearchOption.AllDirectories).Empty() && - !_configService.CreateEmptySeriesFolders) + if (_configService.DeleteEmptyFolders) { - _diskProvider.DeleteFolder(path, true); + if (_diskProvider.GetFiles(path, SearchOption.AllDirectories).Empty()) + { + _diskProvider.DeleteFolder(path, true); + } + else + { + _diskProvider.RemoveEmptySubfolders(path); + } } } diff --git a/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs b/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs index e875a97d2..15a8ab5dc 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaFileDeletionService.cs @@ -117,13 +117,24 @@ namespace NzbDrone.Core.MediaFiles [EventHandleOrder(EventHandleOrder.Last)] public void Handle(EpisodeFileDeletedEvent message) { - var series = message.EpisodeFile.Series.Value; - var path = series.Path; + if (message.Reason == DeleteMediaFileReason.Upgrade) + { + return; + } - if (_diskProvider.GetFiles(path, SearchOption.AllDirectories).Empty() && - !_configService.CreateEmptySeriesFolders) + if (_configService.DeleteEmptyFolders) { - _diskProvider.DeleteFolder(path, true); + var series = message.EpisodeFile.Series.Value; + var seasonFolder = message.EpisodeFile.Path.GetParentPath(); + + if (_diskProvider.GetFiles(series.Path, SearchOption.AllDirectories).Empty()) + { + _diskProvider.DeleteFolder(series.Path, true); + } + else if (_diskProvider.GetFiles(seasonFolder, SearchOption.AllDirectories).Empty()) + { + _diskProvider.RemoveEmptySubfolders(seasonFolder); + } } } } diff --git a/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs b/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs index f3612203f..dc62a7e4d 100644 --- a/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs +++ b/src/UI/Settings/MediaManagement/Sorting/SortingViewTemplate.hbs @@ -23,6 +23,29 @@ + +
+ + +
+
+
+