using Microsoft.Extensions.Options; using MoonlightServers.Daemon.Configuration; using MoonlightServers.Daemon.Models; using MoonlightServers.Daemon.ServerSystem.Abstractions; namespace MoonlightServers.Daemon.ServerSystem.Implementations.Local; public class LocalRuntimeStorageService : IRuntimeStorageService { private readonly IOptions Options; public LocalRuntimeStorageService(IOptions options) { Options = options; } public Task FindAsync(string id) { var path = Path.Combine(Options.Value.RuntimePath, id); if (!Directory.Exists(path)) return Task.FromResult(null); return Task.FromResult(new LocalRuntimeStorage(path)); } public Task CreateAsync(string id, RuntimeConfiguration configuration) { var path = Path.Combine(Options.Value.RuntimePath, id); Directory.CreateDirectory(path); return Task.FromResult(new LocalRuntimeStorage(path)); } public Task UpdateAsync(IRuntimeStorage runtimeStorage, RuntimeConfiguration configuration) => Task.CompletedTask; public Task DeleteAsync(IRuntimeStorage runtimeStorage) { if (runtimeStorage is not LocalRuntimeStorage localRuntimeStorage) { throw new ArgumentException( $"You cannot delete runtime storages which haven't been created by {nameof(LocalRuntimeStorageService)}" ); } if(Directory.Exists(localRuntimeStorage.BindPath)) Directory.Delete(localRuntimeStorage.BindPath, true); return Task.CompletedTask; } }