EnvironmentService.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.IO;
  4. using MediaBrowser.Model.IO;
  5. using ServiceStack.ServiceHost;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel.Composition;
  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. public class GetDirectoryContents : IReturn<List<FileSystemEntryInfo>>
  19. {
  20. /// <summary>
  21. /// Gets or sets the path.
  22. /// </summary>
  23. /// <value>The path.</value>
  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. 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. public bool IncludeDirectories { get; set; }
  35. /// <summary>
  36. /// Gets or sets a value indicating whether [include hidden].
  37. /// </summary>
  38. /// <value><c>true</c> if [include hidden]; otherwise, <c>false</c>.</value>
  39. public bool IncludeHidden { get; set; }
  40. }
  41. /// <summary>
  42. /// Class GetDrives
  43. /// </summary>
  44. [Route("/Environment/Drives", "GET")]
  45. public class GetDrives : IReturn<List<FileSystemEntryInfo>>
  46. {
  47. }
  48. /// <summary>
  49. /// Class GetNetworkComputers
  50. /// </summary>
  51. [Route("/Environment/NetworkComputers", "GET")]
  52. public class GetNetworkComputers : IReturn<List<FileSystemEntryInfo>>
  53. {
  54. }
  55. /// <summary>
  56. /// Class EnvironmentService
  57. /// </summary>
  58. [Export(typeof(IRestfulService))]
  59. public class EnvironmentService : BaseRestService
  60. {
  61. /// <summary>
  62. /// Gets the specified request.
  63. /// </summary>
  64. /// <param name="request">The request.</param>
  65. /// <returns>System.Object.</returns>
  66. /// <exception cref="System.ArgumentNullException">Path</exception>
  67. /// <exception cref="System.ArgumentException"></exception>
  68. public object Get(GetDirectoryContents request)
  69. {
  70. var path = request.Path;
  71. if (string.IsNullOrEmpty(path))
  72. {
  73. throw new ArgumentNullException("Path");
  74. }
  75. // Reject invalid input
  76. if (!Path.IsPathRooted(path))
  77. {
  78. throw new ArgumentException(string.Format("Invalid path: {0}", path));
  79. }
  80. if (path.StartsWith(NetworkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf('\\') == 1)
  81. {
  82. return GetNetworkShares(path).ToList();
  83. }
  84. return GetFileSystemEntries(request).ToList();
  85. }
  86. /// <summary>
  87. /// Gets the specified request.
  88. /// </summary>
  89. /// <param name="request">The request.</param>
  90. /// <returns>System.Object.</returns>
  91. public object Get(GetDrives request)
  92. {
  93. return GetDrives().ToList();
  94. }
  95. /// <summary>
  96. /// Gets the specified request.
  97. /// </summary>
  98. /// <param name="request">The request.</param>
  99. /// <returns>System.Object.</returns>
  100. public object Get(GetNetworkComputers request)
  101. {
  102. return GetNetworkComputers().ToList();
  103. }
  104. /// <summary>
  105. /// Gets the list that is returned when an empty path is supplied
  106. /// </summary>
  107. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  108. private IEnumerable<FileSystemEntryInfo> GetDrives()
  109. {
  110. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  111. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemEntryInfo
  112. {
  113. Name = GetName(d),
  114. Path = d.RootDirectory.FullName,
  115. Type = FileSystemEntryType.Directory
  116. });
  117. }
  118. /// <summary>
  119. /// Gets the network computers.
  120. /// </summary>
  121. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  122. private IEnumerable<FileSystemEntryInfo> GetNetworkComputers()
  123. {
  124. return NetUtils.GetNetworkComputers().Select(c => new FileSystemEntryInfo
  125. {
  126. Name = c,
  127. Path = NetworkPrefix + c,
  128. Type = FileSystemEntryType.NetworkComputer
  129. });
  130. }
  131. /// <summary>
  132. /// Gets the name.
  133. /// </summary>
  134. /// <param name="drive">The drive.</param>
  135. /// <returns>System.String.</returns>
  136. private string GetName(DriveInfo drive)
  137. {
  138. return drive.Name;
  139. }
  140. /// <summary>
  141. /// Gets the network shares.
  142. /// </summary>
  143. /// <param name="path">The path.</param>
  144. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  145. private IEnumerable<FileSystemEntryInfo> GetNetworkShares(string path)
  146. {
  147. return new ShareCollection(path).OfType<Share>().Where(s => s.ShareType == ShareType.Disk).Select(c => new FileSystemEntryInfo
  148. {
  149. Name = c.NetName,
  150. Path = Path.Combine(path, c.NetName),
  151. Type = FileSystemEntryType.NetworkShare
  152. });
  153. }
  154. /// <summary>
  155. /// Gets the file system entries.
  156. /// </summary>
  157. /// <param name="request">The request.</param>
  158. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  159. private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
  160. {
  161. var fileSystemEntries = FileSystem.GetFileSystemEntries(request.Path, "*", request.IncludeFiles, request.IncludeDirectories).Where(f => !f.IsSystemFile);
  162. if (!request.IncludeHidden)
  163. {
  164. fileSystemEntries = fileSystemEntries.Where(f => !f.IsHidden);
  165. }
  166. return fileSystemEntries.Select(f => new FileSystemEntryInfo
  167. {
  168. Name = f.cFileName,
  169. Path = f.Path,
  170. Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
  171. });
  172. }
  173. /// <summary>
  174. /// Gets the network prefix.
  175. /// </summary>
  176. /// <value>The network prefix.</value>
  177. private string NetworkPrefix
  178. {
  179. get { return Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); }
  180. }
  181. }
  182. }