DeviceRepository.cs 5.5 KB

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