EnvironmentService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Model.IO;
  3. using MediaBrowser.Model.Net;
  4. using ServiceStack;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. namespace MediaBrowser.Api
  11. {
  12. /// <summary>
  13. /// Class GetDirectoryContents
  14. /// </summary>
  15. [Route("/Environment/DirectoryContents", "GET")]
  16. [Api(Description = "Gets the contents of a given directory in the file system")]
  17. public class GetDirectoryContents : IReturn<List<FileSystemEntryInfo>>
  18. {
  19. /// <summary>
  20. /// Gets or sets the path.
  21. /// </summary>
  22. /// <value>The path.</value>
  23. [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  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. [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")]
  30. public bool IncludeFiles { get; set; }
  31. /// <summary>
  32. /// Gets or sets a value indicating whether [include directories].
  33. /// </summary>
  34. /// <value><c>true</c> if [include directories]; otherwise, <c>false</c>.</value>
  35. [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")]
  36. public bool IncludeDirectories { get; set; }
  37. /// <summary>
  38. /// Gets or sets a value indicating whether [include hidden].
  39. /// </summary>
  40. /// <value><c>true</c> if [include hidden]; otherwise, <c>false</c>.</value>
  41. [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")]
  42. public bool IncludeHidden { get; set; }
  43. }
  44. [Route("/Environment/NetworkShares", "GET")]
  45. [Api(Description = "Gets shares from a network device")]
  46. public class GetNetworkShares : IReturn<List<FileSystemEntryInfo>>
  47. {
  48. /// <summary>
  49. /// Gets or sets the path.
  50. /// </summary>
  51. /// <value>The path.</value>
  52. [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  53. public string Path { get; set; }
  54. }
  55. /// <summary>
  56. /// Class GetDrives
  57. /// </summary>
  58. [Route("/Environment/Drives", "GET")]
  59. [Api(Description = "Gets available drives from the server's file system")]
  60. public class GetDrives : IReturn<List<FileSystemEntryInfo>>
  61. {
  62. }
  63. /// <summary>
  64. /// Class GetNetworkComputers
  65. /// </summary>
  66. [Route("/Environment/NetworkDevices", "GET")]
  67. [Api(Description = "Gets a list of devices on the network")]
  68. public class GetNetworkDevices : IReturn<List<FileSystemEntryInfo>>
  69. {
  70. }
  71. [Route("/Environment/ParentPath", "GET")]
  72. [Api(Description = "Gets the parent path of a given path")]
  73. public class GetParentPath : IReturn<string>
  74. {
  75. /// <summary>
  76. /// Gets or sets the path.
  77. /// </summary>
  78. /// <value>The path.</value>
  79. [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  80. public string Path { get; set; }
  81. }
  82. /// <summary>
  83. /// Class EnvironmentService
  84. /// </summary>
  85. public class EnvironmentService : BaseApiService
  86. {
  87. const char UncSeparator = '\\';
  88. /// <summary>
  89. /// The _network manager
  90. /// </summary>
  91. private readonly INetworkManager _networkManager;
  92. /// <summary>
  93. /// Initializes a new instance of the <see cref="EnvironmentService" /> class.
  94. /// </summary>
  95. /// <param name="networkManager">The network manager.</param>
  96. /// <exception cref="System.ArgumentNullException">networkManager</exception>
  97. public EnvironmentService(INetworkManager networkManager)
  98. {
  99. if (networkManager == null)
  100. {
  101. throw new ArgumentNullException("networkManager");
  102. }
  103. _networkManager = networkManager;
  104. }
  105. /// <summary>
  106. /// Gets the specified request.
  107. /// </summary>
  108. /// <param name="request">The request.</param>
  109. /// <returns>System.Object.</returns>
  110. /// <exception cref="System.ArgumentNullException">Path</exception>
  111. /// <exception cref="System.ArgumentException"></exception>
  112. public object Get(GetDirectoryContents request)
  113. {
  114. var path = request.Path;
  115. if (string.IsNullOrEmpty(path))
  116. {
  117. throw new ArgumentNullException("Path");
  118. }
  119. var networkPrefix = UncSeparator.ToString(CultureInfo.InvariantCulture) + UncSeparator.ToString(CultureInfo.InvariantCulture);
  120. if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1)
  121. {
  122. return ToOptimizedResult(GetNetworkShares(path).OrderBy(i => i.Path).ToList());
  123. }
  124. return ToOptimizedResult(GetFileSystemEntries(request).OrderBy(i => i.Path).ToList());
  125. }
  126. public object Get(GetNetworkShares request)
  127. {
  128. var path = request.Path;
  129. var shares = GetNetworkShares(path).OrderBy(i => i.Path).ToList();
  130. return ToOptimizedResult(shares);
  131. }
  132. /// <summary>
  133. /// Gets the specified request.
  134. /// </summary>
  135. /// <param name="request">The request.</param>
  136. /// <returns>System.Object.</returns>
  137. public object Get(GetDrives request)
  138. {
  139. var result = GetDrives().ToList();
  140. return ToOptimizedResult(result);
  141. }
  142. /// <summary>
  143. /// Gets the list that is returned when an empty path is supplied
  144. /// </summary>
  145. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  146. private IEnumerable<FileSystemEntryInfo> GetDrives()
  147. {
  148. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  149. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemEntryInfo
  150. {
  151. Name = GetName(d),
  152. Path = d.RootDirectory.FullName,
  153. Type = FileSystemEntryType.Directory
  154. });
  155. }
  156. /// <summary>
  157. /// Gets the specified request.
  158. /// </summary>
  159. /// <param name="request">The request.</param>
  160. /// <returns>System.Object.</returns>
  161. public object Get(GetNetworkDevices request)
  162. {
  163. var result = _networkManager.GetNetworkDevices()
  164. .OrderBy(i => i.Path)
  165. .ToList();
  166. return ToOptimizedResult(result);
  167. }
  168. /// <summary>
  169. /// Gets the name.
  170. /// </summary>
  171. /// <param name="drive">The drive.</param>
  172. /// <returns>System.String.</returns>
  173. private string GetName(DriveInfo drive)
  174. {
  175. return drive.Name;
  176. }
  177. /// <summary>
  178. /// Gets the network shares.
  179. /// </summary>
  180. /// <param name="path">The path.</param>
  181. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  182. private IEnumerable<FileSystemEntryInfo> GetNetworkShares(string path)
  183. {
  184. return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
  185. {
  186. Name = c.Name,
  187. Path = Path.Combine(path, c.Name),
  188. Type = FileSystemEntryType.NetworkShare
  189. });
  190. }
  191. /// <summary>
  192. /// Gets the file system entries.
  193. /// </summary>
  194. /// <param name="request">The request.</param>
  195. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  196. private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
  197. {
  198. var entries = new DirectoryInfo(request.Path).EnumerateFileSystemInfos().Where(i =>
  199. {
  200. if (!request.IncludeHidden && i.Attributes.HasFlag(FileAttributes.Hidden))
  201. {
  202. return false;
  203. }
  204. var isDirectory = i.Attributes.HasFlag(FileAttributes.Directory);
  205. if (!request.IncludeFiles && !isDirectory)
  206. {
  207. return false;
  208. }
  209. if (!request.IncludeDirectories && isDirectory)
  210. {
  211. return false;
  212. }
  213. return true;
  214. });
  215. return entries.Select(f => new FileSystemEntryInfo
  216. {
  217. Name = f.Name,
  218. Path = f.FullName,
  219. Type = f.Attributes.HasFlag(FileAttributes.Directory) ? FileSystemEntryType.Directory : FileSystemEntryType.File
  220. }).ToList();
  221. }
  222. public object Get(GetParentPath request)
  223. {
  224. var parent = Path.GetDirectoryName(request.Path);
  225. if (string.IsNullOrEmpty(parent))
  226. {
  227. // Check if unc share
  228. var index = request.Path.LastIndexOf(UncSeparator);
  229. if (index != -1 && request.Path.IndexOf(UncSeparator) == 0)
  230. {
  231. parent = request.Path.Substring(0, index);
  232. if (string.IsNullOrWhiteSpace(parent.TrimStart(UncSeparator)))
  233. {
  234. parent = null;
  235. }
  236. }
  237. }
  238. return parent;
  239. }
  240. }
  241. }