EnvironmentService.cs 9.9 KB

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