Lidarr/src/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs

211 lines
7.4 KiB
C#
Raw Normal View History

using System;
using System.IO;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
2013-02-24 06:48:52 +00:00
using NzbDrone.Core.Configuration;
2013-05-07 00:39:33 +00:00
using NzbDrone.Core.MediaFiles.Commands;
2013-09-14 06:42:09 +00:00
using NzbDrone.Core.Messaging.Commands;
2013-05-07 00:39:33 +00:00
namespace NzbDrone.Core.MediaFiles
{
public interface IRecycleBinProvider
{
void DeleteFolder(string path);
void DeleteFile(string path, string subfolder = "");
void Empty();
void Cleanup();
}
public class RecycleBinProvider : IExecute<CleanUpRecycleBinCommand>, IRecycleBinProvider
{
private readonly IDiskTransferService _diskTransferService;
2013-05-10 23:53:50 +00:00
private readonly IDiskProvider _diskProvider;
2013-02-24 06:48:52 +00:00
private readonly IConfigService _configService;
2014-12-17 07:12:26 +00:00
private readonly Logger _logger;
public RecycleBinProvider(IDiskTransferService diskTransferService,
IDiskProvider diskProvider,
IConfigService configService,
Logger logger)
{
_diskTransferService = diskTransferService;
_diskProvider = diskProvider;
2013-02-24 06:48:52 +00:00
_configService = configService;
2014-12-17 07:12:26 +00:00
_logger = logger;
}
public void DeleteFolder(string path)
{
2014-12-17 07:12:26 +00:00
_logger.Info("Attempting to send '{0}' to recycling bin", path);
2013-02-24 06:48:52 +00:00
var recyclingBin = _configService.RecycleBin;
if (string.IsNullOrWhiteSpace(recyclingBin))
{
2014-12-17 07:12:26 +00:00
_logger.Info("Recycling Bin has not been configured, deleting permanently. {0}", path);
_diskProvider.DeleteFolder(path, true);
2014-12-17 07:12:26 +00:00
_logger.Debug("Folder has been permanently deleted: {0}", path);
}
else
{
var destination = Path.Combine(recyclingBin, new DirectoryInfo(path).Name);
2014-12-17 07:12:26 +00:00
_logger.Debug("Moving '{0}' to '{1}'", path, destination);
_diskTransferService.TransferFolder(path, destination, TransferMode.Move);
2014-12-17 07:12:26 +00:00
_logger.Debug("Setting last accessed: {0}", path);
_diskProvider.FolderSetLastWriteTime(destination, DateTime.UtcNow);
foreach (var file in _diskProvider.GetFiles(destination, SearchOption.AllDirectories))
{
SetLastWriteTime(file, DateTime.UtcNow);
}
2014-12-17 07:12:26 +00:00
_logger.Debug("Folder has been moved to the recycling bin: {0}", destination);
}
}
public void DeleteFile(string path, string subfolder = "")
{
2014-12-17 07:12:26 +00:00
_logger.Debug("Attempting to send '{0}' to recycling bin", path);
2013-02-24 06:48:52 +00:00
var recyclingBin = _configService.RecycleBin;
if (string.IsNullOrWhiteSpace(recyclingBin))
{
2014-12-17 07:12:26 +00:00
_logger.Info("Recycling Bin has not been configured, deleting permanently. {0}", path);
if (OsInfo.IsWindows)
{
2014-12-17 07:12:26 +00:00
_logger.Debug(_diskProvider.GetFileAttributes(path));
}
_diskProvider.DeleteFile(path);
2014-12-17 07:12:26 +00:00
_logger.Debug("File has been permanently deleted: {0}", path);
}
else
{
var fileInfo = new FileInfo(path);
var destinationFolder = Path.Combine(recyclingBin, subfolder);
var destination = Path.Combine(destinationFolder, fileInfo.Name);
try
{
_logger.Debug("Creating folder [0]", destinationFolder);
_diskProvider.CreateFolder(destinationFolder);
}
catch (IOException e)
{
_logger.Error(e, "Unable to create the folder '{0}' in the recycling bin for the file '{1}'", destinationFolder, fileInfo.Name);
throw;
}
var index = 1;
while (_diskProvider.FileExists(destination))
{
index++;
if (fileInfo.Extension.IsNullOrWhiteSpace())
{
destination = Path.Combine(destinationFolder, fileInfo.Name + "_" + index);
}
else
{
destination = Path.Combine(destinationFolder, Path.GetFileNameWithoutExtension(fileInfo.Name) + "_" + index + fileInfo.Extension);
}
}
try
{
_logger.Debug("Moving '{0}' to '{1}'", path, destination);
_diskTransferService.TransferFile(path, destination, TransferMode.Move);
}
catch (IOException e)
{
2017-01-05 23:32:17 +00:00
_logger.Error(e, "Unable to move '{0}' to the recycling bin: '{1}'", path, destination);
throw;
}
SetLastWriteTime(destination, DateTime.UtcNow);
2014-12-17 07:12:26 +00:00
_logger.Debug("File has been moved to the recycling bin: {0}", destination);
}
}
public void Empty()
{
if (string.IsNullOrWhiteSpace(_configService.RecycleBin))
{
2014-12-17 07:12:26 +00:00
_logger.Info("Recycle Bin has not been configured, cannot empty.");
return;
}
2014-12-17 07:12:26 +00:00
_logger.Info("Removing all items from the recycling bin");
2013-02-24 06:48:52 +00:00
foreach (var folder in _diskProvider.GetDirectories(_configService.RecycleBin))
{
_diskProvider.DeleteFolder(folder, true);
}
2013-02-24 06:48:52 +00:00
foreach (var file in _diskProvider.GetFiles(_configService.RecycleBin, SearchOption.TopDirectoryOnly))
{
_diskProvider.DeleteFile(file);
}
2014-12-17 07:12:26 +00:00
_logger.Debug("Recycling Bin has been emptied.");
}
public void Cleanup()
{
if (string.IsNullOrWhiteSpace(_configService.RecycleBin))
{
2014-12-17 07:12:26 +00:00
_logger.Info("Recycle Bin has not been configured, cannot cleanup.");
return;
}
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(cleanupDays) > DateTime.UtcNow)
{
2014-12-17 07:12:26 +00:00
_logger.Debug("File hasn't expired yet, skipping: {0}", file);
continue;
}
_diskProvider.DeleteFile(file);
}
_diskProvider.RemoveEmptySubfolders(_configService.RecycleBin);
2014-12-17 07:12:26 +00:00
_logger.Debug("Recycling Bin has been cleaned up.");
}
private void SetLastWriteTime(string file, DateTime dateTime)
{
// Swallow any IOException that may be thrown due to "Invalid parameter"
try
{
_diskProvider.FileSetLastWriteTime(file, dateTime);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
2013-05-07 00:39:33 +00:00
public void Execute(CleanUpRecycleBinCommand message)
{
Cleanup();
}
}
}