EnvironmentService.cs 11 KB

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