EnvironmentService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. [Obsolete]
  50. [Route("/Environment/NetworkShares", "GET", Summary = "Gets shares from a network device")]
  51. public class GetNetworkShares : IReturn<List<FileSystemEntryInfo>>
  52. {
  53. /// <summary>
  54. /// Gets or sets the path.
  55. /// </summary>
  56. /// <value>The path.</value>
  57. [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  58. public string Path { get; set; }
  59. }
  60. /// <summary>
  61. /// Class GetDrives
  62. /// </summary>
  63. [Route("/Environment/Drives", "GET", Summary = "Gets available drives from the server's file system")]
  64. public class GetDrives : IReturn<List<FileSystemEntryInfo>>
  65. {
  66. }
  67. /// <summary>
  68. /// Class GetNetworkComputers
  69. /// </summary>
  70. [Route("/Environment/NetworkDevices", "GET", Summary = "Gets a list of devices on the network")]
  71. public class GetNetworkDevices : IReturn<List<FileSystemEntryInfo>>
  72. {
  73. }
  74. [Route("/Environment/ParentPath", "GET", Summary = "Gets the parent path of a given path")]
  75. public class GetParentPath : IReturn<string>
  76. {
  77. /// <summary>
  78. /// Gets or sets the path.
  79. /// </summary>
  80. /// <value>The path.</value>
  81. [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  82. public string Path { get; set; }
  83. }
  84. public class DefaultDirectoryBrowserInfo
  85. {
  86. public string Path { get; set; }
  87. }
  88. [Route("/Environment/DefaultDirectoryBrowser", "GET", Summary = "Gets the parent path of a given path")]
  89. public class GetDefaultDirectoryBrowser : IReturn<DefaultDirectoryBrowserInfo>
  90. {
  91. }
  92. /// <summary>
  93. /// Class EnvironmentService
  94. /// </summary>
  95. [Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]
  96. public class EnvironmentService : BaseApiService
  97. {
  98. const char UncSeparator = '\\';
  99. const string UncSeparatorString = "\\";
  100. /// <summary>
  101. /// The _network manager
  102. /// </summary>
  103. private readonly INetworkManager _networkManager;
  104. private readonly 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(nameof(networkManager));
  114. }
  115. _networkManager = networkManager;
  116. _fileSystem = fileSystem;
  117. }
  118. public void Post(ValidatePath request)
  119. {
  120. if (request.IsFile.HasValue)
  121. {
  122. if (request.IsFile.Value)
  123. {
  124. if (!File.Exists(request.Path))
  125. {
  126. throw new FileNotFoundException("File not found", request.Path);
  127. }
  128. }
  129. else
  130. {
  131. if (!Directory.Exists(request.Path))
  132. {
  133. throw new FileNotFoundException("File not found", request.Path);
  134. }
  135. }
  136. }
  137. else
  138. {
  139. if (!File.Exists(request.Path) && !Directory.Exists(request.Path))
  140. {
  141. throw new FileNotFoundException("Path not found", request.Path);
  142. }
  143. if (request.ValidateWriteable)
  144. {
  145. EnsureWriteAccess(request.Path);
  146. }
  147. }
  148. }
  149. protected void EnsureWriteAccess(string path)
  150. {
  151. var file = Path.Combine(path, Guid.NewGuid().ToString());
  152. File.WriteAllText(file, string.Empty);
  153. _fileSystem.DeleteFile(file);
  154. }
  155. public object Get(GetDefaultDirectoryBrowser request) =>
  156. ToOptimizedResult(new DefaultDirectoryBrowserInfo {Path = null});
  157. /// <summary>
  158. /// Gets the specified request.
  159. /// </summary>
  160. /// <param name="request">The request.</param>
  161. /// <returns>System.Object.</returns>
  162. public object Get(GetDirectoryContents request)
  163. {
  164. var path = request.Path;
  165. if (string.IsNullOrEmpty(path))
  166. {
  167. throw new ArgumentNullException(nameof(Path));
  168. }
  169. var networkPrefix = UncSeparatorString + UncSeparatorString;
  170. if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase)
  171. && path.LastIndexOf(UncSeparator) == 1)
  172. {
  173. return ToOptimizedResult(Array.Empty<FileSystemEntryInfo>());
  174. }
  175. return ToOptimizedResult(GetFileSystemEntries(request).ToList());
  176. }
  177. [Obsolete]
  178. public object Get(GetNetworkShares request)
  179. => ToOptimizedResult(Array.Empty<FileSystemEntryInfo>());
  180. /// <summary>
  181. /// Gets the specified request.
  182. /// </summary>
  183. /// <param name="request">The request.</param>
  184. /// <returns>System.Object.</returns>
  185. public object Get(GetDrives request)
  186. {
  187. var result = GetDrives().ToList();
  188. return ToOptimizedResult(result);
  189. }
  190. /// <summary>
  191. /// Gets the list that is returned when an empty path is supplied
  192. /// </summary>
  193. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  194. private IEnumerable<FileSystemEntryInfo> GetDrives()
  195. {
  196. return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo
  197. {
  198. Name = d.Name,
  199. Path = d.FullName,
  200. Type = FileSystemEntryType.Directory
  201. });
  202. }
  203. /// <summary>
  204. /// Gets the specified request.
  205. /// </summary>
  206. /// <param name="request">The request.</param>
  207. /// <returns>System.Object.</returns>
  208. public object Get(GetNetworkDevices request)
  209. => ToOptimizedResult(Array.Empty<FileSystemEntryInfo>());
  210. /// <summary>
  211. /// Gets the file system entries.
  212. /// </summary>
  213. /// <param name="request">The request.</param>
  214. /// <returns>IEnumerable{FileSystemEntryInfo}.</returns>
  215. private IEnumerable<FileSystemEntryInfo> GetFileSystemEntries(GetDirectoryContents request)
  216. {
  217. var entries = _fileSystem.GetFileSystemEntries(request.Path).OrderBy(i => i.FullName).Where(i =>
  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. });
  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. }