EnvironmentService.cs 8.8 KB

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