2020-04-24 17:20:36 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-08-06 07:17:45 -07:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-04-24 17:20:36 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Jellyfin.Api.Constants;
|
|
|
|
using Jellyfin.Api.Models.EnvironmentDtos;
|
2023-11-10 09:17:22 -07:00
|
|
|
using MediaBrowser.Common.Api;
|
2020-11-13 11:14:44 -07:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2020-04-24 17:20:36 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-06-04 06:59:11 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
namespace Jellyfin.Api.Controllers;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Environment Controller.
|
|
|
|
/// </summary>
|
|
|
|
[Authorize(Policy = Policies.FirstTimeSetupOrElevated)]
|
|
|
|
public class EnvironmentController : BaseJellyfinApiController
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
private const char UncSeparator = '\\';
|
|
|
|
private const string UncStartPrefix = @"\\";
|
|
|
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly ILogger<EnvironmentController> _logger;
|
|
|
|
|
2020-04-24 17:20:36 -07:00
|
|
|
/// <summary>
|
2023-01-31 04:18:10 -07:00
|
|
|
/// Initializes a new instance of the <see cref="EnvironmentController"/> class.
|
2020-04-24 17:20:36 -07:00
|
|
|
/// </summary>
|
2023-01-31 04:18:10 -07:00
|
|
|
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
|
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{EnvironmentController}"/> interface.</param>
|
|
|
|
public EnvironmentController(IFileSystem fileSystem, ILogger<EnvironmentController> logger)
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the contents of a given directory in the file system.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <param name="includeFiles">An optional filter to include or exclude files from the results. true/false.</param>
|
|
|
|
/// <param name="includeDirectories">An optional filter to include or exclude folders from the results. true/false.</param>
|
|
|
|
/// <response code="200">Directory contents returned.</response>
|
|
|
|
/// <returns>Directory contents.</returns>
|
|
|
|
[HttpGet("DirectoryContents")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public IEnumerable<FileSystemEntryInfo> GetDirectoryContents(
|
|
|
|
[FromQuery, Required] string path,
|
|
|
|
[FromQuery] bool includeFiles = false,
|
|
|
|
[FromQuery] bool includeDirectories = false)
|
|
|
|
{
|
|
|
|
if (path.StartsWith(UncStartPrefix, StringComparison.OrdinalIgnoreCase)
|
|
|
|
&& path.LastIndexOf(UncSeparator) == 1)
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
return Array.Empty<FileSystemEntryInfo>();
|
|
|
|
}
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
var entries =
|
|
|
|
_fileSystem.GetFileSystemEntries(path)
|
|
|
|
.Where(i => (i.IsDirectory && includeDirectories) || (!i.IsDirectory && includeFiles))
|
|
|
|
.OrderBy(i => i.FullName);
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
return entries.Select(f => new FileSystemEntryInfo(f.Name, f.FullName, f.IsDirectory ? FileSystemEntryType.Directory : FileSystemEntryType.File));
|
|
|
|
}
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Validates path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="validatePathDto">Validate request object.</param>
|
|
|
|
/// <response code="204">Path validated.</response>
|
|
|
|
/// <response code="404">Path not found.</response>
|
|
|
|
/// <returns>Validation status.</returns>
|
|
|
|
[HttpPost("ValidatePath")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
public ActionResult ValidatePath([FromBody, Required] ValidatePathDto validatePathDto)
|
|
|
|
{
|
|
|
|
if (validatePathDto.IsFile.HasValue)
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
if (validatePathDto.IsFile.Value)
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
if (!System.IO.File.Exists(validatePathDto.Path))
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
return NotFound();
|
2020-04-24 17:20:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
if (!Directory.Exists(validatePathDto.Path))
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
2023-01-31 04:18:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!System.IO.File.Exists(validatePathDto.Path) && !Directory.Exists(validatePathDto.Path))
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
if (validatePathDto.ValidateWritable)
|
|
|
|
{
|
|
|
|
if (validatePathDto.Path is null)
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
throw new ResourceNotFoundException(nameof(validatePathDto.Path));
|
|
|
|
}
|
2020-11-13 09:04:31 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
var file = Path.Combine(validatePathDto.Path, Guid.NewGuid().ToString());
|
|
|
|
try
|
|
|
|
{
|
|
|
|
System.IO.File.WriteAllText(file, string.Empty);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (System.IO.File.Exists(file))
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
System.IO.File.Delete(file);
|
2020-04-24 17:20:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
return NoContent();
|
|
|
|
}
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets network paths.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="200">Empty array returned.</response>
|
|
|
|
/// <returns>List of entries.</returns>
|
|
|
|
[Obsolete("This endpoint is obsolete.")]
|
|
|
|
[HttpGet("NetworkShares")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public ActionResult<IEnumerable<FileSystemEntryInfo>> GetNetworkShares()
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Obsolete endpoint accessed: /Environment/NetworkShares");
|
|
|
|
return Array.Empty<FileSystemEntryInfo>();
|
|
|
|
}
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets available drives from the server's file system.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="200">List of entries returned.</response>
|
|
|
|
/// <returns>List of entries.</returns>
|
|
|
|
[HttpGet("Drives")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public IEnumerable<FileSystemEntryInfo> GetDrives()
|
|
|
|
{
|
|
|
|
return _fileSystem.GetDrives().Select(d => new FileSystemEntryInfo(d.Name, d.FullName, FileSystemEntryType.Directory));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the parent path of a given path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <returns>Parent path.</returns>
|
|
|
|
[HttpGet("ParentPath")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public ActionResult<string?> GetParentPath([FromQuery, Required] string path)
|
|
|
|
{
|
|
|
|
string? parent = Path.GetDirectoryName(path);
|
|
|
|
if (string.IsNullOrEmpty(parent))
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
// Check if unc share
|
|
|
|
var index = path.LastIndexOf(UncSeparator);
|
|
|
|
|
2023-11-14 12:21:34 -07:00
|
|
|
if (index != -1 && path[0] == UncSeparator)
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
parent = path.Substring(0, index);
|
2020-04-24 17:20:36 -07:00
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(parent.TrimStart(UncSeparator)))
|
2020-04-24 17:20:36 -07:00
|
|
|
{
|
2023-01-31 04:18:10 -07:00
|
|
|
parent = null;
|
2020-04-24 17:20:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 04:18:10 -07:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get Default directory browser.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="200">Default directory browser returned.</response>
|
|
|
|
/// <returns>Default directory browser.</returns>
|
|
|
|
[HttpGet("DefaultDirectoryBrowser")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public ActionResult<DefaultDirectoryBrowserInfoDto> GetDefaultDirectoryBrowser()
|
|
|
|
{
|
|
|
|
return new DefaultDirectoryBrowserInfoDto();
|
2020-04-24 17:20:36 -07:00
|
|
|
}
|
|
|
|
}
|