DeviceRepository.cs 6.0 KB

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