diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java b/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java index f47f6ca..3748357 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/intern/InternArticleController.java @@ -3,10 +3,16 @@ package org.hso.ecommerce.controller.intern; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Optional; - import javax.imageio.ImageIO; import org.hso.ecommerce.entities.shop.Article; @@ -19,9 +25,12 @@ import org.hso.ecommerce.repos.shop.ImageRepository; import org.hso.ecommerce.repos.shop.OffersRepository; import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.view.RedirectView; @Controller @@ -80,12 +89,11 @@ public class InternArticleController { @RequestParam(value = "price_netto", required = true) String pricenetto, @RequestParam(value = "reorderMaxPrice", required = true) String reorderMaxPrice, @RequestParam(value = "autobuy", required = true) Boolean shouldReorder, - @RequestParam(value = "categories", required = true) String categories) { + @RequestParam(value = "categories", required = true) String categories, + @RequestParam(value = "img", required = true) MultipartFile imgFile) { Article tmpArticle = articleRepository.findArticleById(id); // get the old article - // TODO img - String[] separatedCategories = categories.split("\n"); tmpArticle.categories.clear(); @@ -102,6 +110,7 @@ public class InternArticleController { tmpArticle.shopPricePerUnitNetCent = (int) (Float.parseFloat(pricenetto) * 100); tmpArticle.warehouseUnitsPerSlot = Integer.parseInt(warehouseUnitsPerSlot); tmpArticle.title = title; + updateImage(tmpArticle, imgFile); tmpArticle.description = description; articleRepository.save(tmpArticle); // save updated article @@ -133,30 +142,56 @@ public class InternArticleController { return new RedirectView("../" + articleRepository.findArticleIDByRelatedID(offeredArticleID).get()); } - private void setDefaultImage(Article tmpArticle) { + private void setDefaultImage(Article article) { String defaultImagePath = "./data/img/no_product_img.jpg"; // path + name of default img - Optional imageID = imageRepository.findImageByPath(defaultImagePath); // get default img + Optional imageID = imageRepository.findImageIDByPath(defaultImagePath); // get default img if (imageID.isPresent()) { // default img is in DB - tmpArticle.image = imageRepository.findImageById(imageID.get()); // set default img to new article + article.image = imageRepository.findImageById(imageID.get()); // set default img to new article } else { - //default img is not in DB + // default img is not in DB File tmpFile = new File(defaultImagePath); - //test if default img file exits + // test if default img file exits if (!tmpFile.exists()) { - //fallback if the file not exists - BufferedImage bufferedImage = new BufferedImage(422, 428, BufferedImage.TYPE_INT_RGB); //create new file + // fallback if the file not exists + // create new file + BufferedImage bufferedImage = new BufferedImage(422, 428, BufferedImage.TYPE_INT_RGB); try { - ImageIO.write(bufferedImage, "jpg", new File(defaultImagePath)); //save new file on disk + ImageIO.write(bufferedImage, "jpg", new File(defaultImagePath)); // save new file on disk } catch (IOException e) { e.printStackTrace(); } } - Image defaultImage = new Image(); - defaultImage.path = defaultImagePath; //set new file to default img - imageRepository.save(defaultImage); //save default img - tmpArticle.image = defaultImage; //set default img to new article + Image defaultImage = new Image(); + defaultImage.path = defaultImagePath; // set new file to default img + imageRepository.save(defaultImage); // save default img + article.image = defaultImage; // set default img to new article + } + } + + private void updateImage(Article article, MultipartFile imgFile) { + // check if a file is present + if (imgFile.getSize() > 0) { + try { + // get file name based on MD5 hash + String fileName = DigestUtils.md5DigestAsHex(imgFile.getBytes()) + ".jpg"; + // check if img is already in system + Optional imageID = imageRepository.findImageIDByPath("./data/img/" + fileName); + if (imageID.isPresent()) { + article.image = imageRepository.findImageById(imageID.get()); // add existing img to article + } else { + // write new img file to disk + Files.newOutputStream(Paths.get("./data/img/", fileName)).write(imgFile.getBytes()); + // create new img + Image newImage = new Image(); + newImage.path = "./data/img/" + fileName; // set new file to new img + imageRepository.save(newImage); // save new img + article.image = newImage; // set new img to article + } + } catch (Exception e) { + setDefaultImage(article); // if upload failed, reset to default img + } } } diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java index bcef6be..d0f3997 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java @@ -12,7 +12,7 @@ import java.util.Optional; public interface ImageRepository extends JpaRepository { @Query("SELECT i.id FROM Image i WHERE i.path = :path") - Optional findImageByPath(@Param("path") String path); + Optional findImageIDByPath(@Param("path") String path); @Query("SELECT i FROM Image i WHERE i.id = :imageId") Image findImageById(@Param("imageId") long imageId); diff --git a/prototype/src/main/resources/application.properties b/prototype/src/main/resources/application.properties index 18ec88c..aee1c7d 100644 --- a/prototype/src/main/resources/application.properties +++ b/prototype/src/main/resources/application.properties @@ -16,6 +16,8 @@ spring.jpa.show-sql=true #server.servlet.session.persistent=true # ---------------------------------------- # WEB PROPERTIES +spring.servlet.multipart.max-file-size=10MB +spring.servlet.multipart.max-request-size=10MB # ---------------------------------------- # EMBEDDED SERVER CONFIGURATION (ServerProperties) server.address=::1 diff --git a/prototype/src/main/resources/static/css/ecom.css b/prototype/src/main/resources/static/css/ecom.css index 1d8f125..2a696d2 100644 --- a/prototype/src/main/resources/static/css/ecom.css +++ b/prototype/src/main/resources/static/css/ecom.css @@ -278,7 +278,7 @@ img.s { } img.m { - width: var(--u8); + width: 20rem; } /* diff --git a/prototype/src/main/resources/templates/intern/listedArticles/id.html b/prototype/src/main/resources/templates/intern/listedArticles/id.html index 7050d3c..1fad38c 100644 --- a/prototype/src/main/resources/templates/intern/listedArticles/id.html +++ b/prototype/src/main/resources/templates/intern/listedArticles/id.html @@ -24,7 +24,7 @@

- +

diff --git a/prototype/src/main/resources/templates/intern/listedArticles/index.html b/prototype/src/main/resources/templates/intern/listedArticles/index.html index 2840417..ad33f89 100644 --- a/prototype/src/main/resources/templates/intern/listedArticles/index.html +++ b/prototype/src/main/resources/templates/intern/listedArticles/index.html @@ -53,7 +53,7 @@ - +