roadie/Roadie.Api/Controllers/ImageController.cs

237 lines
11 KiB
C#
Raw Normal View History

2018-11-11 20:10:10 +00:00
using Microsoft.AspNetCore.Authorization;
2018-11-11 20:45:44 +00:00
using Microsoft.AspNetCore.Identity;
2018-11-05 00:08:37 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
2018-11-11 01:11:58 +00:00
using Roadie.Api.Services;
2018-11-05 00:08:37 +00:00
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
2018-11-11 20:45:44 +00:00
using Roadie.Library.Identity;
2018-11-05 00:08:37 +00:00
using System;
2018-11-11 01:11:58 +00:00
using System.Net;
using System.Threading.Tasks;
2018-11-05 00:08:37 +00:00
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("images")]
2018-11-05 00:08:37 +00:00
[ApiController]
// [Authorize]
2018-11-05 00:08:37 +00:00
public class ImageController : EntityControllerBase
{
2018-11-11 01:11:58 +00:00
private IImageService ImageService { get; }
2019-07-03 16:21:29 +00:00
public ImageController(IImageService imageService, ILoggerFactory logger, ICacheManager cacheManager,
UserManager<ApplicationUser> userManager, IRoadieSettings roadieSettings)
: base(cacheManager, roadieSettings, userManager)
2018-11-05 00:08:37 +00:00
{
2019-07-03 16:21:29 +00:00
Logger = logger.CreateLogger("RoadieApi.Controllers.ImageController");
ImageService = imageService;
2018-11-05 00:08:37 +00:00
}
2018-12-27 21:56:40 +00:00
[HttpGet("artist/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-11-11 20:10:10 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2018-11-21 18:19:38 +00:00
public async Task<IActionResult> ArtistImage(Guid id, int? width, int? height)
2018-11-11 20:10:10 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.ArtistImage(id, width ?? RoadieSettings.ThumbnailImageSize.Width,
height ?? RoadieSettings.ThumbnailImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2018-11-11 20:10:10 +00:00
}
2019-02-10 00:19:26 +00:00
[HttpGet("artist-secondary/{id}/{imageId}/{width:int?}/{height:int?}/{cacheBuster?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> ArtistSecondaryImage(Guid id, int imageId, int? width, int? height)
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.ArtistSecondaryImage(id, imageId,
width ?? RoadieSettings.MaximumImageSize.Width, height ?? RoadieSettings.MaximumImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2019-02-10 00:19:26 +00:00
}
2018-12-27 21:56:40 +00:00
[HttpGet("collection/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-11-11 20:10:10 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2018-11-21 18:19:38 +00:00
public async Task<IActionResult> CollectionImage(Guid id, int? width, int? height)
2018-11-11 20:10:10 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.CollectionImage(id, width ?? RoadieSettings.ThumbnailImageSize.Width,
height ?? RoadieSettings.ThumbnailImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2018-11-11 20:10:10 +00:00
}
2019-05-23 03:10:59 +00:00
[HttpPost("delete/{id}")]
[Authorize(Policy = "Editor")]
[ProducesResponseType(200)]
public async Task<IActionResult> Delete(Guid id)
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.Delete(await CurrentUserModel(), id);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return Ok(result);
}
2018-12-27 21:56:40 +00:00
[HttpGet("{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-11-11 01:11:58 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> Get(Guid id, int? width, int? height)
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.ById(id, width, height);
if (result == null) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2018-11-11 01:11:58 +00:00
}
2018-11-11 16:20:33 +00:00
2018-12-27 21:56:40 +00:00
[HttpGet("label/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-11-11 16:20:33 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2018-11-21 18:19:38 +00:00
public async Task<IActionResult> LabelImage(Guid id, int? width, int? height)
2018-11-11 16:20:33 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.LabelImage(id, width ?? RoadieSettings.ThumbnailImageSize.Width,
height ?? RoadieSettings.ThumbnailImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2018-11-11 20:10:10 +00:00
}
2018-12-27 21:56:40 +00:00
[HttpGet("playlist/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-11-11 20:10:10 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2018-11-21 18:19:38 +00:00
public async Task<IActionResult> PlaylistImage(Guid id, int? width, int? height)
2018-11-11 20:10:10 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.PlaylistImage(id, width ?? RoadieSettings.ThumbnailImageSize.Width,
height ?? RoadieSettings.ThumbnailImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2018-11-11 17:27:13 +00:00
}
2018-12-27 21:56:40 +00:00
[HttpGet("release/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-11-11 17:27:13 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2018-11-21 18:19:38 +00:00
public async Task<IActionResult> ReleaseImage(Guid id, int? width, int? height)
2018-11-11 17:27:13 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.ReleaseImage(id, width ?? RoadieSettings.ThumbnailImageSize.Width,
height ?? RoadieSettings.ThumbnailImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2018-11-11 16:20:33 +00:00
}
2019-02-10 00:19:26 +00:00
[HttpGet("release-secondary/{id}/{imageId}/{width:int?}/{height:int?}/{cacheBuster?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> ReleaseSecondaryImage(Guid id, int imageId, int? width, int? height)
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.ReleaseSecondaryImage(id, imageId,
width ?? RoadieSettings.MaximumImageSize.Width, height ?? RoadieSettings.MaximumImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2019-02-10 00:19:26 +00:00
}
2019-05-29 22:25:40 +00:00
[HttpPost("search/artist/{query}/{resultsCount:int?}")]
2018-11-11 20:10:10 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2019-05-29 22:25:40 +00:00
public async Task<IActionResult> SearchForArtistImage(string query, int? resultsCount)
2018-11-11 20:10:10 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.Search(query, resultsCount ?? 10);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
2019-05-29 22:25:40 +00:00
return Ok(result);
2018-11-11 20:10:10 +00:00
}
2019-05-29 22:25:40 +00:00
[HttpPost("search/label/{query}/{resultsCount:int?}")]
2018-11-11 20:10:10 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2019-05-29 22:25:40 +00:00
public async Task<IActionResult> SearchForLabelImage(string query, int? resultsCount)
2018-11-11 20:10:10 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.Search(query, resultsCount ?? 10);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
2019-05-29 22:25:40 +00:00
return Ok(result);
2018-11-11 20:10:10 +00:00
}
2018-12-21 22:59:33 +00:00
[HttpPost("search/release/{query}/{resultsCount:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> SearchForReleaseCover(string query, int? resultsCount)
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.Search(query, resultsCount ?? 10);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
2018-12-21 22:59:33 +00:00
return Ok(result);
}
2019-05-29 22:25:40 +00:00
[HttpGet("track/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-12-26 21:18:51 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2019-05-29 22:25:40 +00:00
public async Task<IActionResult> TrackImage(Guid id, int? width, int? height)
2018-12-26 21:18:51 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.TrackImage(id, width ?? RoadieSettings.ThumbnailImageSize.Width,
height ?? RoadieSettings.ThumbnailImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.jpg",
result.LastModified,
result.ETag);
2018-12-26 21:18:51 +00:00
}
2018-12-21 22:59:33 +00:00
2019-05-29 22:25:40 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// NOTE that user images/avatars are PNG not JPG this is so it looks better in the menus/applications
2019-05-29 22:25:40 +00:00
/// </summary>
[HttpGet("user/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2019-02-03 17:50:17 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2019-05-29 22:25:40 +00:00
public async Task<IActionResult> UserImage(Guid id, int? width, int? height)
2019-02-03 17:50:17 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await ImageService.UserImage(id, width ?? RoadieSettings.ThumbnailImageSize.Width,
height ?? RoadieSettings.ThumbnailImageSize.Height);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return File(result.Data.Bytes,
result.ContentType,
$"{result.Data.Caption ?? id.ToString()}.png",
result.LastModified,
result.ETag);
2019-02-03 17:50:17 +00:00
}
2018-11-05 00:08:37 +00:00
}
}