2
0

EnvironmentService.cs 6.7 KB

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