DeviceRepository.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. using CommonIO;
  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 List<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 = null;
  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 = LoadDevices().ToList();
  84. }
  85. return _devices.ToList();
  86. }
  87. }
  88. private IEnumerable<DeviceInfo> LoadDevices()
  89. {
  90. var path = GetDevicesPath();
  91. try
  92. {
  93. return _fileSystem
  94. .GetFilePaths(path, true)
  95. .Where(i => string.Equals(Path.GetFileName(i), "device.json", StringComparison.OrdinalIgnoreCase))
  96. .ToList()
  97. .Select(i =>
  98. {
  99. try
  100. {
  101. return _json.DeserializeFromFile<DeviceInfo>(i);
  102. }
  103. catch (Exception ex)
  104. {
  105. _logger.ErrorException("Error reading {0}", ex, i);
  106. return null;
  107. }
  108. })
  109. .Where(i => i != null);
  110. }
  111. catch (IOException)
  112. {
  113. return new List<DeviceInfo>();
  114. }
  115. }
  116. public Task DeleteDevice(string id)
  117. {
  118. var path = GetDevicePath(id);
  119. lock (_syncLock)
  120. {
  121. try
  122. {
  123. _fileSystem.DeleteDirectory(path, true);
  124. }
  125. catch (DirectoryNotFoundException)
  126. {
  127. }
  128. _devices = null;
  129. }
  130. return Task.FromResult(true);
  131. }
  132. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  133. {
  134. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  135. lock (_syncLock)
  136. {
  137. try
  138. {
  139. return _json.DeserializeFromFile<ContentUploadHistory>(path);
  140. }
  141. catch (IOException)
  142. {
  143. return new ContentUploadHistory
  144. {
  145. DeviceId = deviceId
  146. };
  147. }
  148. }
  149. }
  150. public void AddCameraUpload(string deviceId, LocalFileInfo file)
  151. {
  152. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  153. _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
  154. lock (_syncLock)
  155. {
  156. ContentUploadHistory history;
  157. try
  158. {
  159. history = _json.DeserializeFromFile<ContentUploadHistory>(path);
  160. }
  161. catch (IOException)
  162. {
  163. history = new ContentUploadHistory
  164. {
  165. DeviceId = deviceId
  166. };
  167. }
  168. history.DeviceId = deviceId;
  169. history.FilesUploaded.Add(file);
  170. _json.SerializeToFile(history, path);
  171. }
  172. }
  173. }
  174. }