From 1fb03a37c2dfc04411a313cfd6cd814af5b3cbfe Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Wed, 2 Nov 2022 09:40:09 -0700 Subject: [PATCH] Petit Cleanup pour l'ImageController. --- .../Controllers/ImageController.cs | 127 ++++++++++-------- .../Controllers/InventoryController.cs | 3 +- .../Controllers/InvoiceController.cs | 5 +- .../Controllers/ProductController.cs | 9 +- .../Controllers/SearchController.cs | 3 +- 5 files changed, 76 insertions(+), 71 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ImageController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ImageController.cs index b3e921a..1ce66d0 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ImageController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ImageController.cs @@ -1,67 +1,76 @@ -using GrossesMitainesAPI.Data; +namespace GrossesMitainesAPI.Controllers; + +#region Dependencies +using GrossesMitainesAPI.Data; +using GrossesMitainesAPI.Models; +using GrossesMitainesAPI.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; -namespace GrossesMitainesAPI.Controllers { - [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), Authorize(AuthenticationSchemes = "Identity.Application")] +#endregion - public class ImageController : ControllerBase { +[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 _logger; + private readonly DatabaseCacheService? _cache = null; + #endregion - private readonly InventoryContext _context; - private readonly IWebHostEnvironment _hostEnvironment; - private readonly ILogger _logger; - - public ImageController(ILogger logger, InventoryContext context, IWebHostEnvironment hostEnvironment) { - _context = context; - _hostEnvironment = hostEnvironment; - _logger = logger; - } - - - [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Image"), AllowAnonymous] - public IActionResult Get(int id, bool? thumbnail = false) { - - string path; - string filename; - string filetype; - - try { - var prod = _context.Products.Where(x => x.Id == id).First(); - filename = prod.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"; - } - - - path = thumbnail == true ? Path.Combine(_hostEnvironment.ContentRootPath, "Images", Path.GetFileNameWithoutExtension(filename) + "_thumbnail" + Path.GetExtension(filename)) - : Path.Combine(_hostEnvironment.ContentRootPath, "Images", 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"); - } - - - switch (Path.GetExtension(path)) { - case ".jpg": - case ".jpeg": - filetype = "image/jpeg"; - break; - case ".png": - filetype = "image/png"; - break; - default: - filetype = "image/jpeg"; - break; - } - - - byte[] imageData = System.IO.File.ReadAllBytes(path); - return File(imageData, filetype); - } + #region Ctor + public ImageController(ILogger logger, InventoryContext context, IWebHostEnvironment hostEnvironment, DatabaseCacheService cache) { + _context = context; + _hostEnvironment = hostEnvironment; + _logger = logger; + if (cache.isOk()) + _cache = cache; } -} + + #endregion + + #region API Methods + [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Image"), AllowAnonymous] + public IActionResult Get(int id, bool? thumbnail = false) { + string path, filename, filetype; + IQueryable query; + + if (_cache is not null) + query = _cache.queryCache(); + else query = _context.Products; + + 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"; + } + + 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"); + } + + switch (Path.GetExtension(path)) { + case ".png": + filetype = "image/png"; + break; + case ".jpg": case ".jpeg": default: + filetype = "image/jpeg"; + break; + } + + return File(System.IO.File.ReadAllBytes(path), filetype); + } + + #endregion +} \ No newline at end of file diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index d30a784..232c7a6 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -170,5 +170,4 @@ public class InventoryController : Controller { //} #endregion -} - +} \ No newline at end of file diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs index 9485c24..1349439 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs @@ -1,9 +1,10 @@ -using GrossesMitainesAPI.Data; +namespace GrossesMitainesAPI.Controllers; + +using GrossesMitainesAPI.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; -namespace GrossesMitainesAPI.Controllers; [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")] public class InvoiceController : Controller { diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 7da53d4..4f21164 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -32,7 +32,7 @@ public class ProductController : ControllerBase { private readonly IWebHostEnvironment _hostEnvironment; #endregion - + #region Ctor public ProductController(ILogger logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) { _logger = logger; @@ -42,7 +42,7 @@ public class ProductController : ControllerBase { } #endregion - + #region API Methods [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous] public ActionResult Get(int id) { @@ -107,7 +107,7 @@ public class ProductController : ControllerBase { #endregion - #region UtilityMethods + #region Utility Methods private async Task SaveImage(IFormFile imageFile) { string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-'); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(imageFile.FileName); @@ -121,7 +121,6 @@ public class ProductController : ControllerBase { } private void SaveImageThumbnail(FileStream stream, string imageName) { - try { const float maxSize = 200f; Image image = Image.FromStream(stream); @@ -136,8 +135,6 @@ public class ProductController : ControllerBase { catch (Exception ex) { _logger.LogError(8, ex.Message); } - - } #endregion diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 9d2704d..b106e71 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -160,5 +160,4 @@ public class SearchController : Controller { } #endregion -} - +} \ No newline at end of file