using AutoMapper; using LANCommander.Server.Data; using LANCommander.Server.Data.Models; using LANCommander.Server.Extensions; using LANCommander.Server.Models; using LANCommander.SDK; using LANCommander.Server.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace LANCommander.Server.Controllers.Api { [Authorize(AuthenticationSchemes = "Bearer")] [Route("api/[controller]")] [ApiController] public class MediaController : BaseApiController { private readonly IMapper Mapper; private readonly MediaService MediaService; public MediaController( ILogger logger, IMapper mapper, MediaService mediaService) : base(logger) { Mapper = mapper; MediaService = mediaService; } [HttpGet] public async Task>> GetAsync() { return Ok(Mapper.Map>(await MediaService.GetAsync())); } [HttpGet("{id}")] public async Task> GetAsync(Guid id) { var media = await MediaService.GetAsync(id); if (media == null) return NotFound(); else return Mapper.Map(media); } [AllowAnonymous] [HttpGet("{id}/Thumbnail")] public async Task ThumbnailAsync(Guid id) { try { var media = await MediaService.GetAsync(id); var fs = System.IO.File.OpenRead(MediaService.GetThumbnailPath(media)); return File(fs, media.MimeType); } catch (Exception ex) { return NotFound(); } } [AllowAnonymous] [HttpGet("{id}/Download")] public async Task DownloadAsync(Guid id) { try { var media = await MediaService.GetAsync(id); var fs = System.IO.File.OpenRead(Services.MediaService.GetMediaPath(media)); return File(fs, media.MimeType); } catch (Exception ex) { return NotFound(); } } } }