86 lines
2.4 KiB
C#
86 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Icarus.Controllers.Managers;
|
|
using Icarus.Database.Contexts;
|
|
using Icarus.Models;
|
|
|
|
namespace Icarus.Controllers.V1
|
|
{
|
|
[Route("api/v1/coverart")]
|
|
[ApiController]
|
|
public class CoverArtController : ControllerBase
|
|
{
|
|
#region Fields
|
|
private readonly ILogger<CoverArtController> _logger;
|
|
private string _connectionString;
|
|
private IConfiguration _config;
|
|
#endregion
|
|
|
|
|
|
#region Constructors
|
|
public CoverArtController(ILogger<CoverArtController> logger, IConfiguration config)
|
|
{
|
|
_logger = logger;
|
|
_config = config;
|
|
_connectionString = _config.GetConnectionString("DefaultConnection");
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region HTTP Routes
|
|
public IActionResult Get()
|
|
{
|
|
var coverArtContext = new CoverArtContext(_connectionString);
|
|
|
|
var coverArtRecords = coverArtContext.CoverArtImages.ToList();
|
|
|
|
if (coverArtRecords == null)
|
|
{
|
|
_logger.LogInformation("No cover art records");
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
_logger.LogInformation("Found cover art records");
|
|
return Ok(coverArtRecords);
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
[Authorize("download:cover_art")]
|
|
public async Task<IActionResult> Get(int id)
|
|
{
|
|
var coverArt = new CoverArt { CoverArtID = id };
|
|
|
|
var coverArtContext = new CoverArtContext(_connectionString);
|
|
|
|
coverArt = coverArtContext.RetrieveRecord(coverArt);
|
|
|
|
if (coverArt != null)
|
|
{
|
|
_logger.LogInformation("Found cover art record");
|
|
var coverArtBytes = await System.IO.File.ReadAllBytesAsync(
|
|
coverArt.ImagePath);
|
|
|
|
return File(coverArtBytes, "application/x-msdownload",
|
|
coverArt.SongTitle);
|
|
}
|
|
else
|
|
{
|
|
_logger.LogInformation("Cover art not found");
|
|
return NotFound();
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|