EnvironmentController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Jellyfin.Api.Constants;
  7. using Jellyfin.Api.Models.EnvironmentDtos;
  8. using MediaBrowser.Model.IO;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.AspNetCore.Mvc.ModelBinding;
  13. using Microsoft.Extensions.Logging;
  14. namespace Jellyfin.Api.Controllers
  15. {
  16. /// <summary>
  17. /// Environment Controller.
  18. /// </summary>
  19. [Authorize(Policy = Policies.RequiresElevation)]
  20. public class EnvironmentController : BaseJellyfinApiController
  21. {
  22. private const char UncSeparator = '\\';
  23. private const string UncStartPrefix = @"\\";
  24. private readonly IFileSystem _fileSystem;
  25. private readonly ILogger<EnvironmentController> _logger;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="EnvironmentController"/> class.
  28. /// </summary>
  29. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  30. /// <param name="logger">Instance of the <see cref="ILogger{EnvironmentController}"/> interface.</param>
  31. public EnvironmentController(IFileSystem fileSystem, ILogger<EnvironmentController> logger)
  32. {
  33. _fileSystem = fileSystem;
  34. _logger = logger;
  35. }
  36. /// <summary>
  37. /// Gets the contents of a given directory in the file system.
  38. /// </summary>
  39. /// <param name="path">The path.</param>
  40. /// <param name="includeFiles">An optional filter to include or exclude files from the results. true/false.</param>
  41. /// <param name="includeDirectories">An optional filter to include or exclude folders from the results. true/false.</param>
  42. /// <response code="200">Directory contents returned.</response>
  43. /// <returns>Directory contents.</returns>
  44. [HttpGet("DirectoryContents")]
  45. [ProducesResponseType(StatusCodes.Status200OK)]
  46. public IEnumerable<FileSystemEntryInfo> GetDirectoryContents(
  47. [FromQuery, BindRequired] string path,
  48. [FromQuery] bool includeFiles = false,
  49. [FromQuery] bool includeDirectories = false)
  50. {
  51. if (path.StartsWith(UncStartPrefix, StringComparison.OrdinalIgnoreCase)
  52. && path.LastIndexOf(UncSeparator) == 1)
  53. {
  54. return Array.Empty<FileSystemEntryInfo>();
  55. }
  56. var entries =
  57. _fileSystem.GetFileSystemEntries(path)
  58. .Where(i => (i.IsDirectory && includeDirectories) || (!i.IsDirectory && includeFiles))
  59. .OrderBy(i => i.FullName);
  60. return entries.Select(f => new FileSystemEntryInfo(f.Name, f.FullName, f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File));
  61. }
  62. /// <summary>
  63. /// Validates path.
  64. /// </summary>
  65. /// <param name="validatePathDto">Validate request object.</param>
  66. /// <response code="200">Path validated.</response>
  67. /// <response code="404">Path not found.</response>
  68. /// <returns>Validation status.</returns>
  69. [HttpPost("ValidatePath")]
  70. [ProducesResponseType(StatusCodes.Status200OK)]
  71. [ProducesResponseType(StatusCodes.Status404NotFound)]
  72. public ActionResult ValidatePath([FromBody, BindRequired] ValidatePathDto validatePathDto)
  73. {
  74. if (validatePathDto.IsFile.HasValue)
  75. {
  76. if (validatePathDto.IsFile.Value)
  77. {
  78. if (!System.IO.File.Exists(validatePathDto.Path))
  79. {
  80. return NotFound();
  81. }
  82. }
  83. else
  84. {
  85. if (!Directory.Exists(validatePathDto.Path))
  86. {
  87. return NotFound();
  88. }
  89. }
  90. }
  91. else
  92. {
  93. if (!System.IO.File.Exists(validatePathDto.Path) && !Directory.Exists(validatePathDto.Path))
  94. {
  95. return NotFound();
  96. }
  97. if (validatePathDto.ValidateWritable)
  98. {
  99. var file = Path.Combine(validatePathDto.Path, Guid.NewGuid().ToString());
  100. try
  101. {
  102. System.IO.File.WriteAllText(file, string.Empty);
  103. }
  104. finally
  105. {
  106. if (System.IO.File.Exists(file))
  107. {
  108. System.IO.File.Delete(file);
  109. }
  110. }
  111. }
  112. }
  113. return Ok();
  114. }
  115. /// <summary>
  116. /// Gets network paths.
  117. /// </summary>
  118. /// <response code="200">Empty array returned.</response>
  119. /// <returns>List of entries.</returns>
  120. [Obsolete("This endpoint is obsolete.")]
  121. [HttpGet("NetworkShares")]
  122. [ProducesResponseType(StatusCodes.Status200OK)]
  123. public ActionResult<IEnumerable<FileSystemEntryInfo>> GetNetworkShares()
  124. {
  125. _logger.LogWarning("Obsolete endpoint accessed: /Environment/NetworkShares");
  126. return Array.Empty<FileSystemEntryInfo>();
  127. }
  128. /// <summary>
  129. /// Gets available drives from the server's file system.
  130. /// </summary>
  131. /// <response code="200">List of entries returned.</response>
  132. /// <returns>List of entries.</returns>
  133. [HttpGet("Drives")]
  134. [ProducesResponseType(StatusCodes.Status200OK)]
  135. public IEnumerable<FileSystemEntryInfo> GetDrives()
  136. {
  137. return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo(d.Name, d.FullName, FileSystemEntryType.Directory));
  138. }
  139. /// <summary>
  140. /// Gets the parent path of a given path.
  141. /// </summary>
  142. /// <param name="path">The path.</param>
  143. /// <returns>Parent path.</returns>
  144. [HttpGet("ParentPath")]
  145. [ProducesResponseType(StatusCodes.Status200OK)]
  146. public ActionResult<string?> GetParentPath([FromQuery, BindRequired] string path)
  147. {
  148. string? parent = Path.GetDirectoryName(path);
  149. if (string.IsNullOrEmpty(parent))
  150. {
  151. // Check if unc share
  152. var index = path.LastIndexOf(UncSeparator);
  153. if (index != -1 && path.IndexOf(UncSeparator, StringComparison.OrdinalIgnoreCase) == 0)
  154. {
  155. parent = path.Substring(0, index);
  156. if (string.IsNullOrWhiteSpace(parent.TrimStart(UncSeparator)))
  157. {
  158. parent = null;
  159. }
  160. }
  161. }
  162. return parent;
  163. }
  164. /// <summary>
  165. /// Get Default directory browser.
  166. /// </summary>
  167. /// <response code="200">Default directory browser returned.</response>
  168. /// <returns>Default directory browser.</returns>
  169. [HttpGet("DefaultDirectoryBrowser")]
  170. [ProducesResponseType(StatusCodes.Status200OK)]
  171. public ActionResult<DefaultDirectoryBrowserInfoDto> GetDefaultDirectoryBrowser()
  172. {
  173. return new DefaultDirectoryBrowserInfoDto();
  174. }
  175. }
  176. }