react-version #1

Merged
memartel_loc merged 290 commits from react-version into main 2023-11-04 09:48:15 -04:00
5 changed files with 76 additions and 71 deletions
Showing only changes of commit 1fb03a37c2 - Show all commits

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.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 IWebHostEnvironment _hostEnvironment;
private readonly ILogger<ProductController> _logger;
private readonly DatabaseCacheService? _cache = null;
#endregion
private readonly InventoryContext _context; #region Ctor
private readonly IWebHostEnvironment _hostEnvironment; public ImageController(ILogger<ProductController> logger, InventoryContext context, IWebHostEnvironment hostEnvironment, DatabaseCacheService cache) {
private readonly ILogger<ProductController> _logger; _context = context;
_hostEnvironment = hostEnvironment;
public ImageController(ILogger<ProductController> logger, InventoryContext context, IWebHostEnvironment hostEnvironment) { _logger = logger;
_context = context; if (cache.isOk())
_hostEnvironment = hostEnvironment; _cache = cache;
_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);
}
} }
}
#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;
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
}

View File

@ -170,5 +170,4 @@ public class InventoryController : Controller {
//} //}
#endregion #endregion
} }

View File

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

View File

@ -32,7 +32,7 @@ public class ProductController : ControllerBase {
private readonly IWebHostEnvironment _hostEnvironment; private readonly IWebHostEnvironment _hostEnvironment;
#endregion #endregion
#region Ctor #region Ctor
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) { public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) {
_logger = logger; _logger = logger;
@ -42,7 +42,7 @@ public class ProductController : ControllerBase {
} }
#endregion #endregion
#region API Methods #region API Methods
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous] [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
public ActionResult<ProductViewModel> Get(int id) { public ActionResult<ProductViewModel> Get(int id) {
@ -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

View File

@ -160,5 +160,4 @@ public class SearchController : Controller {
} }
#endregion #endregion
} }