Add v5 root folder endpoints

This commit is contained in:
Mark McDowall 2025-11-24 21:25:19 -08:00
parent dd84bcd919
commit 449caa12e3
3 changed files with 127 additions and 0 deletions

View file

@ -204,6 +204,7 @@ private void GetDetails(RootFolder rootFolder, Dictionary<int, string> seriesPat
if (_diskProvider.FolderExists(rootFolder.Path))
{
rootFolder.Accessible = true;
rootFolder.IsEmpty = _diskProvider.FolderEmpty(rootFolder.Path);
rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path);
rootFolder.TotalSpace = _diskProvider.GetTotalSize(rootFolder.Path);
rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path, seriesPaths);

View file

@ -0,0 +1,73 @@
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.RootFolders;
using NzbDrone.Core.Validation.Paths;
using NzbDrone.SignalR;
using Sonarr.Http;
using Sonarr.Http.Extensions;
using Sonarr.Http.REST;
using Sonarr.Http.REST.Attributes;
namespace Sonarr.Api.V5.RootFolders;
[V5ApiController]
public class RootFolderController : RestControllerWithSignalR<RootFolderResource, RootFolder>
{
private readonly IRootFolderService _rootFolderService;
public RootFolderController(IRootFolderService rootFolderService,
IBroadcastSignalRMessage signalRBroadcaster,
RootFolderValidator rootFolderValidator,
PathExistsValidator pathExistsValidator,
MappedNetworkDriveValidator mappedNetworkDriveValidator,
RecycleBinValidator recycleBinValidator,
StartupFolderValidator startupFolderValidator,
SystemFolderValidator systemFolderValidator,
FolderWritableValidator folderWritableValidator)
: base(signalRBroadcaster)
{
_rootFolderService = rootFolderService;
SharedValidator.RuleFor(c => c.Path)
.Cascade(CascadeMode.Stop)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(mappedNetworkDriveValidator)
.SetValidator(startupFolderValidator)
.SetValidator(recycleBinValidator)
.SetValidator(pathExistsValidator)
.SetValidator(systemFolderValidator)
.SetValidator(folderWritableValidator);
}
protected override RootFolderResource GetResourceById(int id)
{
var timeout = Request?.GetBooleanQueryParameter("timeout", true) ?? true;
return _rootFolderService.Get(id, timeout).ToResource();
}
[RestPostById]
[Consumes("application/json")]
public ActionResult<RootFolderResource> CreateRootFolder([FromBody] RootFolderResource rootFolderResource)
{
var model = rootFolderResource.ToModel();
return Created(_rootFolderService.Add(model).Id);
}
[HttpGet]
[Produces("application/json")]
public List<RootFolderResource> GetRootFolders()
{
return _rootFolderService.AllWithUnmappedFolders().ToResource();
}
[RestDeleteById]
public ActionResult DeleteFolder(int id)
{
_rootFolderService.Remove(id);
return NoContent();
}
}

View file

@ -0,0 +1,53 @@
using NzbDrone.Common.Extensions;
using NzbDrone.Core.RootFolders;
using Sonarr.Http.REST;
namespace Sonarr.Api.V5.RootFolders;
public class RootFolderResource : RestResource
{
public string? Path { get; set; }
public bool Accessible { get; set; }
public bool IsEmpty { get; set; }
public long? FreeSpace { get; set; }
public long? TotalSpace { get; set; }
public List<UnmappedFolder> UnmappedFolders { get; set; } = [];
}
public static class RootFolderResourceMapper
{
public static RootFolderResource ToResource(this RootFolder model)
{
return new RootFolderResource
{
Id = model.Id,
Path = model.Path.GetCleanPath(),
Accessible = model.Accessible,
IsEmpty = model.IsEmpty,
FreeSpace = model.FreeSpace,
TotalSpace = model.TotalSpace,
UnmappedFolders = model.UnmappedFolders
};
}
public static RootFolder ToModel(this RootFolderResource resource)
{
return new RootFolder
{
Id = resource.Id,
Path = resource.Path
// Accessible
// IsEmpty
// FreeSpace
// UnmappedFolders
};
}
public static List<RootFolderResource> ToResource(this IEnumerable<RootFolder> models)
{
return models.Select(ToResource).ToList();
}
}