jellyfin/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs

148 lines
3.9 KiB
C#
Raw Normal View History

2015-07-20 18:32:55 +00:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2015-10-04 04:23:11 +00:00
using CommonIO;
2015-07-20 18:32:55 +00:00
namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
{
public class ItemDataProvider<T>
where T : class
{
private readonly object _fileDataLock = new object();
2016-02-24 05:36:58 +00:00
private List<T> _items;
2015-07-20 18:32:55 +00:00
private readonly IJsonSerializer _jsonSerializer;
protected readonly ILogger Logger;
private readonly string _dataPath;
protected readonly Func<T, T, bool> EqualityComparer;
2015-09-13 23:07:54 +00:00
private readonly IFileSystem _fileSystem;
2015-07-20 18:32:55 +00:00
2015-09-13 23:07:54 +00:00
public ItemDataProvider(IFileSystem fileSystem, IJsonSerializer jsonSerializer, ILogger logger, string dataPath, Func<T, T, bool> equalityComparer)
2015-07-20 18:32:55 +00:00
{
Logger = logger;
_dataPath = dataPath;
EqualityComparer = equalityComparer;
_jsonSerializer = jsonSerializer;
2015-09-13 23:07:54 +00:00
_fileSystem = fileSystem;
2015-07-20 18:32:55 +00:00
}
public IReadOnlyList<T> GetAll()
{
2016-02-24 05:36:58 +00:00
lock (_fileDataLock)
2015-07-20 18:32:55 +00:00
{
2016-02-24 05:36:58 +00:00
if (_items == null)
2015-07-20 18:32:55 +00:00
{
2016-03-13 07:34:17 +00:00
Logger.Info("Loading live tv data from {0}", _dataPath);
2016-02-24 05:36:58 +00:00
_items = GetItemsFromFile(_dataPath);
2015-07-20 18:32:55 +00:00
}
2016-03-13 07:34:17 +00:00
return _items.ToList();
2015-07-20 18:32:55 +00:00
}
}
private List<T> GetItemsFromFile(string path)
{
var jsonFile = path + ".json";
try
{
return _jsonSerializer.DeserializeFromFile<List<T>>(jsonFile) ?? new List<T>();
2015-07-20 18:32:55 +00:00
}
catch (FileNotFoundException)
{
}
catch (DirectoryNotFoundException ex)
{
}
catch (IOException ex)
{
Logger.ErrorException("Error deserializing {0}", ex, jsonFile);
}
catch (Exception ex)
{
Logger.ErrorException("Error deserializing {0}", ex, jsonFile);
}
return new List<T>();
}
private void UpdateList(List<T> newList)
{
if (newList == null)
{
throw new ArgumentNullException("newList");
}
2015-07-23 23:40:54 +00:00
var file = _dataPath + ".json";
2015-11-21 04:55:26 +00:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(file));
2015-07-23 23:40:54 +00:00
2015-07-20 18:32:55 +00:00
lock (_fileDataLock)
{
2015-07-23 23:40:54 +00:00
_jsonSerializer.SerializeToFile(newList, file);
2015-07-20 18:32:55 +00:00
_items = newList;
}
}
public virtual void Update(T item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
2015-07-20 18:32:55 +00: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)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
2015-07-20 18:32:55 +00:00
var list = GetAll().ToList();
if (list.Any(i => EqualityComparer(i, item)))
{
throw new ArgumentException("item already exists");
}
list.Add(item);
UpdateList(list);
}
public void AddOrUpdate(T item)
{
var list = GetAll().ToList();
if (!list.Any(i => EqualityComparer(i, item)))
{
Add(item);
}
else
{
Update(item);
}
}
2015-11-21 04:55:26 +00:00
2015-07-20 18:32:55 +00:00
public virtual void Delete(T item)
{
var list = GetAll().Where(i => !EqualityComparer(i, item)).ToList();
UpdateList(list);
}
}
}