DeviceRepository.cs 5.5 KB

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