roadie/Roadie.Api/Controllers/ImageController.cs

282 lines
12 KiB
C#
Raw Permalink Normal View History

using Microsoft.AspNetCore.Identity;
2018-11-05 00:08:37 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
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]
public class ImageController : EntityControllerBase
{
2018-11-11 01:11:58 +00:00
private IImageService ImageService { get; }
public ImageController(
IImageService imageService,
ILogger<ImageController> logger,
ICacheManager cacheManager,
UserManager<User> userManager,
IRoadieSettings roadieSettings)
: base(cacheManager, roadieSettings, userManager)
2018-11-05 00:08:37 +00:00
{
Logger = logger;
2019-07-03 16:21:29 +00:00
ImageService = imageService;
2018-11-05 00:08:37 +00:00
}
private IActionResult MakeFileResult(byte[] bytes, string fileName, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue eTag) => File(bytes, contentType, fileName, lastModified, eTag);
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
{
var result = await ImageService.ArtistImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, 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)
{
var result = await ImageService.ArtistSecondaryImageAsync(id, imageId, width, height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, 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
{
var result = await ImageService.CollectionImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, result.LastModified, result.ETag);
2018-11-11 01:11:58 +00:00
}
2018-11-11 16:20:33 +00:00
[HttpGet("genre/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2018-11-11 16:20:33 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> GenreImage(Guid id, int? width, int? height)
2018-11-11 16:20:33 +00:00
{
var result = await ImageService.GenreImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, result.LastModified, result.ETag);
2018-11-11 20:10:10 +00:00
}
[HttpGet("label/{id}/{width:int?}/{height:int?}/{cacheBuster?}")]
2019-08-02 20:59:24 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> LabelImage(Guid id, int? width, int? height)
2019-08-02 20:59:24 +00:00
{
var result = await ImageService.LabelImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, result.LastModified, result.ETag);
2019-08-02 20:59:24 +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
{
var result = await ImageService.PlaylistImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, 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
{
var result = await ImageService.ReleaseImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, 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)
{
var result = await ImageService.ReleaseSecondaryImageAsync(id, imageId, width ?? RoadieSettings.MaximumImageSize.Width, height ?? RoadieSettings.MaximumImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, 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
{
var result = await ImageService.SearchAsync(query, resultsCount ?? 10).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
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
}
[HttpPost("search/genre/{query}/{resultsCount:int?}")]
2018-11-11 20:10:10 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> SearchForGenreImage(string query, int? resultsCount)
2018-11-11 20:10:10 +00:00
{
var result = await ImageService.SearchAsync(query, resultsCount ?? 10).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
2019-11-04 03:19:04 +00:00
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/label/{query}/{resultsCount:int?}")]
2019-08-02 20:59:24 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> SearchForLabelImage(string query, int? resultsCount)
2019-08-02 20:59:24 +00:00
{
var result = await ImageService.SearchAsync(query, resultsCount ?? 10).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-08-02 20:59:24 +00:00
return Ok(result);
}
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)
{
var result = await ImageService.SearchAsync(query, resultsCount ?? 10).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
{
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
{
var result = await ImageService.TrackImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.jpg", result.ContentType, 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>
/// NOTE that user images/avatars are GIF not JPG this is so it looks better in the menus/applications and allows for animated avatars.
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
{
var result = await ImageService.UserImageAsync(id, width ?? RoadieSettings.ThumbnailImageSize.Width, height ?? RoadieSettings.ThumbnailImageSize.Height).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-11-04 03:19:04 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return MakeFileResult(result.Data.Bytes, $"{id}.gif", result.ContentType, result.LastModified, result.ETag);
2019-02-03 17:50:17 +00:00
}
2018-11-05 00:08:37 +00:00
}
2022-01-18 22:52:02 +00:00
}