EnvironmentService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.IO;
  3. using MediaBrowser.Model.IO;
  4. using MediaBrowser.Model.Net;
  5. using MediaBrowser.Server.Implementations.HttpServer;
  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. if (path.StartsWith(NetworkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf('\\') == 1)
  92. {
  93. return ToOptimizedResult(GetNetworkShares(path).ToList());
  94. }
  95. // Reject invalid input
  96. if (!Path.IsPathRooted(path))
  97. {
  98. throw new ArgumentException(string.Format("Invalid path: {0}", path));
  99. }
  100. return ToOptimizedResult(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. var result = GetDrives().ToList();
  110. return ToOptimizedResult(result);
  111. }
  112. /// <summary>
  113. /// Gets the specified request.
  114. /// </summary>
  115. /// <param name="request">The request.</param>
  116. /// <returns>System.Object.</returns>
  117. public object Get(GetNetworkComputers request)
  118. {
  119. var result = GetNetworkComputers().ToList();
  120. return ToOptimizedResult(result);
  121. }
  122. /// <summary>
  123. /// Gets the list that is returned when an empty path is supplied
  124. /// </summary>
  125. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  126. private IEnumerable<FileSystemEntryInfo> GetDrives()
  127. {
  128. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  129. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemEntryInfo
  130. {
  131. Name = GetName(d),
  132. Path = d.RootDirectory.FullName,
  133. Type = FileSystemEntryType.Directory
  134. });
  135. }
  136. /// <summary>
  137. /// Gets the network computers.
  138. /// </summary>
  139. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  140. private IEnumerable<FileSystemEntryInfo> GetNetworkComputers()
  141. {
  142. return _networkManager.GetNetworkDevices().Select(c => new FileSystemEntryInfo
  143. {
  144. Name = c,
  145. Path = NetworkPrefix + c,
  146. Type = FileSystemEntryType.NetworkComputer
  147. });
  148. }
  149. /// <summary>
  150. /// Gets the name.
  151. /// </summary>
  152. /// <param name="drive">The drive.</param>
  153. /// <returns>System.String.</returns>
  154. private string GetName(DriveInfo drive)
  155. {
  156. return drive.Name;
  157. }
  158. /// <summary>
  159. /// Gets the network shares.
  160. /// </summary>
  161. /// <param name="path">The path.</param>
  162. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  163. private IEnumerable<FileSystemEntryInfo> GetNetworkShares(string path)
  164. {
  165. return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
  166. {
  167. Name = c.Name,
  168. Path = Path.Combine(path, c.Name),
  169. Type = FileSystemEntryType.NetworkShare
  170. });
  171. }
  172. /// <summary>
  173. /// Gets the file system entries.
  174. /// </summary>
  175. /// <param name="request">The request.</param>
  176. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  177. private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
  178. {
  179. var fileSystemEntries = FileSystem.GetFileSystemEntries(request.Path, "*", request.IncludeFiles, request.IncludeDirectories).Where(f => !f.IsSystemFile);
  180. if (!request.IncludeHidden)
  181. {
  182. fileSystemEntries = fileSystemEntries.Where(f => !f.IsHidden);
  183. }
  184. return fileSystemEntries.Select(f => new FileSystemEntryInfo
  185. {
  186. Name = f.cFileName,
  187. Path = f.Path,
  188. Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
  189. });
  190. }
  191. /// <summary>
  192. /// Gets the network prefix.
  193. /// </summary>
  194. /// <value>The network prefix.</value>
  195. private string NetworkPrefix
  196. {
  197. get { return Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); }
  198. }
  199. }
  200. }