DeviceRepository.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Extensions;
  8. using MediaBrowser.Controller.Devices;
  9. using MediaBrowser.Model.Devices;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.Serialization;
  13. using MediaBrowser.Model.Session;
  14. namespace Emby.Server.Core.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 Dictionary<string, 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[device.Id] = device;
  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 = new Dictionary<string, DeviceInfo>(StringComparer.OrdinalIgnoreCase);
  82. var devices = LoadDevices().ToList();
  83. foreach (var device in devices)
  84. {
  85. _devices[device.Id] = device;
  86. }
  87. }
  88. return _devices.Values.ToList();
  89. }
  90. }
  91. private IEnumerable<DeviceInfo> LoadDevices()
  92. {
  93. var path = GetDevicesPath();
  94. try
  95. {
  96. return _fileSystem
  97. .GetFilePaths(path, true)
  98. .Where(i => string.Equals(Path.GetFileName(i), "device.json", StringComparison.OrdinalIgnoreCase))
  99. .ToList()
  100. .Select(i =>
  101. {
  102. try
  103. {
  104. return _json.DeserializeFromFile<DeviceInfo>(i);
  105. }
  106. catch (Exception ex)
  107. {
  108. _logger.ErrorException("Error reading {0}", ex, i);
  109. return null;
  110. }
  111. })
  112. .Where(i => i != null);
  113. }
  114. catch (IOException)
  115. {
  116. return new List<DeviceInfo>();
  117. }
  118. }
  119. public Task DeleteDevice(string id)
  120. {
  121. var path = GetDevicePath(id);
  122. lock (_syncLock)
  123. {
  124. try
  125. {
  126. _fileSystem.DeleteDirectory(path, true);
  127. }
  128. catch (DirectoryNotFoundException)
  129. {
  130. }
  131. _devices = null;
  132. }
  133. return Task.FromResult(true);
  134. }
  135. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  136. {
  137. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  138. lock (_syncLock)
  139. {
  140. try
  141. {
  142. return _json.DeserializeFromFile<ContentUploadHistory>(path);
  143. }
  144. catch (IOException)
  145. {
  146. return new ContentUploadHistory
  147. {
  148. DeviceId = deviceId
  149. };
  150. }
  151. }
  152. }
  153. public void AddCameraUpload(string deviceId, LocalFileInfo file)
  154. {
  155. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  156. _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
  157. lock (_syncLock)
  158. {
  159. ContentUploadHistory history;
  160. try
  161. {
  162. history = _json.DeserializeFromFile<ContentUploadHistory>(path);
  163. }
  164. catch (IOException)
  165. {
  166. history = new ContentUploadHistory
  167. {
  168. DeviceId = deviceId
  169. };
  170. }
  171. history.DeviceId = deviceId;
  172. history.FilesUploaded.Add(file);
  173. _json.SerializeToFile(history, path);
  174. }
  175. }
  176. }
  177. }