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