Implemented upload and downloading in daemon, api server and frontend
This commit is contained in:
@@ -73,6 +73,19 @@ public class ServerFileSystem
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Read(string inputPath, Func<Stream, Task> onHandle)
|
||||
{
|
||||
var path = Normalize(inputPath);
|
||||
|
||||
FileSystem.OpenFile(path, stream =>
|
||||
{
|
||||
// No try catch here because the safe fs abstraction already handles every error occuring in the handle
|
||||
onHandle.Invoke(stream).Wait();
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private string Normalize(string path)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonlightServers.Daemon.Configuration;
|
||||
using MoonlightServers.Daemon.Helpers;
|
||||
using MoonlightServers.Daemon.Services;
|
||||
|
||||
namespace MoonlightServers.Daemon.Http.Controllers.Servers;
|
||||
|
||||
[AllowAnonymous]
|
||||
[ApiController]
|
||||
[Route("api/servers/download")]
|
||||
public class DownloadController : Controller
|
||||
{
|
||||
private readonly AccessTokenHelper AccessTokenHelper;
|
||||
private readonly AppConfiguration Configuration;
|
||||
private readonly ServerService ServerService;
|
||||
|
||||
public DownloadController(
|
||||
AccessTokenHelper accessTokenHelper,
|
||||
ServerService serverService,
|
||||
AppConfiguration configuration
|
||||
)
|
||||
{
|
||||
AccessTokenHelper = accessTokenHelper;
|
||||
ServerService = serverService;
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task Download([FromQuery] string token)
|
||||
{
|
||||
#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 != "download")
|
||||
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);
|
||||
|
||||
var pathClaim = claims.FirstOrDefault(x => x.Type == "path");
|
||||
|
||||
if(pathClaim == null || string.IsNullOrEmpty(pathClaim.Value))
|
||||
throw new HttpApiException("Invalid access token provided: Missing or invalid path", 401);
|
||||
|
||||
#endregion
|
||||
|
||||
var server = ServerService.GetServer(serverId);
|
||||
|
||||
if (server == null)
|
||||
throw new HttpApiException("No server with this id found", 404);
|
||||
|
||||
var path = pathClaim.Value;
|
||||
|
||||
await server.FileSystem.Read(path, async dataStream =>
|
||||
{
|
||||
await Results.File(dataStream).ExecuteAsync(HttpContext);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
|
||||
<PackageReference Include="MoonCore" Version="1.8.4" />
|
||||
<PackageReference Include="MoonCore.Extended" Version="1.3.0" />
|
||||
<PackageReference Include="MoonCore.Unix" Version="1.0.2" />
|
||||
<PackageReference Include="MoonCore.Unix" Version="1.0.3" />
|
||||
<PackageReference Include="Stateless" Version="5.17.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user