Commit 1afb874a by Patryk Czarnik

obsługa zdjęć w SklepSpring

parent 3f66a77c
...@@ -4,11 +4,9 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -4,11 +4,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sklep.model.Product; import sklep.model.Product;
import sklep.photo.PhotoUtil;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
import java.math.BigDecimal; import java.math.BigDecimal;
...@@ -21,6 +19,9 @@ public class ProductController { ...@@ -21,6 +19,9 @@ public class ProductController {
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
@Autowired
private PhotoUtil photoUtil;
@GetMapping @GetMapping
public String odczytajProdukty(Model model) { public String odczytajProdukty(Model model) {
List<Product> products = productRepository.findAll(Sort.by("id")); List<Product> products = productRepository.findAll(Sort.by("id"));
...@@ -68,4 +69,10 @@ public class ProductController { ...@@ -68,4 +69,10 @@ public class ProductController {
model.addAttribute("products", products); model.addAttribute("products", products);
return "wyszukiwarka2"; return "wyszukiwarka2";
} }
@GetMapping(path="/{id}/photo", produces="image/jpeg")
@ResponseBody
public byte[] getPhoto(@PathVariable int id) {
return photoUtil.readBytes(id);
}
} }
package sklep.photo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
@Component
public class PhotoUtil {
private static final String EXT = ".jpg";
@Value("${alx.photo_dir}")
private String dir;
public File getFile(int productId) {
Path path = getPath(productId);
File file = path.toFile();
if(file.exists()) {
return file;
} else {
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "Brak zdjęcia nr " + productId);
}
}
public byte[] readBytes(int productId) {
Path path = getPath(productId);
try {
return Files.readAllBytes(path);
} catch (IOException e) {
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "Brak zdjęcia nr " + productId);
}
}
public void writeStream(int productId, InputStream inputStream) {
try {
Path path = getPath(productId);
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
// wypisujemy błąd, ale metoda kończy się normalnie
e.printStackTrace();
}
}
private Path getPath(int productId) {
String fileName = productId + EXT;
return Paths.get(dir, fileName);
}
}
...@@ -6,3 +6,4 @@ spring.datasource.url=jdbc:postgresql://localhost/sklep ...@@ -6,3 +6,4 @@ spring.datasource.url=jdbc:postgresql://localhost/sklep
spring.datasource.username=alx spring.datasource.username=alx
spring.datasource.password=abc123 spring.datasource.password=abc123
alx.photo_dir=/home/patryk/sklep/foto
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
<li><a href="/products/szukaj?name=pralka">wyszukiwarka/pralka</a></li> <li><a href="/products/szukaj?name=pralka">wyszukiwarka/pralka</a></li>
<li><a href="/products/new">nowy produkt</a></li> <li><a href="/products/new">nowy produkt</a></li>
<li><a href="/products/1/edit">edycja produktu</a></li> <li><a href="/products/1/edit">edycja produktu</a></li>
<li><a href="/products/2/photo">przykładowe zdjęcie</a></li>
</ul> </ul>
<h2>Edycja klienta</h2> <h2>Edycja klienta</h2>
......
...@@ -11,10 +11,11 @@ ...@@ -11,10 +11,11 @@
<h1>Informacje o produkcie ${product.id}</h1> <h1>Informacje o produkcie ${product.id}</h1>
<div class="product"> <div class="product">
<p>Towar <a href="/products/${product.id}" class="product-name">${product.productName}</a></p> <img class="photo" src="/products/${product.id}/photo" alt=""/>
<p>Cena: <span class="product-price">${product.price}</span></p> <p>Towar <a href="/products/${product.id}" class="product-name">${product.productName}</a></p>
<p>Stawka VAT: <span class="product-price">${product.vat * 100}%</span></p> <p>Cena: <span class="product-price">${product.price}</span></p>
<p class="product-description">${product.description}</p> <p>Stawka VAT: <span class="product-price">${product.vat * 100}%</span></p>
<p class="product-description">${product.description}</p>
</div> </div>
<p><a href="/products">Przejdź do listy produktów</a></p> <p><a href="/products">Przejdź do listy produktów</a></p>
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
<c:forEach var="product" items="${products}"> <c:forEach var="product" items="${products}">
<div class="product"> <div class="product">
<img class="photo" src="/products/${product.id}/photo" alt=""/>
<p>Towar <a href="/products/${product.id}" class="product-name">${product.productName}</a></p> <p>Towar <a href="/products/${product.id}" class="product-name">${product.productName}</a></p>
<p>Cena: <span class="product-price">${product.price}</span></p> <p>Cena: <span class="product-price">${product.price}</span></p>
<p class="product-description">${product.description}</p> <p class="product-description">${product.description}</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment