[ADD] Added endpoint for providing files

This commit is contained in:
max
2025-09-22 16:15:18 +02:00
parent a478943792
commit 646e0a814a
3 changed files with 48 additions and 4 deletions

View File

@@ -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<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)
{
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");
}
}