ItemDataProvider.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using MediaBrowser.Model.Logging;
  2. using MediaBrowser.Model.Serialization;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
  8. {
  9. public class ItemDataProvider<T>
  10. where T : class
  11. {
  12. private readonly object _fileDataLock = new object();
  13. private List<T> _items;
  14. private readonly IJsonSerializer _jsonSerializer;
  15. protected readonly ILogger Logger;
  16. private readonly string _dataPath;
  17. protected readonly Func<T, T, bool> EqualityComparer;
  18. public ItemDataProvider(IJsonSerializer jsonSerializer, ILogger logger, string dataPath, Func<T, T, bool> equalityComparer)
  19. {
  20. Logger = logger;
  21. _dataPath = dataPath;
  22. EqualityComparer = equalityComparer;
  23. _jsonSerializer = jsonSerializer;
  24. }
  25. public IReadOnlyList<T> GetAll()
  26. {
  27. if (_items == null)
  28. {
  29. lock (_fileDataLock)
  30. {
  31. if (_items == null)
  32. {
  33. _items = GetItemsFromFile(_dataPath);
  34. }
  35. }
  36. }
  37. return _items;
  38. }
  39. private List<T> GetItemsFromFile(string path)
  40. {
  41. var jsonFile = path + ".json";
  42. try
  43. {
  44. return _jsonSerializer.DeserializeFromFile<List<T>>(jsonFile);
  45. }
  46. catch (FileNotFoundException)
  47. {
  48. }
  49. catch (DirectoryNotFoundException ex)
  50. {
  51. }
  52. catch (IOException ex)
  53. {
  54. Logger.ErrorException("Error deserializing {0}", ex, jsonFile);
  55. throw;
  56. }
  57. catch (Exception ex)
  58. {
  59. Logger.ErrorException("Error deserializing {0}", ex, jsonFile);
  60. }
  61. return new List<T>();
  62. }
  63. private void UpdateList(List<T> newList)
  64. {
  65. var file = _dataPath + ".json";
  66. Directory.CreateDirectory(Path.GetDirectoryName(file));
  67. lock (_fileDataLock)
  68. {
  69. _jsonSerializer.SerializeToFile(newList, file);
  70. _items = newList;
  71. }
  72. }
  73. public virtual void Update(T item)
  74. {
  75. var list = GetAll().ToList();
  76. var index = list.FindIndex(i => EqualityComparer(i, item));
  77. if (index == -1)
  78. {
  79. throw new ArgumentException("item not found");
  80. }
  81. list[index] = item;
  82. UpdateList(list);
  83. }
  84. public virtual void Add(T item)
  85. {
  86. var list = GetAll().ToList();
  87. if (list.Any(i => EqualityComparer(i, item)))
  88. {
  89. throw new ArgumentException("item already exists");
  90. }
  91. list.Add(item);
  92. UpdateList(list);
  93. }
  94. public virtual void Delete(T item)
  95. {
  96. var list = GetAll().Where(i => !EqualityComparer(i, item)).ToList();
  97. UpdateList(list);
  98. }
  99. }
  100. }