L'importation d'image fonctionne. Il manque à faire fonctionner la modifiction et le delete avec.

This commit is contained in:
Victor Turgeon
2022-11-01 18:50:32 -04:00
parent f38f4bf44a
commit 9df28d3eed
47 changed files with 198 additions and 34 deletions

View File

@@ -10,7 +10,9 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Services;
using System.Drawing;
using System.IO;
using Microsoft.EntityFrameworkCore.Storage;
#endregion
/// <summary>
@@ -57,7 +59,7 @@ public class ProductController : ControllerBase {
}
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
public async Task<ActionResult<ProductModel>> Post(ProductModel prod) {
public async Task<ActionResult<ProductModel>> Post([FromForm] ProductModel prod) {
if (prod.Price <= prod.PromoPrice)
prod.PromoPrice = prod.Price - 0.01M;
try {
@@ -110,10 +112,33 @@ public class ProductController : ControllerBase {
string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-');
imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(imageFile.FileName);
var imagePath = Path.Combine(_hostEnvironment.ContentRootPath, "Images", imageName);
using (var fileStream = new FileStream(imagePath, FileMode.Create)) {
await imageFile.CopyToAsync(fileStream);
SaveImageThumbnail(fileStream, imageName);
}
return imageName;
}
private void SaveImageThumbnail(FileStream stream, string imageName) {
try {
const float maxSize = 200f;
Image image = Image.FromStream(stream);
//Choisi le bon ratio de division pour ne pas dépasser le 200px ni dans height ni dans width
float ratio = image.Width / (image.Height / maxSize) <= maxSize ? image.Height / maxSize : image.Width / maxSize;
Bitmap resize = new Bitmap(image, new Size((int)(image.Width / ratio), (int)(image.Height / ratio)));
string path = Path.Combine(_hostEnvironment.ContentRootPath, "Images", Path.GetFileNameWithoutExtension(imageName) + "_thumbnail" + Path.GetExtension(imageName));
resize.Save(path);
}
catch (Exception ex) {
_logger.LogError(8, ex.Message);
}
}
#endregion
}