EnvironmentService.cs 7.4 KB

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