Commit a413d668 by Patryk Czarnik

Zdjęcia

parent f6a72821
...@@ -9,9 +9,11 @@ import org.springframework.stereotype.Controller; ...@@ -9,9 +9,11 @@ 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.ResponseBody;
import sklep.model.Product; import sklep.model.Product;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
import sklep.util.PhotoUtil;
@Controller @Controller
public class ProductController { public class ProductController {
...@@ -66,4 +68,13 @@ public class ProductController { ...@@ -66,4 +68,13 @@ public class ProductController {
model.addAttribute("products", products); model.addAttribute("products", products);
return "wyszukiwarka2"; return "wyszukiwarka2";
} }
@GetMapping(path="/products/{id}/photo", produces="image/jpeg")
@ResponseBody
public byte[] getPhoto(@PathVariable("id") Integer productId) {
return photoUtil.readBytes(productId);
}
@Autowired
private PhotoUtil photoUtil;
} }
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;
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);
}
}
...@@ -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
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