using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using ClassNotify.Server.Services; namespace ClassNotify.Server.Controllers; [ApiController] public class DownloadController : ControllerBase { private readonly IConfiguration _config; private readonly IInstallerTicketService _ticketService; private readonly ILogger _logger; public DownloadController(IConfiguration config, IInstallerTicketService ticketService, ILogger logger) { _config = config; _ticketService = ticketService; _logger = logger; } /// /// Gated Download URL for ClassNotify Windows Client Installer. /// Requires short-lived ticket issued to authenticated teacher. /// Anonymous or invalid ticket requests are redirected to registration/login guidance. /// [HttpGet("/download/windows")] [HttpHead("/download/windows")] public IActionResult DownloadWindowsInstaller([FromQuery] string? ticket) { if (string.IsNullOrWhiteSpace(ticket) || !_ticketService.ValidateTicket(ticket, out var teacherId)) { _logger.LogWarning("Anonymous or invalid ticket installer download attempt from {IP}", HttpContext.Connection.RemoteIpAddress); return Redirect("/login?mode=register&returnTo=%2Fteacher%3Ftab%3Dscreens"); } var candidatePaths = new[] { "/var/classnotify/installers/ClassNotifySetup-2.2.0.exe", "/var/classnotify/installers/ClassNotifySetup.exe", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "ClassNotify", "installers", "ClassNotifySetup.exe") }; foreach (var candidate in candidatePaths) { if (System.IO.File.Exists(candidate)) { _logger.LogInformation("Serving verified Windows installer (v2.2.0) to Teacher {TeacherId} at {IP} from {Path}", teacherId, HttpContext.Connection.RemoteIpAddress, candidate); return PhysicalFile(candidate, "application/vnd.microsoft.portable-executable", "ClassNotifySetup.exe", enableRangeProcessing: true); } } _logger.LogError("Installer binary not found on server for Teacher {TeacherId}", teacherId); return StatusCode(500, new { code = "INSTALLER_NOT_FOUND", message = "大屏安装包暂未就绪,请联系管理员" }); } /// /// Public Release Manifest for verification and client updaters. /// Zero public origin leaks. /// [HttpGet("/download/manifest")] [HttpGet("/api/download/manifest")] public IActionResult GetDownloadManifest() { var manifest = new { version = "2.2.0", fileName = "ClassNotifySetup-2.2.0.exe", sizeBytes = 77067064, sha256 = "4EF720C24C59BD8606D06FEF47DA91F5EF481C16C706231B9870952B71E53872", downloadUrl = "/download/windows", updatedAt = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ") }; return Ok(manifest); } }