Implemented basic server file system endpoints and services. Implemented server files tab
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user