[ADD] Added endpoint for providing files
This commit is contained in:
22
Manager.App/Controllers/FileController.cs
Normal file
22
Manager.App/Controllers/FileController.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using Manager.App.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Manager.App.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/[controller]")]
|
||||||
|
public class FileController(ILibraryService libraryService) : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet("provide")]
|
||||||
|
public async Task<IActionResult> ProvideFile([FromQuery(Name = "id")] Guid id, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var fileResult = await libraryService.GetFileByIdAsync(id, cancellationToken);
|
||||||
|
if (!fileResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return BadRequest(fileResult.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var libFile = fileResult.Value;
|
||||||
|
return File(libFile.DataStream, libFile.MimeType, libFile.FileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ public interface ILibraryService
|
|||||||
{
|
{
|
||||||
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
||||||
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
||||||
|
public Task<Result<LibraryFile>> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||||
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||||
public Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default);
|
public Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default);
|
||||||
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
|
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Net.Mime;
|
||||||
using DotBased.Monads;
|
using DotBased.Monads;
|
||||||
using Manager.App.Constants;
|
using Manager.App.Constants;
|
||||||
using Manager.App.Models.Library;
|
using Manager.App.Models.Library;
|
||||||
@@ -51,7 +52,7 @@ public class LibraryService : ILibraryService
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return ResultError.Error(e);
|
return HandleException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result.Success();
|
return Result.Success();
|
||||||
@@ -134,7 +135,27 @@ public class LibraryService : ILibraryService
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return ResultError.Error(e);
|
return HandleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<LibraryFile>> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var file = context.Files.FirstOrDefault(f => f.Id == id);
|
||||||
|
if (file == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail($"File with id {id} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var fs = new FileStream(Path.Combine(_libraryDirectory.FullName, file.RelativePath), FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
|
return new LibraryFile { DataStream = fs, SizeBytes = file.SizeBytes, FileName = file.OriginalFileName ?? file.Id.ToString(), MimeType = file.MimeType ?? MediaTypeNames.Application.Octet };
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +306,7 @@ public class LibraryService : ILibraryService
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return ResultError.Error(e);
|
return HandleException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,7 +365,7 @@ public class LibraryService : ILibraryService
|
|||||||
return ResultError.Fail("Library service operation cancelled");
|
return ResultError.Fail("Library service operation cancelled");
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogError(exception, "Failed to get library information");
|
_logger.LogError(exception, "Service error");
|
||||||
return ResultError.Fail("Failed to get library information");
|
return ResultError.Fail("Failed to get library information");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user