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 _context; public FilesController(IDbContextFactory dbFactory) { _context = dbFactory; } [HttpGet("{id}")] public async Task 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 }; } }