2020-02-06 07:20:23 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2015-07-20 11:32:55 -07:00
|
|
|
using System.Collections.Generic;
|
2021-10-01 02:21:22 -07:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2015-07-20 11:32:55 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-12-23 05:12:40 -07:00
|
|
|
using System.Text.Json;
|
2021-06-19 09:02:33 -07:00
|
|
|
using Jellyfin.Extensions.Json;
|
2019-01-13 12:22:00 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.EmbyTV
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
public class ItemDataProvider<T>
|
|
|
|
where T : class
|
|
|
|
{
|
|
|
|
private readonly string _dataPath;
|
2019-09-12 12:30:57 -07:00
|
|
|
private readonly object _fileDataLock = new object();
|
2021-03-08 21:57:38 -07:00
|
|
|
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
2021-10-01 02:21:22 -07:00
|
|
|
private T[]? _items;
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
public ItemDataProvider(
|
|
|
|
ILogger logger,
|
|
|
|
string dataPath,
|
|
|
|
Func<T, T, bool> equalityComparer)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
Logger = logger;
|
|
|
|
_dataPath = dataPath;
|
|
|
|
EqualityComparer = equalityComparer;
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
protected ILogger Logger { get; }
|
2019-02-24 07:47:59 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
protected Func<T, T, bool> EqualityComparer { get; }
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2021-10-01 02:21:22 -07:00
|
|
|
[MemberNotNull(nameof(_items))]
|
2019-09-12 12:30:57 -07:00
|
|
|
private void EnsureLoaded()
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2022-12-05 07:01:13 -07:00
|
|
|
if (_items is not null)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
return;
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
2019-09-12 12:30:57 -07:00
|
|
|
|
|
|
|
if (File.Exists(_dataPath))
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
Logger.LogInformation("Loading live tv data from {Path}", _dataPath);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-01-12 07:28:02 -07:00
|
|
|
var bytes = File.ReadAllBytes(_dataPath);
|
|
|
|
_items = JsonSerializer.Deserialize<T[]>(bytes, _jsonOptions);
|
2022-12-05 07:00:20 -07:00
|
|
|
if (_items is null)
|
2021-10-01 02:21:22 -07:00
|
|
|
{
|
|
|
|
Logger.LogError("Error deserializing {Path}, data was null", _dataPath);
|
|
|
|
_items = Array.Empty<T>();
|
|
|
|
}
|
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
return;
|
|
|
|
}
|
2021-01-12 07:28:02 -07:00
|
|
|
catch (JsonException ex)
|
2019-09-12 12:30:57 -07:00
|
|
|
{
|
|
|
|
Logger.LogError(ex, "Error deserializing {Path}", _dataPath);
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
2019-02-20 06:29:34 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
_items = Array.Empty<T>();
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
private void SaveList()
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2021-10-01 02:21:22 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(_dataPath) ?? throw new ArgumentException("Path can't be a root directory.", nameof(_dataPath)));
|
2020-12-28 16:35:59 -07:00
|
|
|
var jsonString = JsonSerializer.Serialize(_items, _jsonOptions);
|
|
|
|
File.WriteAllText(_dataPath, jsonString);
|
2019-09-12 12:30:57 -07:00
|
|
|
}
|
2015-07-23 16:40:54 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
public IReadOnlyList<T> GetAll()
|
|
|
|
{
|
2015-07-20 11:32:55 -07:00
|
|
|
lock (_fileDataLock)
|
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
EnsureLoaded();
|
|
|
|
return (T[])_items.Clone();
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Update(T item)
|
|
|
|
{
|
2022-10-06 11:21:23 -07:00
|
|
|
ArgumentNullException.ThrowIfNull(item);
|
2016-03-12 20:30:23 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
lock (_fileDataLock)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
EnsureLoaded();
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
var index = Array.FindIndex(_items, i => EqualityComparer(i, item));
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("item not found");
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
_items[index] = item;
|
|
|
|
|
|
|
|
SaveList();
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Add(T item)
|
|
|
|
{
|
2022-10-06 11:21:23 -07:00
|
|
|
ArgumentNullException.ThrowIfNull(item);
|
2016-03-12 20:30:23 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
lock (_fileDataLock)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
EnsureLoaded();
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
if (_items.Any(i => EqualityComparer(i, item)))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("item already exists", nameof(item));
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
int oldLen = _items.Length;
|
|
|
|
var newList = new T[oldLen + 1];
|
|
|
|
_items.CopyTo(newList, 0);
|
|
|
|
newList[oldLen] = item;
|
|
|
|
_items = newList;
|
|
|
|
|
|
|
|
SaveList();
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
public virtual void AddOrUpdate(T item)
|
2015-11-17 21:18:51 -07:00
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
lock (_fileDataLock)
|
2015-11-17 21:18:51 -07:00
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
EnsureLoaded();
|
|
|
|
|
|
|
|
int index = Array.FindIndex(_items, i => EqualityComparer(i, item));
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
int oldLen = _items.Length;
|
|
|
|
var newList = new T[oldLen + 1];
|
|
|
|
_items.CopyTo(newList, 0);
|
|
|
|
newList[oldLen] = item;
|
|
|
|
_items = newList;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_items[index] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveList();
|
2015-11-17 21:18:51 -07:00
|
|
|
}
|
|
|
|
}
|
2015-11-20 21:55:26 -07:00
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
public virtual void Delete(T item)
|
|
|
|
{
|
2019-09-12 12:30:57 -07:00
|
|
|
lock (_fileDataLock)
|
|
|
|
{
|
|
|
|
EnsureLoaded();
|
|
|
|
_items = _items.Where(i => !EqualityComparer(i, item)).ToArray();
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-09-12 12:30:57 -07:00
|
|
|
SaveList();
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|