2012-09-04 06:49:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2014-01-06 06:20:08 +00:00
|
|
|
|
using NzbDrone.Common.Disk;
|
2013-07-30 00:09:48 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-08-31 01:42:30 +00:00
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
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;
|
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-03-05 19:49:34 +00:00
|
|
|
|
using NzbDrone.Core.Tv.Events;
|
2012-09-04 06:49:04 +00:00
|
|
|
|
|
2013-05-07 00:39:33 +00:00
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2013-07-16 02:56:46 +00: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 06:49:04 +00:00
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-02-24 06:48:52 +00:00
|
|
|
|
private readonly IConfigService _configService;
|
2012-09-04 06:49:04 +00:00
|
|
|
|
|
2013-08-31 01:42:30 +00:00
|
|
|
|
private static readonly Logger logger = NzbDroneLogger.GetLogger();
|
2012-09-04 06:49:04 +00:00
|
|
|
|
|
2013-05-10 23:53:50 +00:00
|
|
|
|
public RecycleBinProvider(IDiskProvider diskProvider, IConfigService configService)
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
2013-02-24 06:48:52 +00:00
|
|
|
|
_configService = configService;
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 02:56:46 +00:00
|
|
|
|
public void DeleteFolder(string path)
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2013-07-31 05:11:54 +00:00
|
|
|
|
logger.Info("Attempting to send '{0}' to recycling bin", path);
|
2013-02-24 06:48:52 +00:00
|
|
|
|
var recyclingBin = _configService.RecycleBin;
|
2012-09-04 06:49:04 +00:00
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(recyclingBin))
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Recycling Bin has not been configured, deleting permanently.");
|
|
|
|
|
_diskProvider.DeleteFolder(path, true);
|
2013-07-31 05:11:54 +00:00
|
|
|
|
logger.Debug("Folder has been permanently deleted: {0}", path);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var destination = Path.Combine(recyclingBin, new DirectoryInfo(path).Name);
|
|
|
|
|
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("Moving '{0}' to '{1}'", path, destination);
|
2013-07-05 04:43:28 +00:00
|
|
|
|
_diskProvider.MoveFolder(path, destination);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("Setting last accessed: {0}", path);
|
2013-07-05 04:43:28 +00:00
|
|
|
|
_diskProvider.FolderSetLastWriteTimeUtc(destination, DateTime.UtcNow);
|
2013-03-05 19:49:34 +00:00
|
|
|
|
foreach (var file in _diskProvider.GetFiles(destination, SearchOption.AllDirectories))
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
|
|
|
|
_diskProvider.FileSetLastWriteTimeUtc(file, DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 05:11:54 +00:00
|
|
|
|
logger.Debug("Folder has been moved to the recycling bin: {0}", destination);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 02:56:46 +00:00
|
|
|
|
public void DeleteFile(string path)
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2013-07-31 05:11:54 +00:00
|
|
|
|
logger.Debug("Attempting to send '{0}' to recycling bin", path);
|
2013-02-24 06:48:52 +00:00
|
|
|
|
var recyclingBin = _configService.RecycleBin;
|
2012-09-04 06:49:04 +00:00
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(recyclingBin))
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Recycling Bin has not been configured, deleting permanently.");
|
2013-07-30 00:09:48 +00:00
|
|
|
|
|
2014-03-09 05:28:40 +00:00
|
|
|
|
if (!OsInfo.IsMono)
|
2013-07-30 00:09:48 +00:00
|
|
|
|
{
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug(_diskProvider.GetFileAttributes(path));
|
2013-07-30 00:09:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-04 06:49:04 +00:00
|
|
|
|
_diskProvider.DeleteFile(path);
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("File has been permanently deleted: {0}", path);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-07-30 22:00:55 +00:00
|
|
|
|
var fileInfo = new FileInfo(path);
|
|
|
|
|
var destination = Path.Combine(recyclingBin, fileInfo.Name);
|
|
|
|
|
|
|
|
|
|
var index = 1;
|
|
|
|
|
while (_diskProvider.FileExists(destination))
|
|
|
|
|
{
|
|
|
|
|
index++;
|
|
|
|
|
if (fileInfo.Extension.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
destination = Path.Combine(recyclingBin, fileInfo.Name + "_" + index);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
destination = Path.Combine(recyclingBin, Path.GetFileNameWithoutExtension(fileInfo.Name) + "_" + index + fileInfo.Extension);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-04 06:49:04 +00:00
|
|
|
|
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("Moving '{0}' to '{1}'", path, destination);
|
2014-07-30 22:00:55 +00:00
|
|
|
|
_diskProvider.MoveFile(path, destination, true);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
_diskProvider.FileSetLastWriteTimeUtc(destination, DateTime.UtcNow);
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("File has been moved to the recycling bin: {0}", destination);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 02:56:46 +00:00
|
|
|
|
public void Empty()
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2013-02-24 06:48:52 +00:00
|
|
|
|
if (String.IsNullOrWhiteSpace(_configService.RecycleBin))
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Recycle Bin has not been configured, cannot empty.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFolder(folder, true);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 06:48:52 +00:00
|
|
|
|
foreach (var file in _diskProvider.GetFiles(_configService.RecycleBin, SearchOption.TopDirectoryOnly))
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFile(file);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("Recycling Bin has been emptied.");
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 02:56:46 +00:00
|
|
|
|
public void Cleanup()
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2013-02-24 06:48:52 +00:00
|
|
|
|
if (String.IsNullOrWhiteSpace(_configService.RecycleBin))
|
2012-09-04 06:49:04 +00: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 06:48:52 +00:00
|
|
|
|
foreach (var folder in _diskProvider.GetDirectories(_configService.RecycleBin))
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2014-05-30 17:34:38 +00:00
|
|
|
|
if (_diskProvider.FolderGetLastWriteUtc(folder).AddDays(7) > DateTime.UtcNow)
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("Folder hasn't expired yet, skipping: {0}", folder);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-03-05 19:49:34 +00:00
|
|
|
|
|
|
|
|
|
_diskProvider.DeleteFolder(folder, true);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 06:48:52 +00:00
|
|
|
|
foreach (var file in _diskProvider.GetFiles(_configService.RecycleBin, SearchOption.TopDirectoryOnly))
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
|
if (_diskProvider.FileGetLastWriteUtc(file).AddDays(7) > DateTime.UtcNow)
|
2012-09-04 06:49:04 +00:00
|
|
|
|
{
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("File hasn't expired yet, skipping: {0}", file);
|
2012-09-04 06:49:04 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-03-05 19:49:34 +00:00
|
|
|
|
|
2012-09-04 06:49:04 +00:00
|
|
|
|
_diskProvider.DeleteFile(file);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 20:12:42 +00:00
|
|
|
|
logger.Debug("Recycling Bin has been cleaned up.");
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
2013-03-05 19:49:34 +00:00
|
|
|
|
|
|
|
|
|
public void HandleAsync(SeriesDeletedEvent message)
|
|
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
|
if (message.DeleteFiles)
|
|
|
|
|
{
|
2013-07-05 04:43:28 +00:00
|
|
|
|
DeleteFolder(message.Series.Path);
|
2013-05-07 00:39:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Execute(CleanUpRecycleBinCommand message)
|
|
|
|
|
{
|
|
|
|
|
Cleanup();
|
2013-03-05 19:49:34 +00:00
|
|
|
|
}
|
2012-09-04 06:49:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|