Implemented basic server file system endpoints and services. Implemented server files tab
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonlightServers.Daemon.Services;
|
||||
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
|
||||
|
||||
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("api/servers")]
|
||||
public class ServerFileSystemController : Controller
|
||||
{
|
||||
private readonly ServerService ServerService;
|
||||
|
||||
public ServerFileSystemController(ServerService serverService)
|
||||
{
|
||||
ServerService = serverService;
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}/files/list")]
|
||||
public async Task<ServerFileSystemResponse[]> List([FromRoute] int id, [FromQuery] string path = "")
|
||||
{
|
||||
var server = ServerService.GetServer(id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
return await server.FileSystem.List(path);
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/files/move")]
|
||||
public async Task Move([FromRoute] int id, [FromQuery] string oldPath, [FromQuery] string newPath)
|
||||
{
|
||||
var server = ServerService.GetServer(id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
await server.FileSystem.Move(oldPath, newPath);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}/files/delete")]
|
||||
public async Task Delete([FromRoute] int id, [FromQuery] string path)
|
||||
{
|
||||
var server = ServerService.GetServer(id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
await server.FileSystem.Delete(path);
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/files/mkdir")]
|
||||
public async Task Mkdir([FromRoute] int id, [FromQuery] string path)
|
||||
{
|
||||
var server = ServerService.GetServer(id);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
await server.FileSystem.Mkdir(path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonlightServers.Daemon.Configuration;
|
||||
using MoonlightServers.Daemon.Helpers;
|
||||
using MoonlightServers.Daemon.Services;
|
||||
|
||||
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
|
||||
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
[Route("api/servers/upload")]
|
||||
public class UploadController : Controller
|
||||
{
|
||||
private readonly AccessTokenHelper AccessTokenHelper;
|
||||
private readonly AppConfiguration Configuration;
|
||||
private readonly ServerService ServerService;
|
||||
|
||||
public UploadController(
|
||||
AccessTokenHelper accessTokenHelper,
|
||||
ServerService serverService,
|
||||
AppConfiguration configuration
|
||||
)
|
||||
{
|
||||
AccessTokenHelper = accessTokenHelper;
|
||||
ServerService = serverService;
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task Upload([FromQuery] string token)
|
||||
{
|
||||
var file = Request.Form.Files.FirstOrDefault();
|
||||
|
||||
if (file == null)
|
||||
throw new HttpApiException("No file provided", 400);
|
||||
|
||||
if(file.Length > ByteConverter.FromMegaBytes(Configuration.Files.UploadLimit).Bytes)
|
||||
throw new HttpApiException("The provided file is bigger than the upload limit", 400);
|
||||
|
||||
#region Token validation
|
||||
|
||||
if (!AccessTokenHelper.Process(token, out var claims))
|
||||
throw new HttpApiException("Invalid access token provided", 401);
|
||||
|
||||
var typeClaim = claims.FirstOrDefault(x => x.Type == "type");
|
||||
|
||||
if (typeClaim == null || typeClaim.Value != "upload")
|
||||
throw new HttpApiException("Invalid access token provided: Missing or invalid type", 401);
|
||||
|
||||
var serverIdClaim = claims.FirstOrDefault(x => x.Type == "serverId");
|
||||
|
||||
if (serverIdClaim == null || !int.TryParse(serverIdClaim.Value, out var serverId))
|
||||
throw new HttpApiException("Invalid access token provided: Missing or invalid server id", 401);
|
||||
|
||||
#endregion
|
||||
|
||||
var server = ServerService.GetServer(serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
var dataStream = file.OpenReadStream();
|
||||
var path = file.FileName;
|
||||
|
||||
await server.FileSystem.Create(path, dataStream);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user