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:
commit
42068a9b2d
2 files changed
+55
-9
No files matched your search
@@ -1,15 +1,38 @@
|
|||||||
package pdl.backend;
|
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 {
|
public class Image {
|
||||||
private static Long count = Long.valueOf(0);
|
private static Long count = Long.valueOf(0);
|
||||||
private Long id;
|
private final Long id;
|
||||||
private String name;
|
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++;
|
id = count++;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.data = data;
|
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() {
|
public long getId() {
|
||||||
@@ -27,4 +50,24 @@ public class Image {
|
|||||||
public byte[] getData() {
|
public byte[] getData() {
|
||||||
return data;
|
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.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
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.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -37,7 +38,7 @@ public class ImageController {
|
|||||||
this.imageDao = imageDao;
|
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) {
|
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||||
|
|
||||||
Optional<Image> image = imageDao.retrieve(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);
|
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) {
|
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||||
|
|
||||||
Optional<Image> image = imageDao.retrieve(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);
|
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) {
|
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||||
|
|
||||||
String contentType = file.getContentType();
|
String contentType = file.getContentType();
|
||||||
@@ -77,7 +78,7 @@ public class ImageController {
|
|||||||
return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
|
return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json")
|
@GetMapping(value = "/images", produces = "application/json")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ArrayNode getImageList() {
|
public ArrayNode getImageList() {
|
||||||
List<Image> images = imageDao.retrieveAll();
|
List<Image> images = imageDao.retrieveAll();
|
||||||
@@ -86,6 +87,8 @@ public class ImageController {
|
|||||||
ObjectNode objectNode = mapper.createObjectNode();
|
ObjectNode objectNode = mapper.createObjectNode();
|
||||||
objectNode.put("id", image.getId());
|
objectNode.put("id", image.getId());
|
||||||
objectNode.put("name", image.getName());
|
objectNode.put("name", image.getName());
|
||||||
|
objectNode.put("size", image.getSize());
|
||||||
|
objectNode.put("type", image.getMediaType().toString());
|
||||||
nodes.add(objectNode);
|
nodes.add(objectNode);
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
|
|||||||
Reference in new issue
Block a user