From 714aaefbcc3abf0b952efc831001cf42e1c873b0 Mon Sep 17 00:00:00 2001 From: crobibero Date: Fri, 24 Apr 2020 18:20:36 -0600 Subject: Transfer EnvironmentService to Jellyfin.Api --- Jellyfin.Api/Controllers/EnvironmentController.cs | 200 ++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 Jellyfin.Api/Controllers/EnvironmentController.cs (limited to 'Jellyfin.Api/Controllers/EnvironmentController.cs') diff --git a/Jellyfin.Api/Controllers/EnvironmentController.cs b/Jellyfin.Api/Controllers/EnvironmentController.cs new file mode 100644 index 000000000..139c1af08 --- /dev/null +++ b/Jellyfin.Api/Controllers/EnvironmentController.cs @@ -0,0 +1,200 @@ +#nullable enable + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Jellyfin.Api.Constants; +using Jellyfin.Api.Models.EnvironmentDtos; +using MediaBrowser.Model.IO; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace Jellyfin.Api.Controllers +{ + /// + /// Environment Controller. + /// + [Authorize(Policy = Policies.RequiresElevation)] + public class EnvironmentController : BaseJellyfinApiController + { + private const char UncSeparator = '\\'; + private const string UncSeparatorString = "\\"; + + private readonly IFileSystem _fileSystem; + + /// + /// Initializes a new instance of the class. + /// + /// Instance of the interface. + public EnvironmentController(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + /// + /// Gets the contents of a given directory in the file system. + /// + /// The path. + /// An optional filter to include or exclude files from the results. true/false. + /// An optional filter to include or exclude folders from the results. true/false. + /// File system entries. + [HttpGet("DirectoryContents")] + [ProducesResponseType(StatusCodes.Status200OK)] + public IEnumerable GetDirectoryContents( + [FromQuery, BindRequired] string path, + [FromQuery] bool includeFiles, + [FromQuery] bool includeDirectories) + { + const string networkPrefix = UncSeparatorString + UncSeparatorString; + if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) + && path.LastIndexOf(UncSeparator) == 1) + { + return Array.Empty(); + } + + var entries = _fileSystem.GetFileSystemEntries(path).OrderBy(i => i.FullName).Where(i => + { + var isDirectory = i.IsDirectory; + + if (!includeFiles && !isDirectory) + { + return false; + } + + return includeDirectories || !isDirectory; + }); + + return entries.Select(f => new FileSystemEntryInfo + { + Name = f.Name, + Path = f.FullName, + Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File + }); + } + + /// + /// Validates path. + /// + /// Validate request object. + /// Status. + [HttpPost("ValidatePath")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public ActionResult ValidatePath([FromBody, BindRequired] ValidatePathDto validatePathDto) + { + if (validatePathDto.IsFile.HasValue) + { + if (validatePathDto.IsFile.Value) + { + if (!System.IO.File.Exists(validatePathDto.Path)) + { + return NotFound(); + } + } + else + { + if (!Directory.Exists(validatePathDto.Path)) + { + return NotFound(); + } + } + } + else + { + if (!System.IO.File.Exists(validatePathDto.Path) && !Directory.Exists(validatePathDto.Path)) + { + return NotFound(); + } + + if (validatePathDto.ValidateWritable) + { + var file = Path.Combine(validatePathDto.Path, Guid.NewGuid().ToString()); + try + { + System.IO.File.WriteAllText(file, string.Empty); + } + finally + { + if (System.IO.File.Exists(file)) + { + System.IO.File.Delete(file); + } + } + } + } + + return Ok(); + } + + /// + /// Gets network paths. + /// + /// List of entries. + [Obsolete("This endpoint is obsolete.")] + [HttpGet("NetworkShares")] + [ProducesResponseType(StatusCodes.Status200OK)] + public ActionResult> GetNetworkShares() + { + return Array.Empty(); + } + + /// + /// Gets available drives from the server's file system. + /// + /// List of entries. + [HttpGet("Drives")] + [ProducesResponseType(StatusCodes.Status200OK)] + public IEnumerable GetDrives() + { + return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo + { + Name = d.Name, + Path = d.FullName, + Type = FileSystemEntryType.Directory + }); + } + + /// + /// Gets the parent path of a given path. + /// + /// The path. + /// Parent path. + [HttpGet("ParentPath")] + [ProducesResponseType(StatusCodes.Status200OK)] + public ActionResult GetParentPath([FromQuery, BindRequired] string path) + { + string? parent = Path.GetDirectoryName(path); + if (string.IsNullOrEmpty(parent)) + { + // Check if unc share + var index = path.LastIndexOf(UncSeparator); + + if (index != -1 && path.IndexOf(UncSeparator, StringComparison.OrdinalIgnoreCase) == 0) + { + parent = path.Substring(0, index); + + if (string.IsNullOrWhiteSpace(parent.TrimStart(UncSeparator))) + { + parent = null; + } + } + } + + return parent; + } + + /// + /// Get Default directory browser. + /// + /// Default directory browser. + [HttpGet("DefaultDirectoryBrowser")] + [ProducesResponseType(StatusCodes.Status200OK)] + public ActionResult GetDefaultDirectoryBrowser() + { + return new DefaultDirectoryBrowserInfo(); + } + } +} -- cgit v1.2.3 From e03c97d7cdfad65a48bc0aff6ca0e45f9b3ec3cd Mon Sep 17 00:00:00 2001 From: crobibero Date: Tue, 19 May 2020 10:02:52 -0600 Subject: update xml docs --- Jellyfin.Api/Controllers/EnvironmentController.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'Jellyfin.Api/Controllers/EnvironmentController.cs') diff --git a/Jellyfin.Api/Controllers/EnvironmentController.cs b/Jellyfin.Api/Controllers/EnvironmentController.cs index 139c1af08..78c206ba1 100644 --- a/Jellyfin.Api/Controllers/EnvironmentController.cs +++ b/Jellyfin.Api/Controllers/EnvironmentController.cs @@ -40,7 +40,8 @@ namespace Jellyfin.Api.Controllers /// The path. /// An optional filter to include or exclude files from the results. true/false. /// An optional filter to include or exclude folders from the results. true/false. - /// File system entries. + /// Directory contents returned. + /// Directory contents. [HttpGet("DirectoryContents")] [ProducesResponseType(StatusCodes.Status200OK)] public IEnumerable GetDirectoryContents( @@ -79,7 +80,9 @@ namespace Jellyfin.Api.Controllers /// Validates path. /// /// Validate request object. - /// Status. + /// Path validated. + /// Path not found. + /// Validation status. [HttpPost("ValidatePath")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -132,6 +135,7 @@ namespace Jellyfin.Api.Controllers /// /// Gets network paths. /// + /// Empty array returned. /// List of entries. [Obsolete("This endpoint is obsolete.")] [HttpGet("NetworkShares")] @@ -144,6 +148,7 @@ namespace Jellyfin.Api.Controllers /// /// Gets available drives from the server's file system. /// + /// List of entries returned. /// List of entries. [HttpGet("Drives")] [ProducesResponseType(StatusCodes.Status200OK)] @@ -189,6 +194,7 @@ namespace Jellyfin.Api.Controllers /// /// Get Default directory browser. /// + /// Default directory browser returned. /// Default directory browser. [HttpGet("DefaultDirectoryBrowser")] [ProducesResponseType(StatusCodes.Status200OK)] -- cgit v1.2.3 From 5f0c37d5745cbf2632d377905a0763f0254bca08 Mon Sep 17 00:00:00 2001 From: crobibero Date: Tue, 19 May 2020 13:22:09 -0600 Subject: Fix DefaultDirectoryBrowserInfo naming --- Jellyfin.Api/Controllers/EnvironmentController.cs | 4 ++-- .../Models/EnvironmentDtos/DefaultDirectoryBrowserInfo.cs | 13 ------------- .../EnvironmentDtos/DefaultDirectoryBrowserInfoDto.cs | 13 +++++++++++++ 3 files changed, 15 insertions(+), 15 deletions(-) delete mode 100644 Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfo.cs create mode 100644 Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfoDto.cs (limited to 'Jellyfin.Api/Controllers/EnvironmentController.cs') diff --git a/Jellyfin.Api/Controllers/EnvironmentController.cs b/Jellyfin.Api/Controllers/EnvironmentController.cs index 78c206ba1..8d9d2642f 100644 --- a/Jellyfin.Api/Controllers/EnvironmentController.cs +++ b/Jellyfin.Api/Controllers/EnvironmentController.cs @@ -198,9 +198,9 @@ namespace Jellyfin.Api.Controllers /// Default directory browser. [HttpGet("DefaultDirectoryBrowser")] [ProducesResponseType(StatusCodes.Status200OK)] - public ActionResult GetDefaultDirectoryBrowser() + public ActionResult GetDefaultDirectoryBrowser() { - return new DefaultDirectoryBrowserInfo(); + return new DefaultDirectoryBrowserInfoDto(); } } } diff --git a/Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfo.cs b/Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfo.cs deleted file mode 100644 index 6b1c750bf..000000000 --- a/Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfo.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Jellyfin.Api.Models.EnvironmentDtos -{ - /// - /// Default directory browser info. - /// - public class DefaultDirectoryBrowserInfo - { - /// - /// Gets or sets the path. - /// - public string Path { get; set; } - } -} diff --git a/Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfoDto.cs b/Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfoDto.cs new file mode 100644 index 000000000..a86815b81 --- /dev/null +++ b/Jellyfin.Api/Models/EnvironmentDtos/DefaultDirectoryBrowserInfoDto.cs @@ -0,0 +1,13 @@ +namespace Jellyfin.Api.Models.EnvironmentDtos +{ + /// + /// Default directory browser info. + /// + public class DefaultDirectoryBrowserInfoDto + { + /// + /// Gets or sets the path. + /// + public string Path { get; set; } + } +} -- cgit v1.2.3 From 22f56842bd6422a8f2789a2ce5a7d6f4caf563f2 Mon Sep 17 00:00:00 2001 From: crobibero Date: Thu, 4 Jun 2020 07:59:11 -0600 Subject: Apply review suggestions --- Jellyfin.Api/Controllers/EnvironmentController.cs | 31 ++++++++++------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'Jellyfin.Api/Controllers/EnvironmentController.cs') diff --git a/Jellyfin.Api/Controllers/EnvironmentController.cs b/Jellyfin.Api/Controllers/EnvironmentController.cs index 8d9d2642f..35cd89e0e 100644 --- a/Jellyfin.Api/Controllers/EnvironmentController.cs +++ b/Jellyfin.Api/Controllers/EnvironmentController.cs @@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.Extensions.Logging; namespace Jellyfin.Api.Controllers { @@ -21,17 +22,20 @@ namespace Jellyfin.Api.Controllers public class EnvironmentController : BaseJellyfinApiController { private const char UncSeparator = '\\'; - private const string UncSeparatorString = "\\"; + private const string UncStartPrefix = @"\\"; private readonly IFileSystem _fileSystem; + private readonly ILogger _logger; /// /// Initializes a new instance of the class. /// /// Instance of the interface. - public EnvironmentController(IFileSystem fileSystem) + /// Instance of the interface. + public EnvironmentController(IFileSystem fileSystem, ILogger logger) { _fileSystem = fileSystem; + _logger = logger; } /// @@ -46,27 +50,19 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] public IEnumerable GetDirectoryContents( [FromQuery, BindRequired] string path, - [FromQuery] bool includeFiles, - [FromQuery] bool includeDirectories) + [FromQuery] bool includeFiles = false, + [FromQuery] bool includeDirectories = false) { - const string networkPrefix = UncSeparatorString + UncSeparatorString; - if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase) + if (path.StartsWith(UncStartPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf(UncSeparator) == 1) { return Array.Empty(); } - var entries = _fileSystem.GetFileSystemEntries(path).OrderBy(i => i.FullName).Where(i => - { - var isDirectory = i.IsDirectory; - - if (!includeFiles && !isDirectory) - { - return false; - } - - return includeDirectories || !isDirectory; - }); + var entries = + _fileSystem.GetFileSystemEntries(path) + .Where(i => (i.IsDirectory && includeDirectories) || (!i.IsDirectory && includeFiles)) + .OrderBy(i => i.FullName); return entries.Select(f => new FileSystemEntryInfo { @@ -142,6 +138,7 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult> GetNetworkShares() { + _logger.LogWarning("Obsolete endpoint accessed: /Environment/NetworkShares"); return Array.Empty(); } -- cgit v1.2.3 From 2d4998c5782d426f09aa264f37a2bc384bf940f2 Mon Sep 17 00:00:00 2001 From: crobibero Date: Sat, 13 Jun 2020 15:28:04 -0600 Subject: fix build --- Jellyfin.Api/Controllers/EnvironmentController.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'Jellyfin.Api/Controllers/EnvironmentController.cs') diff --git a/Jellyfin.Api/Controllers/EnvironmentController.cs b/Jellyfin.Api/Controllers/EnvironmentController.cs index 35cd89e0e..046ffdf8e 100644 --- a/Jellyfin.Api/Controllers/EnvironmentController.cs +++ b/Jellyfin.Api/Controllers/EnvironmentController.cs @@ -64,12 +64,7 @@ namespace Jellyfin.Api.Controllers .Where(i => (i.IsDirectory && includeDirectories) || (!i.IsDirectory && includeFiles)) .OrderBy(i => i.FullName); - return entries.Select(f => new FileSystemEntryInfo - { - Name = f.Name, - Path = f.FullName, - Type = f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File - }); + return entries.Select(f => new FileSystemEntryInfo(f.Name, f.FullName, f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File)); } /// @@ -151,12 +146,7 @@ namespace Jellyfin.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] public IEnumerable GetDrives() { - return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo - { - Name = d.Name, - Path = d.FullName, - Type = FileSystemEntryType.Directory - }); + return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo(d.Name, d.FullName, FileSystemEntryType.Directory)); } /// -- cgit v1.2.3 From 6767c47ccdede27a374ea21b3013fe32ee356f01 Mon Sep 17 00:00:00 2001 From: crobibero Date: Fri, 19 Jun 2020 09:01:37 -0600 Subject: remove #nullable --- Jellyfin.Api/Controllers/EnvironmentController.cs | 2 -- 1 file changed, 2 deletions(-) (limited to 'Jellyfin.Api/Controllers/EnvironmentController.cs') diff --git a/Jellyfin.Api/Controllers/EnvironmentController.cs b/Jellyfin.Api/Controllers/EnvironmentController.cs index 046ffdf8e..719bb7d86 100644 --- a/Jellyfin.Api/Controllers/EnvironmentController.cs +++ b/Jellyfin.Api/Controllers/EnvironmentController.cs @@ -1,5 +1,3 @@ -#nullable enable - using System; using System.Collections.Generic; using System.IO; -- cgit v1.2.3