DeviceRepository.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. using MediaBrowser.Model.Extensions;
  15. namespace Emby.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 Dictionary<string, 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. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
  44. lock (_syncLock)
  45. {
  46. _json.SerializeToFile(device, path);
  47. _devices[device.Id] = device;
  48. }
  49. return Task.FromResult(true);
  50. }
  51. public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  52. {
  53. var device = GetDevice(reportedId);
  54. if (device == null)
  55. {
  56. throw new ArgumentException("No device has been registed with id " + reportedId);
  57. }
  58. device.Capabilities = capabilities;
  59. SaveDevice(device);
  60. return Task.FromResult(true);
  61. }
  62. public ClientCapabilities GetCapabilities(string reportedId)
  63. {
  64. var device = GetDevice(reportedId);
  65. return device == null ? null : device.Capabilities;
  66. }
  67. public DeviceInfo GetDevice(string id)
  68. {
  69. if (string.IsNullOrWhiteSpace(id))
  70. {
  71. throw new ArgumentNullException("id");
  72. }
  73. return GetDevices()
  74. .FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
  75. }
  76. public IEnumerable<DeviceInfo> GetDevices()
  77. {
  78. lock (_syncLock)
  79. {
  80. if (_devices == null)
  81. {
  82. _devices = new Dictionary<string, DeviceInfo>(StringComparer.OrdinalIgnoreCase);
  83. var devices = LoadDevices().ToList();
  84. foreach (var device in devices)
  85. {
  86. _devices[device.Id] = device;
  87. }
  88. }
  89. return _devices.Values.ToList();
  90. }
  91. }
  92. private IEnumerable<DeviceInfo> LoadDevices()
  93. {
  94. var path = GetDevicesPath();
  95. try
  96. {
  97. return _fileSystem
  98. .GetFilePaths(path, true)
  99. .Where(i => string.Equals(Path.GetFileName(i), "device.json", StringComparison.OrdinalIgnoreCase))
  100. .ToList()
  101. .Select(i =>
  102. {
  103. try
  104. {
  105. return _json.DeserializeFromFile<DeviceInfo>(i);
  106. }
  107. catch (Exception ex)
  108. {
  109. _logger.ErrorException("Error reading {0}", ex, i);
  110. return null;
  111. }
  112. })
  113. .Where(i => i != null);
  114. }
  115. catch (IOException)
  116. {
  117. return new List<DeviceInfo>();
  118. }
  119. }
  120. public Task DeleteDevice(string id)
  121. {
  122. var path = GetDevicePath(id);
  123. lock (_syncLock)
  124. {
  125. try
  126. {
  127. _fileSystem.DeleteDirectory(path, true);
  128. }
  129. catch (IOException)
  130. {
  131. }
  132. _devices = null;
  133. }
  134. return Task.FromResult(true);
  135. }
  136. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  137. {
  138. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  139. lock (_syncLock)
  140. {
  141. try
  142. {
  143. return _json.DeserializeFromFile<ContentUploadHistory>(path);
  144. }
  145. catch (IOException)
  146. {
  147. return new ContentUploadHistory
  148. {
  149. DeviceId = deviceId
  150. };
  151. }
  152. }
  153. }
  154. public void AddCameraUpload(string deviceId, LocalFileInfo file)
  155. {
  156. var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
  157. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
  158. lock (_syncLock)
  159. {
  160. ContentUploadHistory history;
  161. try
  162. {
  163. history = _json.DeserializeFromFile<ContentUploadHistory>(path);
  164. }
  165. catch (IOException)
  166. {
  167. history = new ContentUploadHistory
  168. {
  169. DeviceId = deviceId
  170. };
  171. }
  172. history.DeviceId = deviceId;
  173. var list = history.FilesUploaded.ToList();
  174. list.Add(file);
  175. history.FilesUploaded = list.ToArray(list.Count);
  176. _json.SerializeToFile(history, path);
  177. }
  178. }
  179. }
  180. }