Files
watcher/Watcher/Controllers/DownloadController.cs
2025-06-25 19:51:55 +02:00

53 lines
1.7 KiB
C#

using System.Runtime.InteropServices;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Watcher.Controllers;
[Authorize]
public class DownloadController : Controller
{
// Logging einbinden
private readonly ILogger<AuthController> _logger;
public DownloadController(ILogger<AuthController> logger)
{
_logger = logger;
}
// Allgemeiner Download von Dateien
[Authorize]
[HttpGet("Download/File/{type}/{filename}")]
public IActionResult FileDownload(string type, string filename)
{
// Nur erlaubte Endungen zulassen (Sicherheit!)
// TODO: aktuelles "" für Binaries ist das absolute Gegenteil von sicher, ich hab aber keine bessere Idee
var allowedExtensions = new[] { ".exe", "", ".sql" };
var extension = Path.GetExtension(filename).ToLowerInvariant();
if (!allowedExtensions.Contains(extension))
return BadRequest("Dateityp nicht erlaubt");
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "downloads", type, filename);
if (!System.IO.File.Exists(path))
{
_logger.LogError("Download fehlgeschlagen: Datei nicht gefunden ({path})", path);
return NotFound("Datei nicht gefunden");
}
// .exe MIME-Typ: application/octet-stream
var mimeType = "application/octet-stream";
var fileBytes = System.IO.File.ReadAllBytes(path);
_logger.LogTrace("Download ausgeführt: {filename}", filename);
return File(fileBytes, mimeType, filename);
//return File(fileBytes, filename);
}
}