Commit a523da83 by Patryk Czarnik

Obsługa zdjęć

parent cecea189
...@@ -4,14 +4,15 @@ import java.util.List; ...@@ -4,14 +4,15 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.WebServerException;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sklep.model.Product; import sklep.model.Product;
import sklep.photo.PhotoUtil;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
@Controller @Controller
...@@ -20,6 +21,9 @@ public class ProductController { ...@@ -20,6 +21,9 @@ public class ProductController {
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
@Autowired
private PhotoUtil photoUtil;
@GetMapping @GetMapping
public String wszystkieProdukty(Model model) { public String wszystkieProdukty(Model model) {
List<Product> products = productRepository.findAll(); List<Product> products = productRepository.findAll();
...@@ -39,5 +43,10 @@ public class ProductController { ...@@ -39,5 +43,10 @@ public class ProductController {
} }
} }
@GetMapping(path="/{id}/photo", produces="image/jpeg")
@ResponseBody
public byte[] foto(@PathVariable Integer id, Model model) {
return photoUtil.readBytes(id);
}
} }
package sklep.photo;
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;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
@Component
public class PhotoUtil {
@Value("${alx.photo_dir}")
private String photoDir;
private static final String EXT = ".jpg";
public File getFile(int productId) {
Path path = getPath(productId);
File file = path.toFile();
if(file.exists()) {
return file;
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot read photo for product id = " + productId);
}
}
public byte[] readBytes(int productId) {
Path path = getPath(productId);
try {
return Files.readAllBytes(path);
} catch (IOException e) {
// System.err.println(e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot read photo for product id = " + 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(photoDir, fileName);
}
}
...@@ -4,3 +4,5 @@ spring.datasource.password=abc123 ...@@ -4,3 +4,5 @@ spring.datasource.password=abc123
spring.mvc.view.prefix=/WEB-INF/templates/ spring.mvc.view.prefix=/WEB-INF/templates/
spring.mvc.view.suffix=.jsp spring.mvc.view.suffix=.jsp
alx.photo_dir=/home/patryk/sklep/foto
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
<h1>Informacje o produkcie ${product.productId}</h1> <h1>Informacje o produkcie ${product.productId}</h1>
<div class="product"> <div class="product">
<img class="photo" src="/products/${product.productId}/photo" alt=""/>
<p>Towar <a href="/products/${product.productId}" class="product-name">${product.productName}</a></p> <p>Towar <a href="/products/${product.productId}" 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>Stawka VAT: <span class="product-price">${product.vat * 100}%</span></p> <p>Stawka VAT: <span class="product-price">${product.vat * 100}%</span></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.productId}/photo" alt=""/>
<p>Towar <a href="/products/${product.productId}" class="product-name">${product.productName}</a></p> <p>Towar <a href="/products/${product.productId}" 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