Petit Cleanup pour l'ImageController.

This commit is contained in:
MarcEricMartel 2022-11-02 09:40:09 -07:00
parent e92dfc0772
commit 1fb03a37c2
5 changed files with 76 additions and 71 deletions

View File

@ -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")]
public class ImageController : ControllerBase {
#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;
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;
_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<ProductModel> query;
string path;
string filename;
string filetype;
if (_cache is not null)
query = _cache.queryCache();
else query = _context.Products;
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) {
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 = thumbnail == true ? Path.Combine(_hostEnvironment.ContentRootPath, "Images", Path.GetFileNameWithoutExtension(filename) + "_thumbnail" + Path.GetExtension(filename))
: Path.Combine(_hostEnvironment.ContentRootPath, "Images", filename);
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 ".jpg":
case ".jpeg":
filetype = "image/jpeg";
break;
case ".png":
filetype = "image/png";
break;
default:
case ".jpg": case ".jpeg": default:
filetype = "image/jpeg";
break;
}
return File(System.IO.File.ReadAllBytes(path), filetype);
}
byte[] imageData = System.IO.File.ReadAllBytes(path);
return File(imageData, filetype);
}
}
#endregion
}

View File

@ -171,4 +171,3 @@ public class InventoryController : Controller {
#endregion
}

View File

@ -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 {

View File

@ -107,7 +107,7 @@ public class ProductController : ControllerBase {
#endregion
#region UtilityMethods
#region Utility Methods
private async Task<string> 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

View File

@ -161,4 +161,3 @@ public class SearchController : Controller {
#endregion
}