EnvironmentService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Net;
  3. using MediaBrowser.Model.IO;
  4. using MediaBrowser.Model.Net;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using MediaBrowser.Model.Services;
  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. public GetDirectoryContents()
  43. {
  44. IncludeHidden = true;
  45. }
  46. }
  47. [Route("/Environment/NetworkShares", "GET", Summary = "Gets shares from a network device")]
  48. public class GetNetworkShares : IReturn<List<FileSystemEntryInfo>>
  49. {
  50. /// <summary>
  51. /// Gets or sets the path.
  52. /// </summary>
  53. /// <value>The path.</value>
  54. [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  55. public string Path { get; set; }
  56. }
  57. /// <summary>
  58. /// Class GetDrives
  59. /// </summary>
  60. [Route("/Environment/Drives", "GET", Summary = "Gets available drives from the server's file system")]
  61. public class GetDrives : IReturn<List<FileSystemEntryInfo>>
  62. {
  63. }
  64. /// <summary>
  65. /// Class GetNetworkComputers
  66. /// </summary>
  67. [Route("/Environment/NetworkDevices", "GET", Summary = "Gets a list of devices on the network")]
  68. public class GetNetworkDevices : IReturn<List<FileSystemEntryInfo>>
  69. {
  70. }
  71. [Route("/Environment/ParentPath", "GET", Summary = "Gets the parent path of a given path")]
  72. public class GetParentPath : IReturn<string>
  73. {
  74. /// <summary>
  75. /// Gets or sets the path.
  76. /// </summary>
  77. /// <value>The path.</value>
  78. [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  79. public string Path { get; set; }
  80. }
  81. public class DefaultDirectoryBrowserInfo
  82. {
  83. public string Path { get; set; }
  84. }
  85. [Route("/Environment/DefaultDirectoryBrowser", "GET", Summary = "Gets the parent path of a given path")]
  86. public class GetDefaultDirectoryBrowser : IReturn<DefaultDirectoryBrowserInfo>
  87. {
  88. }
  89. /// <summary>
  90. /// Class EnvironmentService
  91. /// </summary>
  92. [Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]
  93. public class EnvironmentService : BaseApiService
  94. {
  95. const char UncSeparator = '\\';
  96. const string UncSeparatorString = "\\";
  97. /// <summary>
  98. /// The _network manager
  99. /// </summary>
  100. private readonly INetworkManager _networkManager;
  101. private IFileSystem _fileSystem;
  102. /// <summary>
  103. /// Initializes a new instance of the <see cref="EnvironmentService" /> class.
  104. /// </summary>
  105. /// <param name="networkManager">The network manager.</param>
  106. public EnvironmentService(INetworkManager networkManager, IFileSystem fileSystem)
  107. {
  108. if (networkManager == null)
  109. {
  110. throw new ArgumentNullException("networkManager");
  111. }
  112. _networkManager = networkManager;
  113. _fileSystem = fileSystem;
  114. }
  115. public object Get(GetDefaultDirectoryBrowser request)
  116. {
  117. var result = new DefaultDirectoryBrowserInfo();
  118. try
  119. {
  120. var qnap = "/share/CACHEDEV1_DATA";
  121. if (_fileSystem.DirectoryExists(qnap))
  122. {
  123. result.Path = qnap;
  124. }
  125. }
  126. catch
  127. {
  128. }
  129. return ToOptimizedResult(result);
  130. }
  131. /// <summary>
  132. /// Gets the specified request.
  133. /// </summary>
  134. /// <param name="request">The request.</param>
  135. /// <returns>System.Object.</returns>
  136. public object Get(GetDirectoryContents request)
  137. {
  138. var path = request.Path;
  139. if (string.IsNullOrEmpty(path))
  140. {
  141. throw new ArgumentNullException("Path");
  142. }
  143. var networkPrefix = UncSeparatorString + UncSeparatorString;
  144. if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1)
  145. {
  146. return ToOptimizedSerializedResultUsingCache(GetNetworkShares(path).OrderBy(i => i.Path).ToList());
  147. }
  148. return ToOptimizedSerializedResultUsingCache(GetFileSystemEntries(request).OrderBy(i => i.Path).ToList());
  149. }
  150. public object Get(GetNetworkShares request)
  151. {
  152. var path = request.Path;
  153. var shares = GetNetworkShares(path).OrderBy(i => i.Path).ToList();
  154. return ToOptimizedSerializedResultUsingCache(shares);
  155. }
  156. /// <summary>
  157. /// Gets the specified request.
  158. /// </summary>
  159. /// <param name="request">The request.</param>
  160. /// <returns>System.Object.</returns>
  161. public object Get(GetDrives request)
  162. {
  163. var result = GetDrives().ToList();
  164. return ToOptimizedSerializedResultUsingCache(result);
  165. }
  166. /// <summary>
  167. /// Gets the list that is returned when an empty path is supplied
  168. /// </summary>
  169. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  170. private IEnumerable<FileSystemEntryInfo> GetDrives()
  171. {
  172. return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo
  173. {
  174. Name = d.Name,
  175. Path = d.FullName,
  176. Type = FileSystemEntryType.Directory
  177. });
  178. }
  179. /// <summary>
  180. /// Gets the specified request.
  181. /// </summary>
  182. /// <param name="request">The request.</param>
  183. /// <returns>System.Object.</returns>
  184. public object Get(GetNetworkDevices request)
  185. {
  186. var result = _networkManager.GetNetworkDevices()
  187. .OrderBy(i => i.Path)
  188. .ToList();
  189. return ToOptimizedSerializedResultUsingCache(result);
  190. }
  191. /// <summary>
  192. /// Gets the network shares.
  193. /// </summary>
  194. /// <param name="path">The path.</param>
  195. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  196. private IEnumerable<FileSystemEntryInfo> GetNetworkShares(string path)
  197. {
  198. return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
  199. {
  200. Name = c.Name,
  201. Path = Path.Combine(path, c.Name),
  202. Type = FileSystemEntryType.NetworkShare
  203. });
  204. }
  205. /// <summary>
  206. /// Gets the file system entries.
  207. /// </summary>
  208. /// <param name="request">The request.</param>
  209. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  210. private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
  211. {
  212. // using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
  213. var entries = _fileSystem.GetFileSystemEntries(request.Path).Where(i =>
  214. {
  215. if (!request.IncludeHidden && i.IsHidden)
  216. {
  217. return false;
  218. }
  219. var isDirectory = i.IsDirectory;
  220. if (!request.IncludeFiles && !isDirectory)
  221. {
  222. return false;
  223. }
  224. if (!request.IncludeDirectories && isDirectory)
  225. {
  226. return false;
  227. }
  228. return true;
  229. });
  230. return entries.Select(f => new FileSystemEntryInfo
  231. {
  232. Name = f.Name,
  233. Path = f.FullName,
  234. Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
  235. }).ToList();
  236. }
  237. public object Get(GetParentPath request)
  238. {
  239. var parent = Path.GetDirectoryName(request.Path);
  240. if (string.IsNullOrEmpty(parent))
  241. {
  242. // Check if unc share
  243. var index = request.Path.LastIndexOf(UncSeparator);
  244. if (index != -1 && request.Path.IndexOf(UncSeparator) == 0)
  245. {
  246. parent = request.Path.Substring(0, index);
  247. if (string.IsNullOrWhiteSpace(parent.TrimStart(UncSeparator)))
  248. {
  249. parent = null;
  250. }
  251. }
  252. }
  253. return parent;
  254. }
  255. }
  256. }