using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Threading.Tasks; using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; namespace LinkDotNet.Blog.Web.Features.Services; public sealed class LocalStorageService : ILocalStorageService { private readonly ProtectedLocalStorage localStorage; public LocalStorageService(ProtectedLocalStorage localStorage) { this.localStorage = localStorage; } public async ValueTask ContainsKeyAsync(string key) { try { return (await localStorage.GetAsync(key)).Success; } catch (CryptographicException) { await localStorage.DeleteAsync(key); return false; } } public async ValueTask GetItemAsync(string key) { try { var result = await localStorage.GetAsync(key); return !result.Success ? throw new KeyNotFoundException($"Key {key} not found") : result.Value!; } catch (CryptographicException) { await localStorage.DeleteAsync(key); throw new KeyNotFoundException($"Key {key} was invalid and has been removed"); } } public async ValueTask SetItemAsync(string key, T value) { try { await localStorage.SetAsync(key, value!); } catch (CryptographicException) { await localStorage.DeleteAsync(key); throw new InvalidOperationException($"Could not set value for key {key}. The key has been removed."); } } }