using LANCommander.Server.Data; using LANCommander.Server.Data.Models; using LANCommander.Server.Extensions; using LANCommander.Server.Models; using LANCommander.Server.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace LANCommander.Server.Controllers.Api { [Authorize(AuthenticationSchemes = "Bearer")] [Route("api/[controller]")] [ApiController] public class ArchivesController : ControllerBase { private readonly DatabaseContext Context; private readonly LANCommanderSettings Settings = SettingService.GetSettings(); public ArchivesController(DatabaseContext context) { Context = context; } [HttpGet] public async Task> Get() { using (var repo = new Repository(Context, HttpContext)) { return await repo.Get(a => true).ToListAsync(); } } [HttpGet("{id}")] public async Task Get(Guid id) { using (var repo = new Repository(Context, HttpContext)) { return await repo.Find(id); } } [HttpGet("Download/{id}")] public async Task Download(Guid id) { using (var repo = new Repository(Context, HttpContext)) { var archive = await repo.Find(id); if (archive == null) return NotFound(); var filename = Path.Combine(Settings.Archives.StoragePath, archive.ObjectKey); if (!System.IO.File.Exists(filename)) return NotFound(); return File(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", $"{archive.Game.Title.SanitizeFilename()}.zip"); } } } }