roadie/Roadie.Api/Controllers/ImageController.cs

260 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.Configuration;
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;
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; }
2018-11-11 20:45:44 +00:00
public ImageController(IImageService imageService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager<ApplicationUser> userManager)
: base(cacheManager, configuration, userManager)
2018-11-05 00:08:37 +00:00
{
2018-11-21 06:34:53 +00:00
this.Logger = logger.CreateLogger("RoadieApi.Controllers.ImageController");
2018-11-11 01:11:58 +00:00
this.ImageService = imageService;
2018-11-05 00:08:37 +00:00
}
2018-11-07 04:33:22 +00:00
//[EnableQuery]
//public IActionResult Get()
//{
// return Ok(this._RoadieDbContext.Tracks.ProjectToType<models.Image>());
//}
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
{
var result = await this.ImageService.ArtistImage(id, width ?? this.RoadieSettings.ThumbnailImageSize.Width, height ?? this.RoadieSettings.ThumbnailImageSize.Height);
2018-11-11 20:10:10 +00:00
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.jpg",
lastModified: result.LastModified,
entityTag: result.ETag);
}
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 this.ImageService.CollectionImage(id, width ?? this.RoadieSettings.ThumbnailImageSize.Width, height ?? this.RoadieSettings.ThumbnailImageSize.Height);
2018-11-11 20:10:10 +00:00
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.jpg",
lastModified: result.LastModified,
entityTag: result.ETag);
}
[HttpPost("{id}")]
[Authorize(Policy = "Editor")]
[ProducesResponseType(200)]
public async Task<IActionResult> Delete(Guid id)
{
var result = await this.ImageService.Delete(await this.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)
{
var result = await this.ImageService.ById(id, width, height);
2018-11-11 01:11:58 +00:00
if (result == null)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2018-11-11 20:10:10 +00:00
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
2018-11-11 14:46:09 +00:00
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.jpg",
2018-11-11 20:10:10 +00:00
lastModified: result.LastModified,
2018-11-11 01:11:58 +00:00
entityTag: result.ETag);
}
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
{
var result = await this.ImageService.LabelImage(id, width ?? this.RoadieSettings.ThumbnailImageSize.Width, height ?? this.RoadieSettings.ThumbnailImageSize.Height);
2018-11-11 20:10:10 +00:00
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.jpg",
lastModified: result.LastModified,
entityTag: result.ETag);
}
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 this.ImageService.PlaylistImage(id, width ?? this.RoadieSettings.ThumbnailImageSize.Width, height ?? this.RoadieSettings.ThumbnailImageSize.Height);
2018-11-11 17:27:13 +00:00
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.jpg",
lastModified: result.LastModified,
entityTag: result.ETag);
}
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 this.ImageService.ReleaseImage(id, width ?? this.RoadieSettings.ThumbnailImageSize.Width, height ?? this.RoadieSettings.ThumbnailImageSize.Height);
2018-11-11 16:20:33 +00:00
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.jpg",
lastModified: result.LastModified,
entityTag: result.ETag);
}
2018-12-27 21:56:40 +00:00
[HttpGet("track/{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> TrackImage(Guid id, int? width, int? height)
2018-11-11 20:10:10 +00:00
{
var result = await this.ImageService.TrackImage(id, width ?? this.RoadieSettings.ThumbnailImageSize.Width, height ?? this.RoadieSettings.ThumbnailImageSize.Height);
2018-11-11 20:10:10 +00:00
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.jpg",
lastModified: result.LastModified,
entityTag: result.ETag);
}
2018-11-21 18:19:38 +00:00
/// <summary>
/// NOTE that user images/avatars are PNG not JPG this is so it looks better in the menus/applications
/// </summary>
2018-12-27 21:56:40 +00:00
[HttpGet("user/{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> UserImage(Guid id, int? width, int? height)
2018-11-11 20:10:10 +00:00
{
var result = await this.ImageService.UserImage(id, width ?? this.RoadieSettings.ThumbnailImageSize.Width, height ?? this.RoadieSettings.ThumbnailImageSize.Height);
2018-11-11 20:10:10 +00:00
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
2018-11-21 18:19:38 +00:00
fileDownloadName: $"{ result.Data.Caption ?? id.ToString()}.png",
2018-11-11 20:10:10 +00:00
lastModified: result.LastModified,
entityTag: result.ETag);
}
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 this.ImageService.Search(query, resultsCount ?? 10);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
2018-12-26 21:18:51 +00:00
[HttpPost("search/artist/{query}/{resultsCount:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> SearchForArtistImage(string query, int? resultsCount)
{
var result = await this.ImageService.Search(query, resultsCount ?? 10);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
2018-12-21 22:59:33 +00:00
2018-11-05 00:08:37 +00:00
}
}