Fix de modification et delete d'image

This commit is contained in:
Victor Turgeon
2022-11-05 13:41:45 -04:00
parent 3f6c303865
commit d5cb00dc3d
3 changed files with 74 additions and 21 deletions

View File

@@ -86,11 +86,11 @@ public class ProductController : ControllerBase {
[EnableCors("_myAllowSpecificOrigins"), HttpPatch()]
public async Task<ActionResult<ProductModel>> Patch([FromForm] ProductModel prod) {
string? oldImage = "";
if (prod.Price <= prod.PromoPrice)
prod.PromoPrice = prod.Price - 0.01M;
try {
if (prod.ImageFile is not null) {
oldImage = _context.Products.FirstOrDefault(x => x.Id == prod.Id).ImageName;
if (oldImage == prod.ImageName)
oldImage = "";
if (prod.ImageFile is not null) {
oldImage = prod.ImageName;
prod.ImageName = await SaveImage(prod.ImageFile);
}
@@ -157,11 +157,15 @@ public class ProductController : ControllerBase {
}
private void DeleteImages(string imageName) {
if (imageName == "default.jpg" || imageName == "default_thumbnail.jpg")
return;
var files = System.IO.Directory.GetFiles(_hostEnvironment.ContentRootPath + "/Images")
.Where(x => x.Contains(imageName)).ToArray();
.Where(x => x.Contains(Path.GetFileNameWithoutExtension(imageName))).ToArray();
foreach (var file in files)
System.IO.File.Delete(_hostEnvironment.ContentRootPath + "/Images/" + file);
System.IO.File.Delete(file);
}
#endregion