DeviceRepository.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Controller.Devices;
  4. using MediaBrowser.Model.Devices;
  5. using MediaBrowser.Model.Serialization;
  6. using MediaBrowser.Model.Session;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Implementations.Devices
  14. {
  15. public class DeviceRepository : IDeviceRepository
  16. {
  17. private readonly object _syncLock = new object();
  18. private readonly IApplicationPaths _appPaths;
  19. private readonly IJsonSerializer _json;
  20. private ConcurrentBag<DeviceInfo> _devices;
  21. public DeviceRepository(IApplicationPaths appPaths, IJsonSerializer json)
  22. {
  23. _appPaths = appPaths;
  24. _json = json;
  25. }
  26. private string GetDevicesPath()
  27. {
  28. return Path.Combine(_appPaths.DataPath, "devices");
  29. }
  30. private string GetDevicePath(string id)
  31. {
  32. return Path.Combine(GetDevicesPath(), id.GetMD5().ToString("N"));
  33. }
  34. public Task SaveDevice(DeviceInfo device)
  35. {
  36. var path = Path.Combine(GetDevicePath(device.Id), "device.json");
  37. Directory.CreateDirectory(Path.GetDirectoryName(path));
  38. lock (_syncLock)
  39. {
  40. _json.SerializeToFile(device, path);
  41. _devices = null;
  42. }
  43. return Task.FromResult(true);
  44. }
  45. public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  46. {
  47. var device = GetDevice(reportedId);
  48. device.Capabilities = capabilities;
  49. SaveDevice(device);
  50. return Task.FromResult(true);
  51. }
  52. public ClientCapabilities GetCapabilities(string reportedId)
  53. {
  54. var device = GetDevice(reportedId);
  55. return device == null ? null : device.Capabilities;
  56. }
  57. public DeviceInfo GetDevice(string id)
  58. {
  59. return GetDevices().FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
  60. }
  61. public IEnumerable<DeviceInfo> GetDevices()
  62. {
  63. if (_devices == null)
  64. {
  65. lock (_syncLock)
  66. {
  67. if (_devices == null)
  68. {
  69. _devices = new ConcurrentBag<DeviceInfo>(LoadDevices());
  70. }
  71. }
  72. }
  73. return _devices.ToList();
  74. }
  75. private IEnumerable<DeviceInfo> LoadDevices()
  76. {
  77. var path = GetDevicesPath();
  78. try
  79. {
  80. return new DirectoryInfo(path)
  81. .EnumerateFiles("*", SearchOption.AllDirectories)
  82. .Where(i => string.Equals(i.Name, "device.json", StringComparison.OrdinalIgnoreCase))
  83. .Select(i => _json.DeserializeFromFile<DeviceInfo>(i.FullName))
  84. .ToList();
  85. }
  86. catch (IOException)
  87. {
  88. return new List<DeviceInfo>();
  89. }
  90. }
  91. public Task DeleteDevice(string id)
  92. {
  93. var path = GetDevicePath(id);
  94. lock (_syncLock)
  95. {
  96. try
  97. {
  98. Directory.Delete(path, true);
  99. }
  100. catch (DirectoryNotFoundException)
  101. {
  102. }
  103. _devices = null;
  104. }
  105. return Task.FromResult(true);
  106. }
  107. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  108. {
  109. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  110. lock (_syncLock)
  111. {
  112. try
  113. {
  114. return _json.DeserializeFromFile<ContentUploadHistory>(path);
  115. }
  116. catch (IOException)
  117. {
  118. return new ContentUploadHistory
  119. {
  120. DeviceId = deviceId
  121. };
  122. }
  123. }
  124. }
  125. public void AddCameraUpload(string deviceId, LocalFileInfo file)
  126. {
  127. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  128. Directory.CreateDirectory(Path.GetDirectoryName(path));
  129. lock (_syncLock)
  130. {
  131. ContentUploadHistory history;
  132. try
  133. {
  134. history = _json.DeserializeFromFile<ContentUploadHistory>(path);
  135. }
  136. catch (IOException)
  137. {
  138. history = new ContentUploadHistory
  139. {
  140. DeviceId = deviceId
  141. };
  142. }
  143. history.DeviceId = deviceId;
  144. history.FilesUploaded.Add(file);
  145. _json.SerializeToFile(history, path);
  146. }
  147. }
  148. }
  149. }