EnvironmentService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if (Environment.OSVersion.Platform == PlatformID.Unix)
  122. {
  123. try
  124. {
  125. var qnap = "/share/CACHEDEV1_DATA";
  126. if (Directory.Exists(qnap))
  127. {
  128. result.Path = qnap;
  129. }
  130. }
  131. catch
  132. {
  133. }
  134. }
  135. return ToOptimizedResult(result);
  136. }
  137. /// <summary>
  138. /// Gets the specified request.
  139. /// </summary>
  140. /// <param name="request">The request.</param>
  141. /// <returns>System.Object.</returns>
  142. public object Get(GetDirectoryContents request)
  143. {
  144. var path = request.Path;
  145. if (string.IsNullOrEmpty(path))
  146. {
  147. throw new ArgumentNullException("Path");
  148. }
  149. var networkPrefix = UncSeparator.ToString(CultureInfo.InvariantCulture) + UncSeparator.ToString(CultureInfo.InvariantCulture);
  150. if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1)
  151. {
  152. return ToOptimizedSerializedResultUsingCache(GetNetworkShares(path).OrderBy(i => i.Path).ToList());
  153. }
  154. return ToOptimizedSerializedResultUsingCache(GetFileSystemEntries(request).OrderBy(i => i.Path).ToList());
  155. }
  156. public object Get(GetNetworkShares request)
  157. {
  158. var path = request.Path;
  159. var shares = GetNetworkShares(path).OrderBy(i => i.Path).ToList();
  160. return ToOptimizedSerializedResultUsingCache(shares);
  161. }
  162. /// <summary>
  163. /// Gets the specified request.
  164. /// </summary>
  165. /// <param name="request">The request.</param>
  166. /// <returns>System.Object.</returns>
  167. public object Get(GetDrives request)
  168. {
  169. var result = GetDrives().ToList();
  170. return ToOptimizedSerializedResultUsingCache(result);
  171. }
  172. /// <summary>
  173. /// Gets the list that is returned when an empty path is supplied
  174. /// </summary>
  175. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  176. private IEnumerable<FileSystemEntryInfo> GetDrives()
  177. {
  178. // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
  179. return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemEntryInfo
  180. {
  181. Name = GetName(d),
  182. Path = d.RootDirectory.FullName,
  183. Type = FileSystemEntryType.Directory
  184. });
  185. }
  186. /// <summary>
  187. /// Gets the specified request.
  188. /// </summary>
  189. /// <param name="request">The request.</param>
  190. /// <returns>System.Object.</returns>
  191. public object Get(GetNetworkDevices request)
  192. {
  193. var result = _networkManager.GetNetworkDevices()
  194. .OrderBy(i => i.Path)
  195. .ToList();
  196. return ToOptimizedSerializedResultUsingCache(result);
  197. }
  198. /// <summary>
  199. /// Gets the name.
  200. /// </summary>
  201. /// <param name="drive">The drive.</param>
  202. /// <returns>System.String.</returns>
  203. private string GetName(DriveInfo drive)
  204. {
  205. return drive.Name;
  206. }
  207. /// <summary>
  208. /// Gets the network shares.
  209. /// </summary>
  210. /// <param name="path">The path.</param>
  211. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  212. private IEnumerable<FileSystemEntryInfo> GetNetworkShares(string path)
  213. {
  214. return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
  215. {
  216. Name = c.Name,
  217. Path = Path.Combine(path, c.Name),
  218. Type = FileSystemEntryType.NetworkShare
  219. });
  220. }
  221. /// <summary>
  222. /// Gets the file system entries.
  223. /// </summary>
  224. /// <param name="request">The request.</param>
  225. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  226. private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
  227. {
  228. // using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
  229. var entries = _fileSystem.GetFileSystemEntries(request.Path).Where(i =>
  230. {
  231. if (!request.IncludeHidden && i.IsHidden)
  232. {
  233. return false;
  234. }
  235. var isDirectory = i.IsDirectory;
  236. if (!request.IncludeFiles && !isDirectory)
  237. {
  238. return false;
  239. }
  240. if (!request.IncludeDirectories && isDirectory)
  241. {
  242. return false;
  243. }
  244. return true;
  245. });
  246. return entries.Select(f => new FileSystemEntryInfo
  247. {
  248. Name = f.Name,
  249. Path = f.FullName,
  250. Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File
  251. }).ToList();
  252. }
  253. public object Get(GetParentPath request)
  254. {
  255. var parent = Path.GetDirectoryName(request.Path);
  256. if (string.IsNullOrEmpty(parent))
  257. {
  258. // Check if unc share
  259. var index = request.Path.LastIndexOf(UncSeparator);
  260. if (index != -1 && request.Path.IndexOf(UncSeparator) == 0)
  261. {
  262. parent = request.Path.Substring(0, index);
  263. if (string.IsNullOrWhiteSpace(parent.TrimStart(UncSeparator)))
  264. {
  265. parent = null;
  266. }
  267. }
  268. }
  269. return parent;
  270. }
  271. }
  272. }