Lidarr/src/NzbDrone.Core/RootFolders/RootFolderService.cs

176 lines
5.4 KiB
C#
Raw Normal View History

2013-02-04 04:18:59 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NLog;
2011-11-13 04:07:06 +00:00
using NzbDrone.Common;
using NzbDrone.Common.Disk;
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;
2013-02-04 04:18:59 +00:00
namespace NzbDrone.Core.RootFolders
{
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);
string GetBestRootFolderPath(string path);
2013-02-04 04:18:59 +00:00
}
public class RootFolderService : IRootFolderService
{
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;
private readonly Logger _logger;
2013-11-13 20:08:37 +00:00
public RootFolderService(IRootFolderRepository rootFolderRepository,
IDiskProvider diskProvider,
2020-02-09 19:15:43 +00:00
IManageCommandQueue commandQueueManager,
Logger logger)
{
2013-02-04 04:18:59 +00:00
_rootFolderRepository = rootFolderRepository;
_diskProvider = diskProvider;
2020-02-09 19:15:43 +00:00
_commandQueueManager = commandQueueManager;
_logger = logger;
}
2013-08-21 01:17:06 +00:00
public List<RootFolder> All()
{
2013-02-19 06:56:02 +00:00
var rootFolders = _rootFolderRepository.All().ToList();
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();
rootFolders.ForEach(folder =>
2013-05-13 04:24:04 +00:00
{
try
{
if (folder.Path.IsPathValid())
{
GetDetails(folder);
}
}
// 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
}
});
return rootFolders;
}
2020-02-09 19:15:43 +00:00
private void VerifyRootFolder(RootFolder rootFolder)
{
if (string.IsNullOrWhiteSpace(rootFolder.Path) || !Path.IsPathRooted(rootFolder.Path))
{
throw new ArgumentException("Invalid path");
}
if (!_diskProvider.FolderExists(rootFolder.Path))
{
throw new DirectoryNotFoundException("Can't add root directory that doesn't exist.");
}
2020-02-09 19:15:43 +00:00
if (!_diskProvider.FolderWritable(rootFolder.Path))
{
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));
}
2020-02-09 19:15:43 +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)))
{
2020-02-09 19:15:43 +00:00
throw new InvalidOperationException("Root folder already exists.");
}
2013-02-19 06:56:02 +00:00
_rootFolderRepository.Insert(rootFolder);
2013-02-04 04:18:59 +00:00
_commandQueueManager.Push(new RescanFoldersCommand(new List<string> { rootFolder.Path }, FilterFilesType.None, true, null));
2020-02-09 19:15:43 +00:00
GetDetails(rootFolder);
return rootFolder;
}
2011-04-10 02:44:01 +00:00
2020-02-09 19:15:43 +00:00
public RootFolder Update(RootFolder rootFolder)
{
2020-02-09 19:15:43 +00:00
VerifyRootFolder(rootFolder);
2020-02-09 19:15:43 +00:00
_rootFolderRepository.Update(rootFolder);
2020-02-09 19:15:43 +00:00
GetDetails(rootFolder);
2020-02-09 19:15:43 +00:00
return rootFolder;
}
2020-02-09 19:15:43 +00:00
public void Remove(int id)
{
_rootFolderRepository.Delete(id);
}
2013-04-12 00:36:47 +00:00
public RootFolder Get(int id)
{
var rootFolder = _rootFolderRepository.Get(id);
GetDetails(rootFolder);
return rootFolder;
2013-04-12 00:36:47 +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)
{
2020-02-09 19:15:43 +00:00
return All().Where(r => PathEqualityComparer.Instance.Equals(r.Path, path) || r.Path.IsParentPath(path))
.OrderByDescending(r => r.Path.Length)
.FirstOrDefault();
2020-02-09 19:15:43 +00:00
}
public string GetBestRootFolderPath(string path)
{
var possibleRootFolder = GetBestRootFolder(path);
if (possibleRootFolder == null)
{
return _diskProvider.GetParentFolder(path);
}
return possibleRootFolder.Path;
}
private void GetDetails(RootFolder rootFolder)
{
Task.Run(() =>
{
if (_diskProvider.FolderExists(rootFolder.Path))
{
rootFolder.Accessible = true;
rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path);
rootFolder.TotalSpace = _diskProvider.GetTotalSize(rootFolder.Path);
}
}).Wait(5000);
}
}
}