using System.IO; using System.Text.Json; using Futonizer.Models; namespace Futonizer.Services; /// /// Persists a rolling history of previously processed files (newest first, /// capped at ) as a JSON file stored next to the /// application executable. /// public static class HistoryService { private const int MaxEntries = 1000; private static readonly string HistoryFilePath = Path.Combine(AppContext.BaseDirectory, "history.json"); private static readonly object FileLock = new(); public static List Load() { lock (FileLock) { try { if (!File.Exists(HistoryFilePath)) return new List(); string json = File.ReadAllText(HistoryFilePath); var list = JsonSerializer.Deserialize>(json); return list ?? new List(); } catch { return new List(); } } } /// /// Adds a new entry at the front of the history and trims it down to /// items. Safe to call concurrently from /// multiple in-flight strip jobs. /// public static void Add(HistoryEntry entry) { lock (FileLock) { try { var list = File.Exists(HistoryFilePath) ? JsonSerializer.Deserialize>(File.ReadAllText(HistoryFilePath)) ?? new List() : new List(); list.Insert(0, entry); if (list.Count > MaxEntries) list.RemoveRange(MaxEntries, list.Count - MaxEntries); string json = JsonSerializer.Serialize(list, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText(HistoryFilePath, json); } catch { // Best effort — history is a convenience feature, never worth failing a strip job over. } } } }