EnvironmentService.cs 8.6 KB

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