2022-11-02 12:40:09 -04:00
|
|
|
|
namespace GrossesMitainesAPI.Controllers;
|
|
|
|
|
|
|
|
|
|
#region Dependencies
|
|
|
|
|
using GrossesMitainesAPI.Data;
|
|
|
|
|
using GrossesMitainesAPI.Models;
|
|
|
|
|
using GrossesMitainesAPI.Services;
|
2022-11-01 18:50:32 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
2022-11-02 12:40:09 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
|
|
|
|
Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
|
|
|
public class ImageController : ControllerBase {
|
|
|
|
|
#region DI Fields
|
|
|
|
|
private readonly InventoryContext _context;
|
|
|
|
|
private readonly IWebHostEnvironment _hostEnvironment;
|
|
|
|
|
private readonly ILogger<ProductController> _logger;
|
|
|
|
|
private readonly DatabaseCacheService? _cache = null;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
|
|
|
|
public ImageController(ILogger<ProductController> logger, InventoryContext context, IWebHostEnvironment hostEnvironment, DatabaseCacheService cache) {
|
|
|
|
|
_context = context;
|
|
|
|
|
_hostEnvironment = hostEnvironment;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
if (cache.isOk())
|
|
|
|
|
_cache = cache;
|
|
|
|
|
}
|
2022-11-01 18:50:32 -04:00
|
|
|
|
|
2022-11-02 12:40:09 -04:00
|
|
|
|
#endregion
|
2022-11-01 18:50:32 -04:00
|
|
|
|
|
2022-11-02 12:40:09 -04:00
|
|
|
|
#region API Methods
|
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Image"), AllowAnonymous]
|
|
|
|
|
public IActionResult Get(int id, bool? thumbnail = false) {
|
|
|
|
|
string path, filename, filetype;
|
|
|
|
|
IQueryable<ProductModel> query;
|
2022-11-01 18:50:32 -04:00
|
|
|
|
|
2022-11-02 12:40:09 -04:00
|
|
|
|
if (_cache is not null)
|
|
|
|
|
query = _cache.queryCache();
|
|
|
|
|
else query = _context.Products;
|
2022-11-01 18:50:32 -04:00
|
|
|
|
|
2022-11-02 12:40:09 -04:00
|
|
|
|
try {
|
|
|
|
|
filename = query.Where(x => x.Id == id).First().ImageName ??
|
|
|
|
|
throw new Exception("Unable to find product image name. Sending default.jpg instead...");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(8, e.Message);
|
|
|
|
|
filename = "default.jpg";
|
2022-11-01 18:50:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 12:40:09 -04:00
|
|
|
|
path = Path.Combine(_hostEnvironment.ContentRootPath, "Images",
|
|
|
|
|
Path.GetFileNameWithoutExtension(filename) +
|
|
|
|
|
(thumbnail == true ? "_thumbnail" : "") +
|
|
|
|
|
Path.GetExtension(filename));
|
|
|
|
|
|
|
|
|
|
if (!System.IO.File.Exists(path)) {
|
|
|
|
|
_logger.LogError(8, "Unable to find image. Sending default image instead...");
|
|
|
|
|
path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", "default.jpg");
|
|
|
|
|
}
|
2022-11-01 18:50:32 -04:00
|
|
|
|
|
2022-11-02 12:40:09 -04:00
|
|
|
|
switch (Path.GetExtension(path)) {
|
|
|
|
|
case ".png":
|
|
|
|
|
filetype = "image/png";
|
|
|
|
|
break;
|
|
|
|
|
case ".jpg": case ".jpeg": default:
|
|
|
|
|
filetype = "image/jpeg";
|
|
|
|
|
break;
|
2022-11-01 18:50:32 -04:00
|
|
|
|
}
|
2022-11-02 12:40:09 -04:00
|
|
|
|
|
|
|
|
|
return File(System.IO.File.ReadAllBytes(path), filetype);
|
2022-11-01 18:50:32 -04:00
|
|
|
|
}
|
2022-11-02 12:40:09 -04:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|