//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
using Microsoft.Extensions.Options;
namespace slskd.Files.API
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Security;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OneOf;
using Serilog;
///
/// Files.
///
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("0")]
[ApiController]
[Produces("application/json")]
[Consumes("application/json")]
public class FilesController : ControllerBase
{
public FilesController(
IFileService fileService,
IOptionsSnapshot optionsSnapshot)
{
Files = fileService;
OptionsSnapshot = optionsSnapshot;
}
private IFileService Files { get; }
private IOptionsSnapshot OptionsSnapshot { get; }
private ILogger Log { get; set; } = Serilog.Log.ForContext();
///
/// Lists the contents of the downloads directory.
///
/// An optional value indicating whether to recursively list subdirectories and files.
///
/// The request completed successfully.
/// Authentication failed.
[HttpGet("downloads/directories")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(FilesystemDirectory), 200)]
[ProducesResponseType(401)]
public Task GetDownloadContentsAsync([FromQuery] bool recursive = false)
=> ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName: null, recursive);
///
/// Lists the contents of the specified subdirectory within the downloads directory.
///
/// The relative, base 64 encoded, name of the subdirectory to list.
/// An optional value indicating whether to recursively list subdirectories and files.
///
/// The request completed successfully.
/// Authentication failed.
/// Access to the specified subdirectory was denied.
/// The specified subdirectory does not exist.
[HttpGet("downloads/directories/{base64SubdirectoryName}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(FilesystemDirectory), 200)]
[ProducesResponseType(401)]
[ProducesResponseType(403)]
[ProducesResponseType(404)]
public Task GetDownloadSubdirectoryContentsAsync([FromRoute] string base64SubdirectoryName, [FromQuery] bool recursive = false)
=> ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName, recursive);
///
/// Deletes the specified subdirectory within the downloads directory.
///
/// The relative, base 64 encoded, name of the subdirectory to delete.
///
/// The request completed successfully.
/// Authentication failed.
/// Access to the specified subdirectory was denied.
/// The specified subdirectory does not exist.
[HttpDelete("downloads/directories/{base64SubdirectoryName}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(Dictionary>), 204)]
public Task DeleteDownloadSubdirectoryAsync([FromRoute] string base64SubdirectoryName)
=> DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName);
///
/// Deletes the specified file within the downloads directory.
///
/// The relative, base 64 encoded, name of the file to delete.
///
/// The request completed successfully.
/// Authentication failed.
/// Access to the specified subdirectory was denied.
/// The specified subdirectory does not exist.
[HttpDelete("downloads/files/{base64Filename}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(Dictionary>), 204)]
public Task DeleteDownloadFileAsync([FromRoute] string base64filename)
=> DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64filename);
///
/// Lists the contents of the downloads directory.
///
/// An optional value indicating whether to recursively list subdirectories and files.
///
/// The request completed successfully.
/// Authentication failed.
[HttpGet("incomplete/directories")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(FilesystemDirectory), 200)]
public Task GetIncompleteContentsAsync([FromQuery] bool recursive = false)
=> ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName: null, recursive);
///
/// Lists the contents of the specified subdirectory within the incomplete directory.
///
/// The relative, base 64 encoded, name of the subdirectory to list.
/// An optional value indicating whether to recursively list subdirectories and files.
///
/// The request completed successfully.
/// Authentication failed.
/// Access to the specified subdirectory was denied.
/// The specified subdirectory does not exist.
[HttpGet("incomplete/directories/{base64SubdirectoryName}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(FilesystemDirectory), 200)]
public Task GetIncompleteSubdirectoryContentsAsync([FromRoute, Required] string base64SubdirectoryName, [FromQuery] bool recursive = false)
=> ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName, recursive);
///
/// Deletes the specified subdirectory within the downloads directory.
///
/// The relative, base 64 encoded, name of the subdirectory to delete.
///
/// The request completed successfully.
/// Authentication failed.
/// Access to the specified subdirectory was denied.
/// The specified subdirectory does not exist.
[HttpDelete("incomplete/directories/{base64SubdirectoryName}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(typeof(Dictionary>), 204)]
public Task DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName)
=> DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName);
///
/// Deletes the specified file within the downloads directory.
///
/// The relative, base 64 encoded, name of the file to delete.
///
/// The request completed successfully.
/// Authentication failed.
/// Access to the specified subdirectory was denied.
/// The specified subdirectory does not exist.
[HttpDelete("incomplete/files/{base64Filename}")]
[Authorize(Policy = AuthPolicy.Any)]
[ProducesResponseType(204)]
public Task DeleteIncompleteFileAsync([FromRoute] string base64filename)
=> DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64filename);
private async Task ListDirectoryAsync(string rootDirectory, string base64SubdirectoryName = null, bool recursive = false)
{
var requestedDir = (base64SubdirectoryName ?? string.Empty)
.FromBase64()
.Replace('\\', Path.DirectorySeparatorChar)
.Replace('/', Path.DirectorySeparatorChar);
requestedDir = Path.GetFullPath(Path.Combine(rootDirectory, requestedDir));
Log.Debug("Listing directory '{Directory}'", requestedDir);
try
{
var response = await Files.ListContentsAsync(
directory: requestedDir,
enumerationOptions: new EnumerationOptions
{
AttributesToSkip = FileAttributes.System,
RecurseSubdirectories = recursive,
});
return Ok(response);
}
catch (SecurityException)
{
Log.Warning("Directory listing of '{Directory}' forbidden", requestedDir);
return Forbid();
}
catch (NotFoundException)
{
Log.Debug("Directory '{Directory}' not found", requestedDir);
return NotFound();
}
}
private async Task DeleteSubdirectoryAsync(string rootDirectory, string base64SubdirectoryName)
{
if (!OptionsSnapshot.Value.RemoteFileManagement)
{
return Forbid();
}
var requestedDir = base64SubdirectoryName
.FromBase64()
.Replace('\\', Path.DirectorySeparatorChar)
.Replace('/', Path.DirectorySeparatorChar);
requestedDir = Path.GetFullPath(Path.Combine(rootDirectory, requestedDir));
Log.Information("Deleting directory '{Directory}'", requestedDir);
try
{
var results = await Files.DeleteDirectoriesAsync(requestedDir);
return results[requestedDir].Match(
success => NoContent(),
failure => throw failure);
}
catch (SecurityException)
{
Log.Warning("Directory deletion of '{Directory}' forbidden", requestedDir);
return Forbid();
}
catch (NotFoundException)
{
Log.Information("Directory '{Directory}' not found", requestedDir);
return NotFound();
}
}
private async Task DeleteFileAsync(string rootDirectory, string base64Filename)
{
if (!OptionsSnapshot.Value.RemoteFileManagement)
{
return Forbid();
}
var requestedFilename = base64Filename
.FromBase64()
.Replace('\\', Path.DirectorySeparatorChar)
.Replace('/', Path.DirectorySeparatorChar);
requestedFilename = Path.GetFullPath(Path.Combine(rootDirectory, requestedFilename));
Log.Information("Deleting file '{File}'", requestedFilename);
try
{
var results = await Files.DeleteFilesAsync(requestedFilename);
return results[requestedFilename].Match(
success => NoContent(),
failure => throw failure);
}
catch (SecurityException)
{
Log.Warning("File deletion of '{File}' forbidden", requestedFilename);
return Forbid();
}
catch (NotFoundException)
{
Log.Information("File '{File}' not found", requestedFilename);
return NotFound();
}
}
}
}