DeviceRepository.cs 6.1 KB

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