2
0

EnvironmentService.cs 6.8 KB

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