EnvironmentService.cs 7.8 KB

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