EnvironmentService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.IO;
  3. using MediaBrowser.Model.IO;
  4. using MediaBrowser.Model.Net;
  5. using ServiceStack.ServiceHost;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. namespace MediaBrowser.Api
  12. {
  13. /// <summary>
  14. /// Class GetDirectoryContents
  15. /// </summary>
  16. [Route("/Environment/DirectoryContents", "GET")]
  17. public class GetDirectoryContents : IReturn<List<FileSystemEntryInfo>>
  18. {
  19. /// <summary>
  20. /// Gets or sets the path.
  21. /// </summary>
  22. /// <value>The path.</value>
  23. public string Path { get; set; }
  24. /// <summary>
  25. /// Gets or sets a value indicating whether [include files].
  26. /// </summary>
  27. /// <value><c>true</c> if [include files]; otherwise, <c>false</c>.</value>
  28. public bool IncludeFiles { get; set; }
  29. /// <summary>
  30. /// Gets or sets a value indicating whether [include directories].
  31. /// </summary>
  32. /// <value><c>true</c> if [include directories]; otherwise, <c>false</c>.</value>
  33. public bool IncludeDirectories { get; set; }
  34. /// <summary>
  35. /// Gets or sets a value indicating whether [include hidden].
  36. /// </summary>
  37. /// <value><c>true</c> if [include hidden]; otherwise, <c>false</c>.</value>
  38. public bool IncludeHidden { get; set; }
  39. }
  40. /// <summary>
  41. /// Class GetDrives
  42. /// </summary>
  43. [Route("/Environment/Drives", "GET")]
  44. public class GetDrives : IReturn<List<FileSystemEntryInfo>>
  45. {
  46. }
  47. /// <summary>
  48. /// Class GetNetworkComputers
  49. /// </summary>
  50. [Route("/Environment/NetworkComputers", "GET")]
  51. public class GetNetworkComputers : IReturn<List<FileSystemEntryInfo>>
  52. {
  53. }
  54. /// <summary>
  55. /// Class EnvironmentService
  56. /// </summary>
  57. public class EnvironmentService : BaseRestService
  58. {
  59. /// <summary>
  60. /// The _network manager
  61. /// </summary>
  62. private readonly INetworkManager _networkManager;
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="EnvironmentService" /> class.
  65. /// </summary>
  66. /// <param name="networkManager">The network manager.</param>
  67. /// <exception cref="System.ArgumentNullException">networkManager</exception>
  68. public EnvironmentService(INetworkManager networkManager)
  69. {
  70. if (networkManager == null)
  71. {
  72. throw new ArgumentNullException("networkManager");
  73. }
  74. _networkManager = networkManager;
  75. }
  76. /// <summary>
  77. /// Gets the specified request.
  78. /// </summary>
  79. /// <param name="request">The request.</param>
  80. /// <returns>System.Object.</returns>
  81. /// <exception cref="System.ArgumentNullException">Path</exception>
  82. /// <exception cref="System.ArgumentException"></exception>
  83. public object Get(GetDirectoryContents request)
  84. {
  85. var path = request.Path;
  86. if (string.IsNullOrEmpty(path))
  87. {
  88. throw new ArgumentNullException("Path");
  89. }
  90. // Reject invalid input
  91. if (!Path.IsPathRooted(path))
  92. {
  93. throw new ArgumentException(string.Format("Invalid path: {0}", path));
  94. }
  95. if (path.StartsWith(NetworkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf('\\') == 1)
  96. {
  97. return GetNetworkShares(path).ToList();
  98. }
  99. return GetFileSystemEntries(request).ToList();
  100. }
  101. /// <summary>
  102. /// Gets the specified request.
  103. /// </summary>
  104. /// <param name="request">The request.</param>
  105. /// <returns>System.Object.</returns>
  106. public object Get(GetDrives request)
  107. {
  108. return GetDrives().ToList();
  109. }
  110. /// <summary>
  111. /// Gets the specified request.
  112. /// </summary>
  113. /// <param name="request">The request.</param>
  114. /// <returns>System.Object.</returns>
  115. public object Get(GetNetworkComputers request)
  116. {
  117. return GetNetworkComputers().ToList();
  118. }
  119. /// <summary>
  120. /// Gets the list that is returned when an empty path is supplied
  121. /// </summary>
  122. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  123. private IEnumerable<FileSystemEntryInfo> GetDrives()
  124. {
  125. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  126. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemEntryInfo
  127. {
  128. Name = GetName(d),
  129. Path = d.RootDirectory.FullName,
  130. Type = FileSystemEntryType.Directory
  131. });
  132. }
  133. /// <summary>
  134. /// Gets the network computers.
  135. /// </summary>
  136. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  137. private IEnumerable<FileSystemEntryInfo> GetNetworkComputers()
  138. {
  139. return _networkManager.GetNetworkDevices().Select(c => new FileSystemEntryInfo
  140. {
  141. Name = c,
  142. Path = NetworkPrefix + c,
  143. Type = FileSystemEntryType.NetworkComputer
  144. });
  145. }
  146. /// <summary>
  147. /// Gets the name.
  148. /// </summary>
  149. /// <param name="drive">The drive.</param>
  150. /// <returns>System.String.</returns>
  151. private string GetName(DriveInfo drive)
  152. {
  153. return drive.Name;
  154. }
  155. /// <summary>
  156. /// Gets the network shares.
  157. /// </summary>
  158. /// <param name="path">The path.</param>
  159. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  160. private IEnumerable<FileSystemEntryInfo> GetNetworkShares(string path)
  161. {
  162. return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
  163. {
  164. Name = c.Name,
  165. Path = Path.Combine(path, c.Name),
  166. Type = FileSystemEntryType.NetworkShare
  167. });
  168. }
  169. /// <summary>
  170. /// Gets the file system entries.
  171. /// </summary>
  172. /// <param name="request">The request.</param>
  173. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  174. private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
  175. {
  176. var fileSystemEntries = FileSystem.GetFileSystemEntries(request.Path, "*", request.IncludeFiles, request.IncludeDirectories).Where(f => !f.IsSystemFile);
  177. if (!request.IncludeHidden)
  178. {
  179. fileSystemEntries = fileSystemEntries.Where(f => !f.IsHidden);
  180. }
  181. return fileSystemEntries.Select(f => new FileSystemEntryInfo
  182. {
  183. Name = f.cFileName,
  184. Path = f.Path,
  185. Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
  186. });
  187. }
  188. /// <summary>
  189. /// Gets the network prefix.
  190. /// </summary>
  191. /// <value>The network prefix.</value>
  192. private string NetworkPrefix
  193. {
  194. get { return Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); }
  195. }
  196. }
  197. }