2
0

DeviceRepository.cs 5.8 KB

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