Petit Cleanup pour l'ImageController.
This commit is contained in:
		| @@ -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.Authorization; | ||||||
| using Microsoft.AspNetCore.Cors; | using Microsoft.AspNetCore.Cors; | ||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
|  |  | ||||||
| namespace GrossesMitainesAPI.Controllers { | #endregion | ||||||
|     [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), Authorize(AuthenticationSchemes = "Identity.Application")] |  | ||||||
|  |  | ||||||
|     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 InventoryContext _context; | ||||||
|     private readonly IWebHostEnvironment _hostEnvironment; |     private readonly IWebHostEnvironment _hostEnvironment; | ||||||
|     private readonly ILogger<ProductController> _logger; |     private readonly ILogger<ProductController> _logger; | ||||||
|  |     private readonly DatabaseCacheService? _cache = null; | ||||||
|  |  | ||||||
|         public ImageController(ILogger<ProductController> logger, InventoryContext context, IWebHostEnvironment hostEnvironment) { |     #endregion | ||||||
|  |  | ||||||
|  |     #region Ctor | ||||||
|  |     public ImageController(ILogger<ProductController> logger, InventoryContext context, IWebHostEnvironment hostEnvironment, DatabaseCacheService cache) { | ||||||
|         _context = context; |         _context = context; | ||||||
|         _hostEnvironment = hostEnvironment; |         _hostEnvironment = hostEnvironment; | ||||||
|         _logger = logger; |         _logger = logger; | ||||||
|  |         if (cache.isOk()) | ||||||
|  |             _cache = cache; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     #endregion | ||||||
|  |  | ||||||
|  |     #region API Methods | ||||||
|     [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Image"), AllowAnonymous] |     [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Image"), AllowAnonymous] | ||||||
|     public IActionResult Get(int id, bool? thumbnail = false) { |     public IActionResult Get(int id, bool? thumbnail = false) { | ||||||
|  |         string path, filename, filetype; | ||||||
|  |         IQueryable<ProductModel> query; | ||||||
|  |  | ||||||
|             string path; |         if (_cache is not null) | ||||||
|             string filename; |             query = _cache.queryCache(); | ||||||
|             string filetype; |         else query = _context.Products; | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|                 var prod = _context.Products.Where(x => x.Id == id).First(); |             filename = query.Where(x => x.Id == id).First().ImageName ??  | ||||||
|                 filename = prod.ImageName ?? throw new Exception("Unable to find product image name. Sending default.jpg instead..."); |                 throw new Exception("Unable to find product image name. Sending default.jpg instead..."); | ||||||
|             } |         } catch (Exception e) { | ||||||
|             catch (Exception e) { |  | ||||||
|             _logger.LogError(8, e.Message); |             _logger.LogError(8, e.Message); | ||||||
|             filename = "default.jpg"; |             filename = "default.jpg"; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", | ||||||
|             path = thumbnail == true ? Path.Combine(_hostEnvironment.ContentRootPath, "Images", Path.GetFileNameWithoutExtension(filename) + "_thumbnail" + Path.GetExtension(filename)) |                             Path.GetFileNameWithoutExtension(filename) +  | ||||||
|                 : Path.Combine(_hostEnvironment.ContentRootPath, "Images", filename); |                             (thumbnail == true ? "_thumbnail" : "") +  | ||||||
|  |                             Path.GetExtension(filename)); | ||||||
|                     |                     | ||||||
|         if (!System.IO.File.Exists(path)) { |         if (!System.IO.File.Exists(path)) { | ||||||
|             _logger.LogError(8, "Unable to find image. Sending default image instead..."); |             _logger.LogError(8, "Unable to find image. Sending default image instead..."); | ||||||
|             path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", "default.jpg"); |             path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", "default.jpg"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|         switch (Path.GetExtension(path)) { |         switch (Path.GetExtension(path)) { | ||||||
|                 case ".jpg": |  | ||||||
|                 case ".jpeg": |  | ||||||
|                     filetype = "image/jpeg"; |  | ||||||
|                     break; |  | ||||||
|             case ".png": |             case ".png": | ||||||
|                 filetype = "image/png"; |                 filetype = "image/png"; | ||||||
|                 break; |                 break; | ||||||
|                 default: |             case ".jpg": case ".jpeg": default: | ||||||
|                 filetype = "image/jpeg"; |                 filetype = "image/jpeg"; | ||||||
|                 break; |                 break; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         return File(System.IO.File.ReadAllBytes(path), filetype); | ||||||
|  |     } | ||||||
|  |  | ||||||
|             byte[] imageData = System.IO.File.ReadAllBytes(path); |     #endregion | ||||||
|             return File(imageData, filetype); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| @@ -171,4 +171,3 @@ public class InventoryController : Controller { | |||||||
|  |  | ||||||
|     #endregion |     #endregion | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,9 +1,10 @@ | |||||||
| using GrossesMitainesAPI.Data; | namespace GrossesMitainesAPI.Controllers; | ||||||
|  |  | ||||||
|  | using GrossesMitainesAPI.Data; | ||||||
| using Microsoft.AspNetCore.Authorization; | using Microsoft.AspNetCore.Authorization; | ||||||
| using Microsoft.AspNetCore.Cors; | using Microsoft.AspNetCore.Cors; | ||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
|  |  | ||||||
| namespace GrossesMitainesAPI.Controllers; |  | ||||||
| [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), | [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), | ||||||
|  Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")] |  Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")] | ||||||
| public class InvoiceController : Controller { | public class InvoiceController : Controller { | ||||||
|   | |||||||
| @@ -107,7 +107,7 @@ public class ProductController : ControllerBase { | |||||||
|  |  | ||||||
|     #endregion |     #endregion | ||||||
|  |  | ||||||
|     #region UtilityMethods |     #region Utility Methods | ||||||
|     private async Task<string> SaveImage(IFormFile imageFile) { |     private async Task<string> SaveImage(IFormFile imageFile) { | ||||||
|         string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-'); |         string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-'); | ||||||
|         imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(imageFile.FileName); |         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) { |     private void SaveImageThumbnail(FileStream stream, string imageName) { | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             const float maxSize = 200f; |             const float maxSize = 200f; | ||||||
|             Image image = Image.FromStream(stream); |             Image image = Image.FromStream(stream); | ||||||
| @@ -136,8 +135,6 @@ public class ProductController : ControllerBase { | |||||||
|         catch (Exception ex) { |         catch (Exception ex) { | ||||||
|             _logger.LogError(8, ex.Message); |             _logger.LogError(8, ex.Message); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     #endregion |     #endregion | ||||||
|   | |||||||
| @@ -161,4 +161,3 @@ public class SearchController : Controller { | |||||||
|  |  | ||||||
|     #endregion |     #endregion | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user