using MoonCore.Blazor.Tailwind.Fm; using MoonCore.Blazor.Tailwind.Fm.Models; using MoonCore.Helpers; using Moonlight.Shared.Http.Requests.Admin.Sys.Files; using Moonlight.Shared.Http.Responses.Admin.Sys.Files; namespace Moonlight.Client.Implementations; public class SysFileSystemProvider : IFileSystemProvider, ICompressFileSystemProvider { private readonly HttpApiClient HttpApiClient; private readonly string BaseApiUrl = "api/admin/system/files"; public CompressType[] CompressTypes { get; } = [ new() { Extension = "zip", DisplayName = "ZIP Archive" }, new() { Extension = "tar.gz", DisplayName = "GZ Compressed Tar Archive" } ]; public SysFileSystemProvider(HttpApiClient httpApiClient) { HttpApiClient = httpApiClient; } public async Task List(string path) { var entries = await HttpApiClient.GetJson( $"{BaseApiUrl}/list?path={path}" ); return entries.Select(x => new FileSystemEntry() { Name = x.Name, Size = x.Size, CreatedAt = x.CreatedAt, IsFile = x.IsFile, UpdatedAt = x.UpdatedAt }).ToArray(); } public async Task Create(string path, Stream stream) => await HttpApiClient.PostStream($"{BaseApiUrl}/create?path={path}", stream); public async Task Move(string oldPath, string newPath) => await HttpApiClient.Post($"{BaseApiUrl}/move?oldPath={oldPath}&newPath={newPath}"); public async Task Delete(string path) => await HttpApiClient.Delete($"{BaseApiUrl}/delete?path={path}"); public async Task CreateDirectory(string path) => await HttpApiClient.Post($"{BaseApiUrl}/mkdir?path={path}"); public async Task Read(string path) => await HttpApiClient.GetStream($"{BaseApiUrl}/read?path={path}"); public async Task Compress(CompressType type, string path, string[] itemsToCompress) { await HttpApiClient.Post($"{BaseApiUrl}/compress", new CompressRequest() { Type = type.Extension, Path = path, ItemsToCompress = itemsToCompress }); } public async Task Decompress(CompressType type, string path, string destination) { await HttpApiClient.Post($"{BaseApiUrl}/decompress", new DecompressRequest() { Type = type.Extension, Path = path, Destination = destination }); } }