2012-09-04 08:49:04 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-04-24 03:56:00 +02:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-02-24 07:48:52 +01:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2013-05-07 02:39:33 +02:00
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-03-05 20:49:34 +01:00
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
2012-09-04 08:49:04 +02:00
|
|
|
|
|
2013-05-07 02:39:33 +02:00
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
2013-07-16 04:56:46 +02:00
|
|
|
|
public interface IRecycleBinProvider
|
|
|
|
|
{
|
|
|
|
|
void DeleteFolder(string path);
|
|
|
|
|
void DeleteFile(string path);
|
|
|
|
|
void Empty();
|
|
|
|
|
void Cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RecycleBinProvider : IHandleAsync<SeriesDeletedEvent>, IExecute<CleanUpRecycleBinCommand>, IRecycleBinProvider
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-02-24 07:48:52 +01:00
|
|
|
|
private readonly IConfigService _configService;
|
2012-09-04 08:49:04 +02:00
|
|
|
|
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public RecycleBinProvider(IDiskProvider diskProvider, IConfigService configService)
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
2013-02-24 07:48:52 +01:00
|
|
|
|
_configService = configService;
|
2012-09-04 08:49:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 04:56:46 +02:00
|
|
|
|
public void DeleteFolder(string path)
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Trace("Attempting to send '{0}' to recycling bin", path);
|
2013-02-24 07:48:52 +01:00
|
|
|
|
var recyclingBin = _configService.RecycleBin;
|
2012-09-04 08:49:04 +02:00
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(recyclingBin))
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Recycling Bin has not been configured, deleting permanently.");
|
|
|
|
|
_diskProvider.DeleteFolder(path, true);
|
|
|
|
|
logger.Trace("Folder has been permanently deleted: {0}", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var destination = Path.Combine(recyclingBin, new DirectoryInfo(path).Name);
|
|
|
|
|
|
|
|
|
|
logger.Trace("Moving '{0}' to '{1}'", path, destination);
|
2013-07-05 06:43:28 +02:00
|
|
|
|
_diskProvider.MoveFolder(path, destination);
|
2012-09-04 08:49:04 +02:00
|
|
|
|
|
|
|
|
|
logger.Trace("Setting last accessed: {0}", path);
|
2013-07-05 06:43:28 +02:00
|
|
|
|
_diskProvider.FolderSetLastWriteTimeUtc(destination, DateTime.UtcNow);
|
2013-03-05 20:49:34 +01:00
|
|
|
|
foreach (var file in _diskProvider.GetFiles(destination, SearchOption.AllDirectories))
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
_diskProvider.FileSetLastWriteTimeUtc(file, DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Trace("Folder has been moved to the recycling bin: {0}", destination);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 04:56:46 +02:00
|
|
|
|
public void DeleteFile(string path)
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Trace("Attempting to send '{0}' to recycling bin", path);
|
2013-02-24 07:48:52 +01:00
|
|
|
|
var recyclingBin = _configService.RecycleBin;
|
2012-09-04 08:49:04 +02:00
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(recyclingBin))
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Recycling Bin has not been configured, deleting permanently.");
|
|
|
|
|
_diskProvider.DeleteFile(path);
|
|
|
|
|
logger.Trace("File has been permanently deleted: {0}", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var destination = Path.Combine(recyclingBin, new FileInfo(path).Name);
|
|
|
|
|
|
|
|
|
|
logger.Trace("Moving '{0}' to '{1}'", path, destination);
|
|
|
|
|
_diskProvider.MoveFile(path, destination);
|
|
|
|
|
_diskProvider.FileSetLastWriteTimeUtc(destination, DateTime.UtcNow);
|
|
|
|
|
logger.Trace("File has been moved to the recycling bin: {0}", destination);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 04:56:46 +02:00
|
|
|
|
public void Empty()
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
2013-02-24 07:48:52 +01:00
|
|
|
|
if (String.IsNullOrWhiteSpace(_configService.RecycleBin))
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Recycle Bin has not been configured, cannot empty.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info("Removing all items from the recycling bin");
|
|
|
|
|
|
2013-02-24 07:48:52 +01:00
|
|
|
|
foreach (var folder in _diskProvider.GetDirectories(_configService.RecycleBin))
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFolder(folder, true);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 07:48:52 +01:00
|
|
|
|
foreach (var file in _diskProvider.GetFiles(_configService.RecycleBin, SearchOption.TopDirectoryOnly))
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFile(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Trace("Recycling Bin has been emptied.");
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 04:56:46 +02:00
|
|
|
|
public void Cleanup()
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
2013-02-24 07:48:52 +01:00
|
|
|
|
if (String.IsNullOrWhiteSpace(_configService.RecycleBin))
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Recycle Bin has not been configured, cannot cleanup.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info("Removing items older than 7 days from the recycling bin");
|
|
|
|
|
|
2013-02-24 07:48:52 +01:00
|
|
|
|
foreach (var folder in _diskProvider.GetDirectories(_configService.RecycleBin))
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
|
if (_diskProvider.GetLastFolderWrite(folder).AddDays(7) > DateTime.UtcNow)
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Trace("Folder hasn't expired yet, skipping: {0}", folder);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-03-05 20:49:34 +01:00
|
|
|
|
|
|
|
|
|
_diskProvider.DeleteFolder(folder, true);
|
2012-09-04 08:49:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 07:48:52 +01:00
|
|
|
|
foreach (var file in _diskProvider.GetFiles(_configService.RecycleBin, SearchOption.TopDirectoryOnly))
|
2012-09-04 08:49:04 +02:00
|
|
|
|
{
|
|
|
|
|
if (_diskProvider.GetLastFileWrite(file).AddDays(7) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
logger.Trace("File hasn't expired yet, skipping: {0}", file);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-03-05 20:49:34 +01:00
|
|
|
|
|
2012-09-04 08:49:04 +02:00
|
|
|
|
_diskProvider.DeleteFile(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Trace("Recycling Bin has been cleaned up.");
|
|
|
|
|
}
|
2013-03-05 20:49:34 +01:00
|
|
|
|
|
|
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
|
{
|
2013-05-07 02:39:33 +02:00
|
|
|
|
if (message.DeleteFiles)
|
|
|
|
|
{
|
2013-07-05 06:43:28 +02:00
|
|
|
|
DeleteFolder(message.Series.Path);
|
2013-05-07 02:39:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Execute(CleanUpRecycleBinCommand message)
|
|
|
|
|
{
|
|
|
|
|
Cleanup();
|
2013-03-05 20:49:34 +01:00
|
|
|
|
}
|
2012-09-04 08:49:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|