DeviceRepository.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Directory.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. device.Capabilities = capabilities;
  55. SaveDevice(device);
  56. return Task.FromResult(true);
  57. }
  58. public ClientCapabilities GetCapabilities(string reportedId)
  59. {
  60. var device = GetDevice(reportedId);
  61. return device == null ? null : device.Capabilities;
  62. }
  63. public DeviceInfo GetDevice(string id)
  64. {
  65. return GetDevices()
  66. .FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
  67. }
  68. public IEnumerable<DeviceInfo> GetDevices()
  69. {
  70. if (_devices == null)
  71. {
  72. lock (_syncLock)
  73. {
  74. if (_devices == null)
  75. {
  76. _devices = new ConcurrentBag<DeviceInfo>(LoadDevices());
  77. }
  78. }
  79. }
  80. return _devices.ToList();
  81. }
  82. private IEnumerable<DeviceInfo> LoadDevices()
  83. {
  84. var path = GetDevicesPath();
  85. try
  86. {
  87. return Directory
  88. .EnumerateFiles(path, "*", SearchOption.AllDirectories)
  89. .Where(i => string.Equals(Path.GetFileName(i), "device.json", StringComparison.OrdinalIgnoreCase))
  90. .ToList()
  91. .Select(i =>
  92. {
  93. try
  94. {
  95. return _json.DeserializeFromFile<DeviceInfo>(i);
  96. }
  97. catch (Exception ex)
  98. {
  99. _logger.ErrorException("Error reading {0}", ex, i);
  100. return null;
  101. }
  102. })
  103. .Where(i => i != null);
  104. }
  105. catch (IOException)
  106. {
  107. return new List<DeviceInfo>();
  108. }
  109. }
  110. public Task DeleteDevice(string id)
  111. {
  112. var path = GetDevicePath(id);
  113. lock (_syncLock)
  114. {
  115. try
  116. {
  117. _fileSystem.DeleteDirectory(path, true);
  118. }
  119. catch (DirectoryNotFoundException)
  120. {
  121. }
  122. _devices = null;
  123. }
  124. return Task.FromResult(true);
  125. }
  126. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  127. {
  128. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  129. lock (_syncLock)
  130. {
  131. try
  132. {
  133. return _json.DeserializeFromFile<ContentUploadHistory>(path);
  134. }
  135. catch (IOException)
  136. {
  137. return new ContentUploadHistory
  138. {
  139. DeviceId = deviceId
  140. };
  141. }
  142. }
  143. }
  144. public void AddCameraUpload(string deviceId, LocalFileInfo file)
  145. {
  146. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  147. Directory.CreateDirectory(Path.GetDirectoryName(path));
  148. lock (_syncLock)
  149. {
  150. ContentUploadHistory history;
  151. try
  152. {
  153. history = _json.DeserializeFromFile<ContentUploadHistory>(path);
  154. }
  155. catch (IOException)
  156. {
  157. history = new ContentUploadHistory
  158. {
  159. DeviceId = deviceId
  160. };
  161. }
  162. history.DeviceId = deviceId;
  163. history.FilesUploaded.Add(file);
  164. _json.SerializeToFile(history, path);
  165. }
  166. }
  167. }
  168. }