| | 1 | | #pragma warning disable CS1591 |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Diagnostics.CodeAnalysis; |
| | 6 | | using System.IO; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Text.Json; |
| | 9 | | using Jellyfin.Extensions.Json; |
| | 10 | | using Microsoft.Extensions.Logging; |
| | 11 | |
|
| | 12 | | namespace Jellyfin.LiveTv.Timers |
| | 13 | | { |
| | 14 | | public class ItemDataProvider<T> |
| | 15 | | where T : class |
| | 16 | | { |
| | 17 | | private readonly string _dataPath; |
| 44 | 18 | | private readonly object _fileDataLock = new object(); |
| 44 | 19 | | private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; |
| | 20 | | private T[]? _items; |
| | 21 | |
|
| | 22 | | public ItemDataProvider( |
| | 23 | | ILogger logger, |
| | 24 | | string dataPath, |
| | 25 | | Func<T, T, bool> equalityComparer) |
| | 26 | | { |
| 44 | 27 | | Logger = logger; |
| 44 | 28 | | _dataPath = dataPath; |
| 44 | 29 | | EqualityComparer = equalityComparer; |
| 44 | 30 | | } |
| | 31 | |
|
| | 32 | | protected ILogger Logger { get; } |
| | 33 | |
|
| | 34 | | protected Func<T, T, bool> EqualityComparer { get; } |
| | 35 | |
|
| | 36 | | [MemberNotNull(nameof(_items))] |
| | 37 | | private void EnsureLoaded() |
| | 38 | | { |
| 22 | 39 | | if (_items is not null) |
| | 40 | | { |
| 0 | 41 | | return; |
| | 42 | | } |
| | 43 | |
|
| 22 | 44 | | if (File.Exists(_dataPath)) |
| | 45 | | { |
| 0 | 46 | | Logger.LogInformation("Loading live tv data from {Path}", _dataPath); |
| | 47 | |
|
| | 48 | | try |
| | 49 | | { |
| 0 | 50 | | var bytes = File.ReadAllBytes(_dataPath); |
| 0 | 51 | | _items = JsonSerializer.Deserialize<T[]>(bytes, _jsonOptions); |
| 0 | 52 | | if (_items is null) |
| | 53 | | { |
| 0 | 54 | | Logger.LogError("Error deserializing {Path}, data was null", _dataPath); |
| 0 | 55 | | _items = Array.Empty<T>(); |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | return; |
| | 59 | | } |
| 0 | 60 | | catch (JsonException ex) |
| | 61 | | { |
| 0 | 62 | | Logger.LogError(ex, "Error deserializing {Path}", _dataPath); |
| 0 | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| 22 | 66 | | _items = Array.Empty<T>(); |
| 22 | 67 | | } |
| | 68 | |
|
| | 69 | | private void SaveList() |
| | 70 | | { |
| 0 | 71 | | Directory.CreateDirectory(Path.GetDirectoryName(_dataPath) ?? throw new ArgumentException("Path can't be a r |
| 0 | 72 | | var jsonString = JsonSerializer.Serialize(_items, _jsonOptions); |
| 0 | 73 | | File.WriteAllText(_dataPath, jsonString); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public IReadOnlyList<T> GetAll() |
| | 77 | | { |
| 22 | 78 | | lock (_fileDataLock) |
| | 79 | | { |
| 22 | 80 | | EnsureLoaded(); |
| 22 | 81 | | return (T[])_items.Clone(); |
| | 82 | | } |
| 22 | 83 | | } |
| | 84 | |
|
| | 85 | | public virtual void Update(T item) |
| | 86 | | { |
| 0 | 87 | | ArgumentNullException.ThrowIfNull(item); |
| | 88 | |
|
| 0 | 89 | | lock (_fileDataLock) |
| | 90 | | { |
| 0 | 91 | | EnsureLoaded(); |
| | 92 | |
|
| 0 | 93 | | var index = Array.FindIndex(_items, i => EqualityComparer(i, item)); |
| 0 | 94 | | if (index == -1) |
| | 95 | | { |
| 0 | 96 | | throw new ArgumentException("item not found"); |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | _items[index] = item; |
| | 100 | |
|
| 0 | 101 | | SaveList(); |
| 0 | 102 | | } |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | public virtual void Add(T item) |
| | 106 | | { |
| 0 | 107 | | ArgumentNullException.ThrowIfNull(item); |
| | 108 | |
|
| 0 | 109 | | lock (_fileDataLock) |
| | 110 | | { |
| 0 | 111 | | EnsureLoaded(); |
| | 112 | |
|
| 0 | 113 | | if (_items.Any(i => EqualityComparer(i, item))) |
| | 114 | | { |
| 0 | 115 | | throw new ArgumentException("item already exists", nameof(item)); |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | _items = [.._items, item]; |
| | 119 | |
|
| 0 | 120 | | SaveList(); |
| 0 | 121 | | } |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public virtual void AddOrUpdate(T item) |
| | 125 | | { |
| 0 | 126 | | lock (_fileDataLock) |
| | 127 | | { |
| 0 | 128 | | EnsureLoaded(); |
| | 129 | |
|
| 0 | 130 | | int index = Array.FindIndex(_items, i => EqualityComparer(i, item)); |
| 0 | 131 | | if (index == -1) |
| | 132 | | { |
| 0 | 133 | | _items = [.._items, item]; |
| | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 0 | 137 | | _items[index] = item; |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | SaveList(); |
| 0 | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | public virtual void Delete(T item) |
| | 145 | | { |
| 0 | 146 | | lock (_fileDataLock) |
| | 147 | | { |
| 0 | 148 | | EnsureLoaded(); |
| 0 | 149 | | _items = _items.Where(i => !EqualityComparer(i, item)).ToArray(); |
| | 150 | |
|
| 0 | 151 | | SaveList(); |
| 0 | 152 | | } |
| 0 | 153 | | } |
| | 154 | | } |
| | 155 | | } |