LANCommander/LANCommander.Server/Controllers/Api/MediaController.cs

84 lines
2.3 KiB
C#
Raw Permalink Normal View History

using AutoMapper;
2024-08-04 18:44:33 -05:00
using LANCommander.Server.Data;
using LANCommander.Server.Data.Models;
using LANCommander.Server.Extensions;
using LANCommander.Server.Models;
using LANCommander.SDK;
2024-08-04 18:44:33 -05:00
using LANCommander.Server.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2024-08-04 18:44:33 -05:00
namespace LANCommander.Server.Controllers.Api
{
[Authorize(AuthenticationSchemes = "Bearer")]
[Route("api/[controller]")]
[ApiController]
public class MediaController : BaseApiController
{
private readonly IMapper Mapper;
2024-11-07 18:54:46 -06:00
private readonly MediaService MediaService;
public MediaController(
ILogger<MediaController> logger,
IMapper mapper,
2024-11-07 18:54:46 -06:00
MediaService mediaService) : base(logger)
{
Mapper = mapper;
MediaService = mediaService;
}
[HttpGet]
2024-11-12 23:22:01 -06:00
public async Task<ActionResult<IEnumerable<SDK.Models.Media>>> GetAsync()
{
return Ok(Mapper.Map<IEnumerable<SDK.Models.Media>>(await MediaService.GetAsync()));
}
[HttpGet("{id}")]
2024-11-12 23:22:01 -06:00
public async Task<ActionResult<SDK.Models.Media>> GetAsync(Guid id)
{
var media = await MediaService.GetAsync(id);
if (media == null)
return NotFound();
else
return Mapper.Map<SDK.Models.Media>(media);
}
2024-11-07 18:54:46 -06:00
[AllowAnonymous]
[HttpGet("{id}/Thumbnail")]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> ThumbnailAsync(Guid id)
2024-11-07 18:54:46 -06:00
{
try
{
var media = await MediaService.GetAsync(id);
2024-11-07 18:54:46 -06:00
var fs = System.IO.File.OpenRead(MediaService.GetThumbnailPath(media));
return File(fs, media.MimeType);
}
catch (Exception ex)
{
return NotFound();
}
}
[AllowAnonymous]
[HttpGet("{id}/Download")]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> DownloadAsync(Guid id)
{
try
{
var media = await MediaService.GetAsync(id);
2024-11-05 19:38:59 -06:00
var fs = System.IO.File.OpenRead(Services.MediaService.GetMediaPath(media));
return File(fs, media.MimeType);
}
catch (Exception ex)
{
return NotFound();
}
}
}
}