host file access + use in admin resource manager

This commit is contained in:
Daniel Balk 2023-04-06 22:58:26 +02:00
parent cf29099f39
commit d43f77ea47
3 changed files with 151 additions and 3 deletions

View file

@ -0,0 +1,143 @@
using System.IO.Compression;
namespace Moonlight.App.Helpers.Files;
public class HostFileAccess : FileAccess
{
private string BasePath;
public HostFileAccess(string basePath)
{
BasePath = basePath;
}
private string currhp => BasePath.TrimEnd('/') + "/" + CurrentPath.TrimStart('/').TrimEnd('/') + "/";
public override async Task<FileData[]> Ls()
{
var x = new List<FileData>();
foreach (var dir in Directory.GetDirectories(currhp))
{
x.Add(new()
{
Name = dir.Remove(0, currhp.Length),
Size = 0,
IsFile = false,
});
}
foreach (var fn in Directory.GetFiles(currhp))
{
x.Add(new()
{
Name = fn.Remove(0, currhp.Length),
Size = new FileInfo(fn).Length,
IsFile = true,
});
}
return x.ToArray();
}
public override Task Cd(string dir)
{
var x = Path.Combine(CurrentPath, dir).Replace("\\", "/") + "/";
x = x.Replace("//", "/");
CurrentPath = x;
return Task.CompletedTask;
}
public override Task Up()
{
CurrentPath = Path.GetFullPath(Path.Combine(CurrentPath, "..")).Replace("\\", "/").Replace("C:", "");
return Task.CompletedTask;
}
public override Task SetDir(string dir)
{
CurrentPath = dir;
return Task.CompletedTask;
}
public override async Task<string> Read(FileData fileData)
{
return await File.ReadAllTextAsync(currhp + fileData.Name);
}
public override async Task Write(FileData fileData, string content)
{
await File.WriteAllTextAsync(currhp + fileData.Name, content);
}
public override async Task Upload(string name, Stream dataStream, Action<int>? progressUpdated = null)
{
var ms = new MemoryStream();
await dataStream.CopyToAsync(ms);
var data = ms.ToArray();
ms.Dispose();
dataStream.Dispose();
await File.WriteAllBytesAsync(currhp + name, data);
}
public override async Task MkDir(string name)
{
Directory.CreateDirectory(currhp + name + "/");
}
public override Task<string> Pwd()
{
return Task.FromResult(CurrentPath);
}
public override Task<string> DownloadUrl(FileData fileData)
{
throw new NotImplementedException();
}
public override async Task<Stream> DownloadStream(FileData fileData)
{
var s = new MemoryStream(8 * 1024 * 1204); //TODO: Add config option
s.Write(File.ReadAllBytes(currhp + fileData.Name));
s.Position = 0;
return s;
}
public override async Task Delete(FileData fileData)
{
if (fileData.IsFile)
File.Delete(currhp + fileData.Name);
else
Directory.Delete(currhp + fileData.Name);
}
public override async Task Move(FileData fileData, string newPath)
{
if (fileData.IsFile)
File.Move(currhp + fileData.Name, newPath);
else
Directory.Move(currhp + fileData.Name, newPath);
}
public override Task Compress(params FileData[] files)
{
throw new NotImplementedException();
}
public override Task Decompress(FileData fileData)
{
throw new NotImplementedException();
}
public override Task<string> GetLaunchUrl()
{
throw new NotImplementedException();
}
public override object Clone()
{
return new HostFileAccess(BasePath);
}
}

View file

@ -1,14 +1,14 @@
@page "/admin/system/resources"
@using Moonlight.Shared.Components.Navigations
@using Moonlight.Shared.Components.FileManagerPartials
@using Moonlight.App.Models.Files.Accesses
@using Moonlight.App.Helpers.Files
@using Moonlight.Shared.Components.Navigations
<OnlyAdmin>
<AdminSystemNavigation Index="5" />
<div class="card card-body">
<FileManager FileAccess="@(new HostFileAccess("resources"))">
<FileManager Access="@(new HostFileAccess("resources"))">
</FileManager>
</div>
</OnlyAdmin>

View file

@ -513,3 +513,8 @@ Error from plesk;Error from plesk
Host;Host
Username;Username
SRV records cannot be updated thanks to the cloudflare api client. Please delete the record and create a new one;SRV records cannot be updated thanks to the cloudflare api client. Please delete the record and create a new one
Hour;Hour
Day;Day
Month;Month
Year;Year
All time;All time