Commit 763ad912 by Patryk Czarnik

PhotoUtil jako Component i @Value

parent 5a0ee948
......@@ -21,6 +21,9 @@ public class ProductController {
@Autowired
private ProductRepository productRepository;
@Autowired
private PhotoUtil photoUtil;
@GetMapping
public String readAll(Model model) {
List<Product> products = productRepository.findAll();
......@@ -115,6 +118,6 @@ public class ProductController {
@GetMapping(path="/{id}/photo", produces="image/jpeg")
@ResponseBody
public byte[] getPhoto(@PathVariable("id") Integer productId) {
return PhotoUtil.readBytes(productId);
return photoUtil.readBytes(productId);
}
}
package sklep.util;
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;
......@@ -11,12 +13,14 @@ 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";
public static File getFile(int productId) {
@Value("${alx.photo_dir}")
private String dir;
public File getFile(int productId) {
Path path = getPath(productId);
File file = path.toFile();
if(file.exists()) {
......@@ -26,7 +30,7 @@ public class PhotoUtil {
}
}
public static byte[] readBytes(int productId) {
public byte[] readBytes(int productId) {
Path path = getPath(productId);
try {
return Files.readAllBytes(path);
......@@ -35,7 +39,7 @@ public class PhotoUtil {
}
}
public static void writeStream(int productId, InputStream inputStream) {
public void writeStream(int productId, InputStream inputStream) {
try {
Path path = getPath(productId);
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
......@@ -45,9 +49,7 @@ public class PhotoUtil {
}
}
private static Path getPath(int productId) {
//String dir = DBSettings.load().getProperty("photo_dir");
String dir = "/home/patryk/sklep/foto";
private Path getPath(int productId) {
String fileName = productId + EXT;
return Paths.get(dir, fileName);
}
......
......@@ -4,3 +4,5 @@ spring.datasource.password=abc123
spring.mvc.view.prefix=/WEB-INF/templates/
spring.mvc.view.suffix=.jsp
alx.photo_dir=/home/patryk/sklep/foto
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