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 7a7e7fe..1d54302 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 @@ -1,19 +1,30 @@ package org.hso.ecommerce.controller.intern; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.Optional; +import javax.imageio.ImageIO; import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.entities.shop.Category; +import org.hso.ecommerce.entities.shop.Image; import org.hso.ecommerce.entities.supplier.ArticleOffer; import org.hso.ecommerce.repos.shop.ArticleRepository; import org.hso.ecommerce.repos.shop.CategoryRepository; +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.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 @@ -31,6 +42,9 @@ public class InternArticleController { @Autowired private final OffersRepository offersRepository = null; + @Autowired + private final ImageRepository imageRepository = null; + @GetMapping("/") public String internListedArticles(Model model) { @@ -69,12 +83,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(); @@ -91,6 +104,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 @@ -114,15 +128,67 @@ public class InternArticleController { tmpArticle.shouldReorder = false; tmpArticle.title = offeredArticle.title; tmpArticle.warehouseUnitsPerSlot = 1; - tmpArticle.image = articleRepository.findAll().get(0).image; // TODO set any static default image + setDefaultImage(tmpArticle); tmpArticle.related = offeredArticle; - articleRepository.save(tmpArticle); // save new article // return to edit article page return new RedirectView("../" + articleRepository.findArticleIDByRelatedID(offeredArticleID).get()); } + private void setDefaultImage(Article article) { + String defaultImagePath = "./data/img/no_product_img.jpg"; // path + name of default img + Optional imageID = imageRepository.findImageIDByPath(defaultImagePath); // get default img + + if (imageID.isPresent()) { + // default img is in DB + article.image = imageRepository.findImageById(imageID.get()); // set default img to new article + } else { + // default img is not in DB + File tmpFile = new File(defaultImagePath); + // test if default img file exits + if (!tmpFile.exists()) { + // 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 + } catch (IOException e) { + e.printStackTrace(); + } + } + 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 + } + } + } + public static class UImodelArticles { public String imgPath; 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 new file mode 100644 index 0000000..d0f3997 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ImageRepository.java @@ -0,0 +1,20 @@ +package org.hso.ecommerce.repos.shop; + +import org.hso.ecommerce.entities.shop.Image; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface ImageRepository extends JpaRepository { + + @Query("SELECT i.id FROM Image i WHERE i.path = :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/static/img/product-1.jpg b/prototype/src/main/resources/static/img/product-1.jpg deleted file mode 100644 index 8ea7598..0000000 Binary files a/prototype/src/main/resources/static/img/product-1.jpg and /dev/null differ diff --git a/prototype/src/main/resources/static/img/product-2.jpg b/prototype/src/main/resources/static/img/product-2.jpg deleted file mode 100644 index 427b2f0..0000000 Binary files a/prototype/src/main/resources/static/img/product-2.jpg and /dev/null differ diff --git a/prototype/src/main/resources/static/img/product-3.jpg b/prototype/src/main/resources/static/img/product-3.jpg deleted file mode 100644 index dbc1b2e..0000000 Binary files a/prototype/src/main/resources/static/img/product-3.jpg and /dev/null differ diff --git a/prototype/src/main/resources/static/img/product-4.jpg b/prototype/src/main/resources/static/img/product-4.jpg deleted file mode 100644 index 8f2d857..0000000 Binary files a/prototype/src/main/resources/static/img/product-4.jpg and /dev/null differ diff --git a/prototype/src/main/resources/static/img/product-5.jpg b/prototype/src/main/resources/static/img/product-5.jpg deleted file mode 100644 index 5e0a406..0000000 Binary files a/prototype/src/main/resources/static/img/product-5.jpg and /dev/null differ diff --git a/prototype/src/main/resources/static/img/product-6.jpg b/prototype/src/main/resources/static/img/product-6.jpg deleted file mode 100644 index 05d78c0..0000000 Binary files a/prototype/src/main/resources/static/img/product-6.jpg and /dev/null differ diff --git a/prototype/src/main/resources/static/img/product-7.jpg b/prototype/src/main/resources/static/img/product-7.jpg deleted file mode 100644 index 220140c..0000000 Binary files a/prototype/src/main/resources/static/img/product-7.jpg and /dev/null differ diff --git a/prototype/src/main/resources/static/img/product-8.jpg b/prototype/src/main/resources/static/img/product-8.jpg deleted file mode 100644 index cdb829d..0000000 Binary files a/prototype/src/main/resources/static/img/product-8.jpg and /dev/null differ 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 @@ - +