VirtualTask/Controllers/FotoController.cs
2025-09-24 09:48:48 +02:00

76 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using static VirtualTask.Controllers.LoginController;
namespace VirtualTask.Controllers
{
[Route("foto")]
public class FotoController : Controller
{
string _rootPath = string.Empty;
private readonly IConfiguration _configuration;
public FotoController(IConfiguration configuration)
{
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootPath"];
_rootPath = key;
}
[HttpGet("{folder}/{fileName}")]
public IActionResult ShowOrServe(string folder, string fileName)
{
if (string.IsNullOrEmpty(folder) || string.IsNullOrEmpty(fileName))
return Content("Parametri non validi");
var safeFolder = Path.GetFileName(folder); // sicurezza
var safeFileName = Path.GetFileName(fileName);
// Percorso: /mnt/storagebox/{folder}/PHOTO/{fileName}
var filePath = Path.Combine(_rootPath, safeFolder, "PHOTO", safeFileName);
//SOLO PER TEST!!!!!!
//filePath = "C:\\DATI\\V000000009_2.jpg";
if (!System.IO.File.Exists(filePath))
return NotFound("File non trovato");
ViewBag.Folder = safeFolder;
ViewBag.FileName = safeFileName;
return View("ShowImage");
}
// Endpoint che restituisce il file binario, usato dentro la view
[HttpGet("raw/{folder}/{fileName}")]
public IActionResult RawImage(string folder, string fileName)
{
var safeFolder = Path.GetFileName(folder);
var safeFileName = Path.GetFileName(fileName);
var filePath = Path.Combine(_rootPath, safeFolder, "PHOTO", safeFileName);
//SOLO PER TEST!!!!!!
//filePath = "C:\\DATI\\V000000009_2.jpg";
if (!System.IO.File.Exists(filePath))
return NotFound("File non trovato");
var ext = Path.GetExtension(safeFileName).ToLowerInvariant();
var contentType = ext switch
{
".png" => "image/png",
".jpg" or ".jpeg" => "image/jpeg",
".webp" => "image/webp",
_ => "application/octet-stream"
};
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return File(stream, contentType);
}
}
}