namespace Pulsar.Features.Backpack; using Observatory.Framework.Files; public interface IBackpackService : IJournalHandler; public class BackpackService(IOptions options, IEventHubContext hub, ILogger logger) : IBackpackService { public async Task Get() { var filePath = Path.Combine(options.Value.JournalDirectory, FileName); if (!FileHelper.ValidateFile(filePath)) { return new BackpackFile(); } await using var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var backpack = await JsonSerializer.DeserializeAsync(file); if (backpack != null) return backpack; logger.LogWarning("Failed to deserialize backpack file {File}", filePath); return new BackpackFile(); } public async Task HandleFile(string path, CancellationToken token = new ()) { if (!FileHelper.ValidateFile(path)) { return; } var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var backpack = await JsonSerializer.DeserializeAsync(file, cancellationToken: token); if (backpack == null) { logger.LogWarning("Failed to deserialize backpack {FilePath}", file); return; } await hub.Clients.All.BackpackUpdated(backpack); } public string FileName => FileHandlerService.BackpackFileName; }