Apply review suggestions

This commit is contained in:
crobibero 2020-06-04 07:59:11 -06:00
parent 5f0c37d574
commit 22f56842bd

View File

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Api.Controllers namespace Jellyfin.Api.Controllers
{ {
@ -21,17 +22,20 @@ namespace Jellyfin.Api.Controllers
public class EnvironmentController : BaseJellyfinApiController public class EnvironmentController : BaseJellyfinApiController
{ {
private const char UncSeparator = '\\'; private const char UncSeparator = '\\';
private const string UncSeparatorString = "\\"; private const string UncStartPrefix = @"\\";
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private readonly ILogger<EnvironmentController> _logger;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="EnvironmentController"/> class. /// Initializes a new instance of the <see cref="EnvironmentController"/> class.
/// </summary> /// </summary>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
public EnvironmentController(IFileSystem fileSystem) /// <param name="logger">Instance of the <see cref="ILogger{EnvironmentController}"/> interface.</param>
public EnvironmentController(IFileSystem fileSystem, ILogger<EnvironmentController> logger)
{ {
_fileSystem = fileSystem; _fileSystem = fileSystem;
_logger = logger;
} }
/// <summary> /// <summary>
@ -46,27 +50,19 @@ namespace Jellyfin.Api.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
public IEnumerable<FileSystemEntryInfo> GetDirectoryContents( public IEnumerable<FileSystemEntryInfo> GetDirectoryContents(
[FromQuery, BindRequired] string path, [FromQuery, BindRequired] string path,
[FromQuery] bool includeFiles, [FromQuery] bool includeFiles = false,
[FromQuery] bool includeDirectories) [FromQuery] bool includeDirectories = false)
{ {
const string networkPrefix = UncSeparatorString + UncSeparatorString; if (path.StartsWith(UncStartPrefix, StringComparison.OrdinalIgnoreCase)
if (path.StartsWith(networkPrefix, StringComparison.OrdinalIgnoreCase)
&& path.LastIndexOf(UncSeparator) == 1) && path.LastIndexOf(UncSeparator) == 1)
{ {
return Array.Empty<FileSystemEntryInfo>(); return Array.Empty<FileSystemEntryInfo>();
} }
var entries = _fileSystem.GetFileSystemEntries(path).OrderBy(i => i.FullName).Where(i => var entries =
{ _fileSystem.GetFileSystemEntries(path)
var isDirectory = i.IsDirectory; .Where(i => (i.IsDirectory && includeDirectories) || (!i.IsDirectory && includeFiles))
.OrderBy(i => i.FullName);
if (!includeFiles && !isDirectory)
{
return false;
}
return includeDirectories || !isDirectory;
});
return entries.Select(f => new FileSystemEntryInfo return entries.Select(f => new FileSystemEntryInfo
{ {
@ -142,6 +138,7 @@ namespace Jellyfin.Api.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<IEnumerable<FileSystemEntryInfo>> GetNetworkShares() public ActionResult<IEnumerable<FileSystemEntryInfo>> GetNetworkShares()
{ {
_logger.LogWarning("Obsolete endpoint accessed: /Environment/NetworkShares");
return Array.Empty<FileSystemEntryInfo>(); return Array.Empty<FileSystemEntryInfo>();
} }