diff --git a/Manager.App/Controllers/FileController.cs b/Manager.App/Controllers/FileController.cs new file mode 100644 index 0000000..3a81545 --- /dev/null +++ b/Manager.App/Controllers/FileController.cs @@ -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 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); + } +} \ No newline at end of file diff --git a/Manager.App/Services/ILibraryService.cs b/Manager.App/Services/ILibraryService.cs index e3709b5..f1ad57e 100644 --- a/Manager.App/Services/ILibraryService.cs +++ b/Manager.App/Services/ILibraryService.cs @@ -10,6 +10,7 @@ public interface ILibraryService { public Task FetchChannelImagesAsync(InnertubeChannel innertubeChannel); public Task SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default); + public Task> GetFileByIdAsync(Guid id, CancellationToken cancellationToken = default); public Task> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default); public Task SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default); public Task> GetLibraryInfoAsync(CancellationToken cancellationToken = default); diff --git a/Manager.App/Services/LibraryService.cs b/Manager.App/Services/LibraryService.cs index e5891db..a42b006 100644 --- a/Manager.App/Services/LibraryService.cs +++ b/Manager.App/Services/LibraryService.cs @@ -1,3 +1,4 @@ +using System.Net.Mime; using DotBased.Monads; using Manager.App.Constants; using Manager.App.Models.Library; @@ -51,7 +52,7 @@ public class LibraryService : ILibraryService } catch (Exception e) { - return ResultError.Error(e); + return HandleException(e); } return Result.Success(); @@ -134,7 +135,27 @@ public class LibraryService : ILibraryService } catch (Exception e) { - return ResultError.Error(e); + return HandleException(e); + } + } + + public async Task> 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) { - return ResultError.Error(e); + return HandleException(e); } } @@ -344,7 +365,7 @@ public class LibraryService : ILibraryService 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"); } } \ No newline at end of file