New: Cleanup Recycling Bin folders older than X days (0 to disable)

This commit is contained in:
Mark McDowall 2019-08-11 00:22:26 -07:00
parent d72b16531b
commit 8196f6b9db
7 changed files with 48 additions and 4 deletions

View File

@ -316,11 +316,27 @@ class MediaManagement extends Component {
type={inputTypes.PATH}
name="recycleBin"
helpText="Episode files will go here when deleted instead of being permanently deleted"
helpTextWarning="Files in the recycle bin older than a week will be cleaned up automatically"
onChange={onInputChange}
{...settings.recycleBin}
/>
</FormGroup>
<FormGroup
advancedSettings={advancedSettings}
isAdvanced={true}
>
<FormLabel>Recycling Bin Cleanup</FormLabel>
<FormInputGroup
type={inputTypes.NUMBER}
name="recycleBinCleanupDays"
helpText="Set to 0 to disable automatic cleanup"
helpTextWarning="Files in the recycle bin older than the selected number of days will be cleaned up automatically"
min={0}
onChange={onInputChange}
{...settings.recycleBinCleanupDays}
/>
</FormGroup>
</FieldSet>
{

View File

@ -37,6 +37,7 @@ namespace NzbDrone.Core.Test.ProviderTests.RecycleBinProviderTests
public void Setup()
{
Mocker.GetMock<IConfigService>().SetupGet(s => s.RecycleBin).Returns(RecycleBin);
Mocker.GetMock<IConfigService>().SetupGet(s => s.RecycleBinCleanupDays).Returns(7);
Mocker.GetMock<IDiskProvider>().Setup(s => s.GetDirectories(RecycleBin))
.Returns(new [] { @"C:\Test\RecycleBin\Folder1", @"C:\Test\RecycleBin\Folder2", @"C:\Test\RecycleBin\Folder3" });
@ -55,6 +56,16 @@ namespace NzbDrone.Core.Test.ProviderTests.RecycleBinProviderTests
Mocker.GetMock<IDiskProvider>().Verify(v => v.GetDirectories(It.IsAny<string>()), Times.Never());
}
[Test]
public void should_return_if_recycleBinCleanupDays_is_zero()
{
Mocker.GetMock<IConfigService>().SetupGet(s => s.RecycleBinCleanupDays).Returns(0);
Mocker.Resolve<RecycleBinProvider>().Cleanup();
Mocker.GetMock<IDiskProvider>().Verify(v => v.GetDirectories(It.IsAny<string>()), Times.Never());
}
[Test]
public void should_delete_all_expired_files()
{

View File

@ -94,6 +94,12 @@ namespace NzbDrone.Core.Configuration
set { SetValue("RecycleBin", value); }
}
public int RecycleBinCleanupDays
{
get { return GetValueInt("RecycleBinCleanupDays", 7); }
set { SetValue("RecycleBinCleanupDays", value); }
}
public int RssSyncInterval
{
get { return GetValueInt("RssSyncInterval", 15); }

View File

@ -27,6 +27,7 @@ namespace NzbDrone.Core.Configuration
//Media Management
bool AutoUnmonitorPreviouslyDownloadedEpisodes { get; set; }
string RecycleBin { get; set; }
int RecycleBinCleanupDays { get; set; }
ProperDownloadTypes DownloadPropersAndRepacks { get; set; }
bool CreateEmptySeriesFolders { get; set; }
bool DeleteEmptyFolders { get; set; }
@ -39,7 +40,6 @@ namespace NzbDrone.Core.Configuration
RescanAfterRefreshType RescanAfterRefresh { get; set; }
EpisodeTitleRequiredType EpisodeTitleRequired { get; set; }
//Permissions (Media Management)
bool SetPermissionsLinux { get; set; }
string FileChmod { get; set; }

View File

@ -157,11 +157,19 @@ namespace NzbDrone.Core.MediaFiles
return;
}
_logger.Info("Removing items older than 7 days from the recycling bin");
var cleanupDays = _configService.RecycleBinCleanupDays;
if (cleanupDays == 0)
{
_logger.Info("Automatic cleanup of Recycle Bin is disabled");
return;
}
_logger.Info("Removing items older than {0} days from the recycling bin", cleanupDays);
foreach (var file in _diskProvider.GetFiles(_configService.RecycleBin, SearchOption.AllDirectories))
{
if (_diskProvider.FileGetLastWrite(file).AddDays(7) > DateTime.UtcNow)
if (_diskProvider.FileGetLastWrite(file).AddDays(cleanupDays) > DateTime.UtcNow)
{
_logger.Debug("File hasn't expired yet, skipping: {0}", file);
continue;

View File

@ -9,6 +9,7 @@ namespace Sonarr.Api.V3.Config
public MediaManagementConfigModule(IConfigService configService, PathExistsValidator pathExistsValidator)
: base(configService)
{
SharedValidator.RuleFor(c => c.RecycleBinCleanupDays).GreaterThanOrEqualTo(0);
SharedValidator.RuleFor(c => c.FileChmod).NotEmpty();
SharedValidator.RuleFor(c => c.FolderChmod).NotEmpty();
SharedValidator.RuleFor(c => c.RecycleBin).IsValidPath().SetValidator(pathExistsValidator).When(c => !string.IsNullOrWhiteSpace(c.RecycleBin));

View File

@ -10,6 +10,7 @@ namespace Sonarr.Api.V3.Config
{
public bool AutoUnmonitorPreviouslyDownloadedEpisodes { get; set; }
public string RecycleBin { get; set; }
public int RecycleBinCleanupDays { get; set; }
public ProperDownloadTypes DownloadPropersAndRepacks { get; set; }
public bool CreateEmptySeriesFolders { get; set; }
public bool DeleteEmptyFolders { get; set; }
@ -38,6 +39,7 @@ namespace Sonarr.Api.V3.Config
{
AutoUnmonitorPreviouslyDownloadedEpisodes = model.AutoUnmonitorPreviouslyDownloadedEpisodes,
RecycleBin = model.RecycleBin,
RecycleBinCleanupDays = model.RecycleBinCleanupDays,
DownloadPropersAndRepacks = model.DownloadPropersAndRepacks,
CreateEmptySeriesFolders = model.CreateEmptySeriesFolders,
DeleteEmptyFolders = model.DeleteEmptyFolders,