2013-02-04 04:18:59 +00:00
|
|
|
using System;
|
2011-04-27 15:34:53 +00:00
|
|
|
using System.Collections.Generic;
|
2011-05-20 03:47:07 +00:00
|
|
|
using System.IO;
|
2020-01-03 12:49:24 +00:00
|
|
|
using System.Linq;
|
2019-08-17 03:54:03 +00:00
|
|
|
using System.Threading.Tasks;
|
2011-05-20 03:47:07 +00:00
|
|
|
using NLog;
|
2011-11-13 04:07:06 +00:00
|
|
|
using NzbDrone.Common;
|
2014-01-06 06:20:08 +00:00
|
|
|
using NzbDrone.Common.Disk;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2020-02-09 19:15:43 +00:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2011-03-09 07:40:48 +00:00
|
|
|
|
2013-02-04 04:18:59 +00:00
|
|
|
namespace NzbDrone.Core.RootFolders
|
2011-03-09 07:40:48 +00:00
|
|
|
{
|
2013-02-04 04:18:59 +00:00
|
|
|
public interface IRootFolderService
|
|
|
|
{
|
2013-02-05 04:07:07 +00:00
|
|
|
List<RootFolder> All();
|
2020-02-09 19:15:43 +00:00
|
|
|
List<RootFolder> AllWithSpaceStats();
|
|
|
|
RootFolder Add(RootFolder rootFolder);
|
|
|
|
RootFolder Update(RootFolder rootFolder);
|
2013-04-12 00:36:47 +00:00
|
|
|
void Remove(int id);
|
|
|
|
RootFolder Get(int id);
|
2020-02-09 19:15:43 +00:00
|
|
|
List<RootFolder> AllForTag(int tagId);
|
|
|
|
RootFolder GetBestRootFolder(string path);
|
2018-08-08 00:57:15 +00:00
|
|
|
string GetBestRootFolderPath(string path);
|
2013-02-04 04:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class RootFolderService : IRootFolderService
|
2011-03-09 07:40:48 +00:00
|
|
|
{
|
2013-11-13 20:08:37 +00:00
|
|
|
private readonly IRootFolderRepository _rootFolderRepository;
|
2013-05-10 23:53:50 +00:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2020-02-09 19:15:43 +00:00
|
|
|
private readonly IManageCommandQueue _commandQueueManager;
|
2014-02-18 04:50:13 +00:00
|
|
|
private readonly Logger _logger;
|
2011-03-09 07:40:48 +00:00
|
|
|
|
2013-11-13 20:08:37 +00:00
|
|
|
public RootFolderService(IRootFolderRepository rootFolderRepository,
|
2013-08-02 07:16:37 +00:00
|
|
|
IDiskProvider diskProvider,
|
2020-02-09 19:15:43 +00:00
|
|
|
IManageCommandQueue commandQueueManager,
|
2014-02-18 04:50:13 +00:00
|
|
|
Logger logger)
|
2011-03-09 07:40:48 +00:00
|
|
|
{
|
2013-02-04 04:18:59 +00:00
|
|
|
_rootFolderRepository = rootFolderRepository;
|
2011-05-20 03:47:07 +00:00
|
|
|
_diskProvider = diskProvider;
|
2020-02-09 19:15:43 +00:00
|
|
|
_commandQueueManager = commandQueueManager;
|
2014-02-18 04:50:13 +00:00
|
|
|
_logger = logger;
|
2011-03-09 07:40:48 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 01:17:06 +00:00
|
|
|
public List<RootFolder> All()
|
2011-03-09 07:40:48 +00:00
|
|
|
{
|
2013-02-19 06:56:02 +00:00
|
|
|
var rootFolders = _rootFolderRepository.All().ToList();
|
2013-02-08 08:05:43 +00:00
|
|
|
|
2013-05-13 04:24:04 +00:00
|
|
|
return rootFolders;
|
|
|
|
}
|
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
public List<RootFolder> AllWithSpaceStats()
|
2013-05-13 04:24:04 +00:00
|
|
|
{
|
|
|
|
var rootFolders = _rootFolderRepository.All().ToList();
|
|
|
|
|
2013-02-08 08:05:43 +00:00
|
|
|
rootFolders.ForEach(folder =>
|
2013-05-13 04:24:04 +00:00
|
|
|
{
|
2015-02-10 00:42:26 +00:00
|
|
|
try
|
2013-02-08 08:05:43 +00:00
|
|
|
{
|
2019-08-17 03:54:03 +00:00
|
|
|
if (folder.Path.IsPathValid())
|
2015-02-10 00:42:26 +00:00
|
|
|
{
|
2019-08-17 03:54:03 +00:00
|
|
|
GetDetails(folder);
|
2015-02-10 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2015-02-10 00:42:26 +00:00
|
|
|
//We don't want an exception to prevent the root folders from loading in the UI, so they can still be deleted
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
_logger.Error(ex, "Unable to get free space and unmapped folders for root folder {0}", folder.Path);
|
2013-05-13 04:24:04 +00:00
|
|
|
}
|
|
|
|
});
|
2013-02-08 08:05:43 +00:00
|
|
|
|
|
|
|
return rootFolders;
|
2011-03-09 07:40:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
private void VerifyRootFolder(RootFolder rootFolder)
|
2011-03-09 07:40:48 +00:00
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(rootFolder.Path) || !Path.IsPathRooted(rootFolder.Path))
|
2015-02-10 00:42:26 +00:00
|
|
|
{
|
2012-01-19 05:04:55 +00:00
|
|
|
throw new ArgumentException("Invalid path");
|
2015-02-10 00:42:26 +00:00
|
|
|
}
|
2012-01-19 05:04:55 +00:00
|
|
|
|
2013-02-08 08:05:43 +00:00
|
|
|
if (!_diskProvider.FolderExists(rootFolder.Path))
|
2015-02-10 00:42:26 +00:00
|
|
|
{
|
2012-01-19 05:04:55 +00:00
|
|
|
throw new DirectoryNotFoundException("Can't add root directory that doesn't exist.");
|
2015-02-10 00:42:26 +00:00
|
|
|
}
|
2012-01-19 05:04:55 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
if (!_diskProvider.FolderWritable(rootFolder.Path))
|
2015-02-10 00:42:26 +00:00
|
|
|
{
|
2020-02-09 19:15:43 +00:00
|
|
|
throw new UnauthorizedAccessException(string.Format("Root folder path '{0}' is not writable by user '{1}'", rootFolder.Path, Environment.UserName));
|
2015-02-10 00:42:26 +00:00
|
|
|
}
|
2020-02-09 19:15:43 +00:00
|
|
|
}
|
2013-08-02 07:16:37 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
public RootFolder Add(RootFolder rootFolder)
|
|
|
|
{
|
|
|
|
VerifyRootFolder(rootFolder);
|
|
|
|
|
|
|
|
if (All().Exists(r => r.Path.PathEquals(rootFolder.Path)))
|
2015-02-10 00:42:26 +00:00
|
|
|
{
|
2020-02-09 19:15:43 +00:00
|
|
|
throw new InvalidOperationException("Root folder already exists.");
|
2015-02-10 00:42:26 +00:00
|
|
|
}
|
2012-01-19 05:04:55 +00:00
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
_rootFolderRepository.Insert(rootFolder);
|
2013-02-04 04:18:59 +00:00
|
|
|
|
2020-02-27 21:27:06 +00:00
|
|
|
_commandQueueManager.Push(new RescanFoldersCommand(new List<string> { rootFolder.Path }, FilterFilesType.None, true, null));
|
2020-02-09 19:15:43 +00:00
|
|
|
|
2019-08-17 03:54:03 +00:00
|
|
|
GetDetails(rootFolder);
|
|
|
|
|
2013-02-08 08:05:43 +00:00
|
|
|
return rootFolder;
|
2011-03-09 07:40:48 +00:00
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
public RootFolder Update(RootFolder rootFolder)
|
2011-05-20 03:47:07 +00:00
|
|
|
{
|
2020-02-09 19:15:43 +00:00
|
|
|
VerifyRootFolder(rootFolder);
|
2011-05-20 03:47:07 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
_rootFolderRepository.Update(rootFolder);
|
2011-05-20 03:47:07 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
GetDetails(rootFolder);
|
2011-05-20 03:47:07 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
return rootFolder;
|
|
|
|
}
|
2013-08-04 17:04:36 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
public void Remove(int id)
|
|
|
|
{
|
|
|
|
_rootFolderRepository.Delete(id);
|
2011-05-20 03:47:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 00:36:47 +00:00
|
|
|
public RootFolder Get(int id)
|
|
|
|
{
|
2013-08-26 07:27:31 +00:00
|
|
|
var rootFolder = _rootFolderRepository.Get(id);
|
2019-08-17 03:54:03 +00:00
|
|
|
GetDetails(rootFolder);
|
|
|
|
|
2013-08-26 07:27:31 +00:00
|
|
|
return rootFolder;
|
2013-04-12 00:36:47 +00:00
|
|
|
}
|
2018-08-08 00:57:15 +00:00
|
|
|
|
2020-02-09 19:15:43 +00:00
|
|
|
public List<RootFolder> AllForTag(int tagId)
|
|
|
|
{
|
|
|
|
return All().Where(r => r.DefaultTags.Contains(tagId)).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public RootFolder GetBestRootFolder(string path)
|
2018-08-08 00:57:15 +00:00
|
|
|
{
|
2020-02-09 19:15:43 +00:00
|
|
|
return All().Where(r => PathEqualityComparer.Instance.Equals(r.Path, path) || r.Path.IsParentPath(path))
|
2018-08-08 00:57:15 +00:00
|
|
|
.OrderByDescending(r => r.Path.Length)
|
|
|
|
.FirstOrDefault();
|
2020-02-09 19:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetBestRootFolderPath(string path)
|
|
|
|
{
|
|
|
|
var possibleRootFolder = GetBestRootFolder(path);
|
2018-08-08 00:57:15 +00:00
|
|
|
|
|
|
|
if (possibleRootFolder == null)
|
|
|
|
{
|
2020-12-13 02:28:59 +00:00
|
|
|
return _diskProvider.GetParentFolder(path);
|
2018-08-08 00:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return possibleRootFolder.Path;
|
|
|
|
}
|
|
|
|
|
2019-08-17 03:54:03 +00:00
|
|
|
private void GetDetails(RootFolder rootFolder)
|
|
|
|
{
|
|
|
|
Task.Run(() =>
|
|
|
|
{
|
|
|
|
if (_diskProvider.FolderExists(rootFolder.Path))
|
|
|
|
{
|
2019-08-27 21:48:34 +00:00
|
|
|
rootFolder.Accessible = true;
|
2019-08-17 03:54:03 +00:00
|
|
|
rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path);
|
|
|
|
rootFolder.TotalSpace = _diskProvider.GetTotalSize(rootFolder.Path);
|
|
|
|
}
|
2019-08-27 21:48:34 +00:00
|
|
|
}).Wait(5000);
|
2019-08-17 03:54:03 +00:00
|
|
|
}
|
2011-03-09 07:40:48 +00:00
|
|
|
}
|
2017-09-18 03:00:37 +00:00
|
|
|
}
|