Commit 25d7d4b6 by Patryk Czarnik

Obsługa zdjęć

parent c38317d2
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();
}
}
public void writeBytes(int productId, byte[] bytes) {
try {
Path path = getPath(productId);
Files.write(path, bytes);
} catch (Exception e) {
e.printStackTrace();
}
}
private Path getPath(int productId) {
String fileName = productId + EXT;
return Paths.get(photoDir, fileName);
}
}
......@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import sklep.model.Product;
import sklep.photo.PhotoUtil;
import sklep.repository.ProductRepository;
......@@ -31,9 +32,11 @@ import sklep.repository.ProductRepository;
@RequestMapping("/products")
public class ProductController {
private ProductRepository productRepository;
private PhotoUtil photoUtil;
public ProductController(ProductRepository productRepository) {
public ProductController(ProductRepository productRepository, PhotoUtil photoUtil) {
this.productRepository = productRepository;
this.photoUtil = photoUtil;
}
@GetMapping
......@@ -46,7 +49,6 @@ public class ProductController {
return productRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
// Przykład metody, która "wchodzi w szczegóły rekordu".
// To nie jest częsta praktyka, ale pokazuję to dla lepszego zrozumienia idei REST - adresy URL odpowiadają logicznej strukturze danych.
@GetMapping("/{id}/price")
......@@ -137,4 +139,18 @@ public class ProductController {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Brak produktu nr " + productId);
}
}
/* Większość komunikacji w usługach REST odbywa się w formacie JSON,
* ale czasami używany jest też format XML,
* a dla niektórych danych stosujemy bezpośrednio jakiś format specjalny, np. PNG, JPG dla obrazów, PDF dla wydruków itp.
*/
@GetMapping(path="/{id}/photo", produces="image/jpeg")
public byte[] getPhoto(@PathVariable("id") int productId) {
return photoUtil.readBytes(productId);
}
@PutMapping(path="/{id}/photo", consumes="image/jpeg")
public void uploadPhoto(@PathVariable("id") int productId, @RequestBody byte[] bytes) {
photoUtil.writeBytes(productId, bytes);
}
}
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