L'importation d'image fonctionne. Il manque à faire fonctionner la modifiction et le delete avec.
This commit is contained in:
		| @@ -0,0 +1,67 @@ | ||||
| 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")] | ||||
|  | ||||
|     public class ImageController : ControllerBase { | ||||
|  | ||||
|  | ||||
|         private readonly InventoryContext _context; | ||||
|         private readonly IWebHostEnvironment _hostEnvironment; | ||||
|         private readonly ILogger<ProductController> _logger; | ||||
|  | ||||
|         public ImageController(ILogger<ProductController> 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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -9,10 +9,10 @@ using Microsoft.Extensions.Logging; | ||||
| using Microsoft.AspNetCore.Cors; | ||||
| using GrossesMitainesAPI.Services; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
|  | ||||
| using Microsoft.Extensions.Hosting; | ||||
| #endregion | ||||
|  | ||||
| [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),  | ||||
| [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), | ||||
|  Authorize(AuthenticationSchemes = "Identity.Application")] | ||||
| public class InventoryController : Controller { | ||||
|     #region Constants | ||||
| @@ -24,14 +24,16 @@ public class InventoryController : Controller { | ||||
|     private readonly ILogger<InventoryController> _logger; | ||||
|     private readonly InventoryContext _context; | ||||
|     private readonly DatabaseCacheService _cache; | ||||
|     private readonly IWebHostEnvironment _hostEnvironment; | ||||
|  | ||||
|     #endregion | ||||
|  | ||||
|     #region Ctor | ||||
|     public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) { | ||||
|     public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) { | ||||
|         _context = context; | ||||
|         _logger = logger; | ||||
|         _cache = cache; | ||||
|         _hostEnvironment = hostEnvironment; | ||||
|     } | ||||
|  | ||||
|     #endregion | ||||
| @@ -41,7 +43,7 @@ public class InventoryController : Controller { | ||||
|     public IEnumerable<ProductViewModel> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { | ||||
|         bool iscache = false; | ||||
|         IQueryable<ProductViewModel> ret; | ||||
|         if (_cache.isOk()) {  | ||||
|         if (_cache.isOk()) { | ||||
|             ret = _cache.queryCache().Select(x => new ProductViewModel(x)); | ||||
|             iscache = true; | ||||
|         } | ||||
| @@ -78,20 +80,20 @@ public class InventoryController : Controller { | ||||
|                 ret = ret.Where(x => x.Status == ProductModel.States.Discontinued); | ||||
|                 break; | ||||
|             case "isPromoted": | ||||
|                 ret = ret.Where(x => x.Status == ProductModel.States.Clearance ||  | ||||
|                 ret = ret.Where(x => x.Status == ProductModel.States.Clearance || | ||||
|                                      x.Status == ProductModel.States.Promotion); | ||||
|                 break; | ||||
|             default: break; | ||||
|         } | ||||
|         switch (order) { | ||||
|             case "Price": | ||||
|                 ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion ||  | ||||
|                                        x.Status == ProductModel.States.Clearance ?  | ||||
|                 ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion || | ||||
|                                        x.Status == ProductModel.States.Clearance ? | ||||
|                                             x.PromoPrice : x.Price); | ||||
|                 break; | ||||
|             case "PriceDesc": | ||||
|                 ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion ||  | ||||
|                                                  x.Status == ProductModel.States.Clearance ?  | ||||
|                 ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion || | ||||
|                                                  x.Status == ProductModel.States.Clearance ? | ||||
|                                                         x.PromoPrice : x.Price); | ||||
|                 break; | ||||
|             case "Title": | ||||
| @@ -114,9 +116,10 @@ public class InventoryController : Controller { | ||||
|         try { | ||||
|             if (!lastId.HasValue || lastId == 0) | ||||
|                 yup = true; | ||||
|              | ||||
|  | ||||
|             foreach (ProductViewModel prod in ret.ToList()) { | ||||
|                 if (yup && add < AMOUNT_SCROLL || (all.HasValue && all == true)) { | ||||
|  | ||||
|                     lst.Add(prod); | ||||
|                     add++; | ||||
|                 } | ||||
| @@ -124,13 +127,15 @@ public class InventoryController : Controller { | ||||
|                     yup = true; | ||||
|             } | ||||
|             return lst; | ||||
|         } catch (Exception e) { | ||||
|         } | ||||
|         catch (Exception e) { | ||||
|             if (iscache) | ||||
|                 _logger.LogError(e, "Erreur d'appel de cache."); | ||||
|             else _logger.LogError(e, "Erreur d'appel d'API."); | ||||
|             return new List<ProductViewModel>(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     // Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD. | ||||
|     //[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous] | ||||
|     //public ActionResult<int> Delete(int? id) { | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
		Reference in New Issue
	
	Block a user