Commit c166ce1d by Patryk Czarnik

Obsługa zdjęć

parent 6dd615b1
...@@ -14,8 +14,10 @@ import org.springframework.web.bind.annotation.GetMapping; ...@@ -14,8 +14,10 @@ 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.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
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;
import sklep.utils.ExceptionUtils; import sklep.utils.ExceptionUtils;
...@@ -25,6 +27,9 @@ public class ProductController { ...@@ -25,6 +27,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();
...@@ -120,4 +125,10 @@ public class ProductController { ...@@ -120,4 +125,10 @@ public class ProductController {
} }
} }
@GetMapping(path="/{id}/photo", produces="image/jpeg")
@ResponseBody
public byte[] getPhoto(@PathVariable("id") int productId) {
return photoUtil.readBytes(productId);
}
} }
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);
}
}
...@@ -7,3 +7,5 @@ spring.mvc.view.suffix=.jsp ...@@ -7,3 +7,5 @@ spring.mvc.view.suffix=.jsp
# server.port=9090 # server.port=9090
# server.servlet.context-path=/sklep # server.servlet.context-path=/sklep
alx.photo_dir=/home/patryk/sklep/foto
\ No newline at end of file
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Edycja danych klienta</title> <title>Edycja danych klienta</title>
<link rel="stylesheet" type="text/css" href="/styl.css"/> <c:url var="style_url" value="/styl.css"/>
<link rel="stylesheet" type="text/css" href="${style_url}"/>
</head> </head>
<body> <body>
<h1>Edycja danych klienta</h1> <h1>Edycja danych klienta</h1>
......
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