Merge branch 'listImages' into 'main'

besoin 4 done sauf test pas compris

See merge request pdl-l3/teams/2023/v1/v1d!9
This commit is contained in:
Kea Horvath committed 2023-03-13 19:45:23 +01:00
commit 42068a9b2d
2 files changed
+55 -9

No files matched your search

+46 -3
View File
@@ -1,15 +1,38 @@
package pdl.backend;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import org.springframework.http.MediaType;
public class Image {
private static Long count = Long.valueOf(0);
private Long id;
private final Long id;
private String name;
private byte[] data;
private final byte[] data;
private final MediaType mediaType;
private final int width;
private final int height;
private final int nbColors;
public Image(final String name, final byte[] data) {
public Image(final String name, final byte[] data) throws IOException {
id = count++;
this.name = name;
this.data = data;
this.mediaType = MediaType.valueOf(URLConnection.guessContentTypeFromName(name));
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(data));
this.width = bufImg.getWidth();
this.height = bufImg.getHeight();
this.nbColors = bufImg.getColorModel().getNumComponents();
}
public MediaType getMediaType() {
return mediaType;
}
public long getId() {
@@ -27,4 +50,24 @@ public class Image {
public byte[] getData() {
return data;
}
public static Long getCount() {
return count;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getNbColors() {
return nbColors;
}
public String getSize() {
return this.getWidth() + "*" + this.getHeight() + "*" + this.getNbColors();
}
}
@@ -15,9 +15,10 @@ import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@@ -37,7 +38,7 @@ public class ImageController {
this.imageDao = imageDao;
}
@RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id);
@@ -49,7 +50,7 @@ public class ImageController {
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
}
@RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE)
@DeleteMapping(value = "/images/{id}")
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id);
@@ -61,7 +62,7 @@ public class ImageController {
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
}
@RequestMapping(value = "/images", method = RequestMethod.POST)
@PostMapping(value = "/images")
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
String contentType = file.getContentType();
@@ -77,7 +78,7 @@ public class ImageController {
return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
}
@RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/images", produces = "application/json")
@ResponseBody
public ArrayNode getImageList() {
List<Image> images = imageDao.retrieveAll();
@@ -86,6 +87,8 @@ public class ImageController {
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("id", image.getId());
objectNode.put("name", image.getName());
objectNode.put("size", image.getSize());
objectNode.put("type", image.getMediaType().toString());
nodes.add(objectNode);
}
return nodes;