Fix de modification et delete d'image
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user