2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2015-07-20 11:32:55 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2016-10-25 12:02:04 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
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 object _fileDataLock = new object();
|
2016-02-23 22:36:58 -07:00
|
|
|
private List<T> _items;
|
2015-07-20 11:32:55 -07:00
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
protected readonly ILogger Logger;
|
|
|
|
private readonly string _dataPath;
|
|
|
|
protected readonly Func<T, T, bool> EqualityComparer;
|
2015-09-13 16:07:54 -07:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2015-09-13 16:07:54 -07:00
|
|
|
public ItemDataProvider(IFileSystem fileSystem, IJsonSerializer jsonSerializer, ILogger logger, string dataPath, Func<T, T, bool> equalityComparer)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
Logger = logger;
|
|
|
|
_dataPath = dataPath;
|
|
|
|
EqualityComparer = equalityComparer;
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2015-09-13 16:07:54 -07:00
|
|
|
_fileSystem = fileSystem;
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public IReadOnlyList<T> GetAll()
|
|
|
|
{
|
2016-02-23 22:36:58 -07:00
|
|
|
lock (_fileDataLock)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2016-02-23 22:36:58 -07:00
|
|
|
if (_items == null)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2018-12-13 06:18:25 -07:00
|
|
|
Logger.LogInformation("Loading live tv data from {0}", _dataPath);
|
2016-02-23 22:36:58 -07:00
|
|
|
_items = GetItemsFromFile(_dataPath);
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
2016-03-13 00:34:17 -07:00
|
|
|
return _items.ToList();
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<T> GetItemsFromFile(string path)
|
|
|
|
{
|
|
|
|
var jsonFile = path + ".json";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2016-03-12 20:30:23 -07:00
|
|
|
return _jsonSerializer.DeserializeFromFile<List<T>>(jsonFile) ?? new List<T>();
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
|
|
|
}
|
2016-11-13 14:04:21 -07:00
|
|
|
catch (IOException)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
Logger.LogError(ex, "Error deserializing {jsonFile}", jsonFile);
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
return new List<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateList(List<T> newList)
|
|
|
|
{
|
2016-03-12 20:30:23 -07:00
|
|
|
if (newList == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(newList));
|
2016-03-12 20:30:23 -07:00
|
|
|
}
|
|
|
|
|
2015-07-23 16:40:54 -07:00
|
|
|
var file = _dataPath + ".json";
|
2019-01-26 14:08:04 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(file));
|
2015-07-23 16:40:54 -07:00
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
lock (_fileDataLock)
|
|
|
|
{
|
2015-07-23 16:40:54 -07:00
|
|
|
_jsonSerializer.SerializeToFile(newList, file);
|
2015-07-20 11:32:55 -07:00
|
|
|
_items = newList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Update(T item)
|
|
|
|
{
|
2016-03-12 20:30:23 -07:00
|
|
|
if (item == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(item));
|
2016-03-12 20:30:23 -07:00
|
|
|
}
|
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
var list = GetAll().ToList();
|
|
|
|
|
|
|
|
var index = list.FindIndex(i => EqualityComparer(i, item));
|
|
|
|
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("item not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
list[index] = item;
|
|
|
|
|
|
|
|
UpdateList(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Add(T item)
|
|
|
|
{
|
2016-03-12 20:30:23 -07:00
|
|
|
if (item == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(item));
|
2016-03-12 20:30:23 -07:00
|
|
|
}
|
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
var list = GetAll().ToList();
|
|
|
|
|
|
|
|
if (list.Any(i => EqualityComparer(i, item)))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("item already exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Add(item);
|
|
|
|
|
|
|
|
UpdateList(list);
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:18:51 -07:00
|
|
|
public void AddOrUpdate(T item)
|
|
|
|
{
|
|
|
|
var list = GetAll().ToList();
|
|
|
|
|
|
|
|
if (!list.Any(i => EqualityComparer(i, item)))
|
|
|
|
{
|
|
|
|
Add(item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Update(item);
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 21:55:26 -07:00
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
public virtual void Delete(T item)
|
|
|
|
{
|
|
|
|
var list = GetAll().Where(i => !EqualityComparer(i, item)).ToList();
|
|
|
|
|
|
|
|
UpdateList(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|