roadie/RoadieApi/Controllers/ImageController.cs

221 lines
9.3 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("image")]
[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-11 20:10:10 +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-11-11 20:10:10 +00:00
[HttpGet("thumbnail/artist/{id}/{width:int?}/{height:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> ArtistThumbnail(Guid id, int? width, int? height)
{
var result = await this.ImageService.ArtistThumbnail(id, width ?? this.RoadieSettings.Thumbnails.Width, height ?? this.RoadieSettings.Thumbnails.Height);
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);
}
[HttpGet("thumbnail/collection/{id}/{width:int?}/{height:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> CollectionThumbnail(Guid id, int? width, int? height)
{
var result = await this.ImageService.CollectionThumbnail(id, width ?? this.RoadieSettings.Thumbnails.Width, height ?? this.RoadieSettings.Thumbnails.Height);
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-11-11 16:20:33 +00:00
[HttpGet("{id}/{width:int?}/{height:int?}")]
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-11-11 20:10:10 +00:00
[HttpGet("thumbnail/label/{id}/{width:int?}/{height:int?}")]
2018-11-11 16:20:33 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(404)]
2018-11-11 20:10:10 +00:00
public async Task<IActionResult> LabelThumbnail(Guid id, int? width, int? height)
2018-11-11 16:20:33 +00:00
{
2018-11-11 20:10:10 +00:00
var result = await this.ImageService.LabelThumbnail(id, width ?? this.RoadieSettings.Thumbnails.Width, height ?? this.RoadieSettings.Thumbnails.Height);
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);
}
[HttpGet("thumbnail/playlist/{id}/{width:int?}/{height:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> PlaylistThumbnail(Guid id, int? width, int? height)
{
var result = await this.ImageService.PlaylistThumbnail(id, width ?? this.RoadieSettings.Thumbnails.Width, height ?? this.RoadieSettings.Thumbnails.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);
}
[HttpGet("thumbnail/release/{id}/{width:int?}/{height:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> ReleaseThumbnail(Guid id, int? width, int? height)
{
var result = await this.ImageService.ReleaseThumbnail(id, width ?? this.RoadieSettings.Thumbnails.Width, height ?? this.RoadieSettings.Thumbnails.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-11-11 20:10:10 +00:00
[HttpGet("thumbnail/track/{id}/{width:int?}/{height:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> TrackThumbnail(Guid id, int? width, int? height)
{
var result = await this.ImageService.TrackThumbnail(id, width ?? this.RoadieSettings.Thumbnails.Width, height ?? this.RoadieSettings.Thumbnails.Height);
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);
}
[HttpGet("thumbnail/user/{id}/{width:int?}/{height:int?}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> UserThumbnail(Guid id, int? width, int? height)
{
var result = await this.ImageService.UserThumbnail(id, width ?? this.RoadieSettings.Thumbnails.Width, height ?? this.RoadieSettings.Thumbnails.Height);
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-05 00:08:37 +00:00
}
}