Implemented basic server file system endpoints and services. Implemented server files tab
This commit is contained in:
84
MoonlightServers.Daemon/Helpers/ServerFileSystem.cs
Normal file
84
MoonlightServers.Daemon/Helpers/ServerFileSystem.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using Mono.Unix.Native;
|
||||
using MoonCore.Unix.SecureFs;
|
||||
using MoonlightServers.DaemonShared.DaemonSide.Http.Responses.Servers;
|
||||
|
||||
namespace MoonlightServers.Daemon.Helpers;
|
||||
|
||||
public class ServerFileSystem
|
||||
{
|
||||
private readonly SecureFileSystem FileSystem;
|
||||
|
||||
public ServerFileSystem(SecureFileSystem fileSystem)
|
||||
{
|
||||
FileSystem = fileSystem;
|
||||
}
|
||||
|
||||
public Task<ServerFileSystemResponse[]> List(string inputPath)
|
||||
{
|
||||
var path = Normalize(inputPath);
|
||||
var entries = FileSystem.ReadDir(path);
|
||||
|
||||
var result = entries
|
||||
.Select(x => new ServerFileSystemResponse()
|
||||
{
|
||||
Name = x.Name,
|
||||
IsFile = x.IsFile,
|
||||
Size = x.Size,
|
||||
UpdatedAt = x.LastChanged,
|
||||
CreatedAt = x.CreatedAt
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
|
||||
public Task Move(string inputOldPath, string inputNewPath)
|
||||
{
|
||||
var oldPath = Normalize(inputOldPath);
|
||||
var newPath = Normalize(inputNewPath);
|
||||
|
||||
FileSystem.Rename(oldPath, newPath);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Delete(string inputPath)
|
||||
{
|
||||
var path = Normalize(inputPath);
|
||||
|
||||
FileSystem.RemoveAll(path);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Mkdir(string inputPath)
|
||||
{
|
||||
var path = Normalize(inputPath);
|
||||
|
||||
FileSystem.MkdirAll(path, FilePermissions.ACCESSPERMS);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Create(string inputPath, Stream dataStream)
|
||||
{
|
||||
var path = Normalize(inputPath);
|
||||
|
||||
var parentDirectory = Path.GetDirectoryName(path);
|
||||
|
||||
if(!string.IsNullOrEmpty(parentDirectory) && parentDirectory != "/")
|
||||
FileSystem.MkdirAll(parentDirectory, FilePermissions.ACCESSPERMS);
|
||||
|
||||
FileSystem.WriteFile(path, dataStream);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private string Normalize(string path)
|
||||
{
|
||||
return path
|
||||
.Replace("//", "/")
|
||||
.Replace("..", "")
|
||||
.TrimStart('/');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user