EnvironmentService.cs 11 KB

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