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