Extracted some pages to their own assembly and finished the artifact display page code
This commit is contained in:
53
OpenArchival.Blazor/Controllers/FilesController.cs
Normal file
53
OpenArchival.Blazor/Controllers/FilesController.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OpenArchival.DataAccess; // Assuming your DbContext or service is here
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class FilesController : ControllerBase
|
||||
{
|
||||
// Inject your database context or a service to find file paths
|
||||
private readonly IDbContextFactory<ApplicationDbContext> _context;
|
||||
|
||||
public FilesController(IDbContextFactory<ApplicationDbContext> dbFactory)
|
||||
{
|
||||
_context = dbFactory;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetFile(int id)
|
||||
{
|
||||
await using var context = await _context.CreateDbContextAsync();
|
||||
|
||||
FilePathListing fileRecord = await context.ArtifactFilePaths.FindAsync(id);
|
||||
|
||||
if (fileRecord == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var physicalPath = fileRecord.Path;
|
||||
|
||||
if (!System.IO.File.Exists(physicalPath))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var fileStream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read);
|
||||
|
||||
return new FileStreamResult(fileStream, GetMimeType(physicalPath));
|
||||
}
|
||||
|
||||
private static string GetMimeType(string filePath)
|
||||
{
|
||||
var extension = Path.GetExtension(filePath).ToLowerInvariant();
|
||||
return extension switch
|
||||
{
|
||||
".jpg" => "image/jpeg",
|
||||
".jpeg" => "image/jpeg",
|
||||
".png" => "image/png",
|
||||
".gif" => "image/gif",
|
||||
_ => "application/octet-stream", // Default for unknown file types
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user