EnvironmentService.cs 8.7 KB

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