Implemented basic server file system endpoints and services. Implemented server files tab

This commit is contained in:
2025-03-03 18:07:49 +01:00
parent 30390dab71
commit 43b04ff630
21 changed files with 735 additions and 86 deletions

View File

@@ -0,0 +1,127 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using Moonlight.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Services;
using MoonlightServers.Shared.Http.Responses.Client.Servers.Files;
namespace MoonlightServers.ApiServer.Http.Controllers.Client;
[Authorize]
[ApiController]
[Route("api/client/servers")]
public class ServerFileSystemController : Controller
{
private readonly DatabaseRepository<Server> ServerRepository;
private readonly DatabaseRepository<User> UserRepository;
private readonly ServerFileSystemService ServerFileSystemService;
private readonly ServerService ServerService;
private readonly NodeService NodeService;
public ServerFileSystemController(
DatabaseRepository<Server> serverRepository,
DatabaseRepository<User> userRepository,
ServerFileSystemService serverFileSystemService,
ServerService serverService,
NodeService nodeService
)
{
ServerRepository = serverRepository;
UserRepository = userRepository;
ServerFileSystemService = serverFileSystemService;
ServerService = serverService;
NodeService = nodeService;
}
[HttpGet("{serverId:int}/files/list")]
public async Task<ServerFilesEntryResponse[]> List([FromRoute] int serverId, [FromQuery] string path)
{
var server = await GetServerById(serverId);
var entries = await ServerFileSystemService.List(server, path);
return entries.Select(x => new ServerFilesEntryResponse()
{
Name = x.Name,
Size = x.Size,
IsFile = x.IsFile,
CreatedAt = x.CreatedAt,
UpdatedAt = x.UpdatedAt
}).ToArray();
}
[HttpPost("{serverId:int}/files/move")]
public async Task Move([FromRoute] int serverId, [FromQuery] string oldPath, [FromQuery] string newPath)
{
var server = await GetServerById(serverId);
await ServerFileSystemService.Move(server, oldPath, newPath);
}
[HttpDelete("{serverId:int}/files/delete")]
public async Task Delete([FromRoute] int serverId, [FromQuery] string path)
{
var server = await GetServerById(serverId);
await ServerFileSystemService.Delete(server, path);
}
[HttpPost("{serverId:int}/files/mkdir")]
public async Task Mkdir([FromRoute] int serverId, [FromQuery] string path)
{
var server = await GetServerById(serverId);
await ServerFileSystemService.Mkdir(server, path);
}
[HttpGet("{serverId:int}/files/upload")]
public async Task<ServerFilesUploadResponse> Upload([FromRoute] int serverId, [FromQuery] string path)
{
var server = await GetServerById(serverId);
var accessToken = NodeService.CreateAccessToken(
server.Node,
parameters =>
{
parameters.Add("type", "upload");
parameters.Add("serverId", server.Id);
parameters.Add("path", path);
},
TimeSpan.FromMinutes(5)
);
var url = "";
url += server.Node.UseSsl ? "https://" : "http://";
url += $"{server.Node.Fqdn}:{server.Node.HttpPort}/";
url += $"api/servers/upload?token={accessToken}";
return new ServerFilesUploadResponse()
{
UploadUrl = url
};
}
private async Task<Server> GetServerById(int serverId)
{
var server = await ServerRepository
.Get()
.Include(x => x.Node)
.FirstOrDefaultAsync(x => x.Id == serverId);
if (server == null)
throw new HttpApiException("No server with this id found", 404);
var userIdClaim = User.Claims.First(x => x.Type == "userId");
var userId = int.Parse(userIdClaim.Value);
var user = await UserRepository.Get().FirstAsync(x => x.Id == userId);
if (!ServerService.IsAllowedToAccess(user, server))
throw new HttpApiException("No server with this id found", 404);
return server;
}
}

View File

@@ -0,0 +1,82 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Attributes;
using MoonCore.Extended.Abstractions;
using MoonCore.Helpers;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
namespace MoonlightServers.ApiServer.Services;
[Scoped]
public class ServerFileSystemService
{
private readonly NodeService NodeService;
private readonly DatabaseRepository<Server> ServerRepository;
public ServerFileSystemService(
NodeService nodeService,
DatabaseRepository<Server> serverRepository
)
{
NodeService = nodeService;
ServerRepository = serverRepository;
}
public async Task<ServerFileSystemResponse[]> List(Server server, string path)
{
using var apiClient = await GetApiClient(server);
return await apiClient.GetJson<ServerFileSystemResponse[]>(
$"api/servers/{server.Id}/files/list?path={path}"
);
}
public async Task Move(Server server, string oldPath, string newPath)
{
using var apiClient = await GetApiClient(server);
await apiClient.Post(
$"api/servers/{server.Id}/files/move?oldPath={oldPath}&newPath={newPath}"
);
}
public async Task Delete(Server server, string path)
{
using var apiClient = await GetApiClient(server);
await apiClient.Delete(
$"api/servers/{server.Id}/files/delete?path={path}"
);
}
public async Task Mkdir(Server server, string path)
{
using var apiClient = await GetApiClient(server);
await apiClient.Post(
$"api/servers/{server.Id}/files/mkdir?path={path}"
);
}
#region Helpers
private async Task<HttpApiClient> GetApiClient(Server server)
{
var serverWithNode = server;
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
// It can be null when its not included when loading via ef !!!
if (server.Node == null)
{
serverWithNode = await ServerRepository
.Get()
.Include(x => x.Node)
.FirstAsync(x => x.Id == server.Id);
}
return NodeService.CreateApiClient(serverWithNode.Node);
}
#endregion
}