This commit is contained in:
Kea Horvath committed 2023-04-07 16:38:47 +02:00
1 parent ffc9f315ff
commit 8b12042516
3 files changed
+42 -9

No files matched your search

@@ -100,7 +100,7 @@ public class ImageController {
|| (!contentType.equals(MediaType.IMAGE_JPEG_VALUE) && !contentType.equals(MediaType.IMAGE_PNG_VALUE))) || (!contentType.equals(MediaType.IMAGE_JPEG_VALUE) && !contentType.equals(MediaType.IMAGE_PNG_VALUE)))
return JSONError("Content type " + contentType + "is not supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE); return JSONError("Content type " + contentType + "is not supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
try { try {
Image newImg = new Image(file.getOriginalFilename(), file.getBytes()); Image newImg = new Image(file.getOriginalFilename(), file.getBytes(), null);
imageDao.create(newImg); imageDao.create(newImg);
URI newURI = ServletUriComponentsBuilder.fromCurrentRequest().path("/" + newImg.getId()).build().toUri(); URI newURI = ServletUriComponentsBuilder.fromCurrentRequest().path("/" + newImg.getId()).build().toUri();
return ResponseEntity.created(newURI).build(); return ResponseEntity.created(newURI).build();
@@ -165,8 +165,7 @@ public class ImageController {
* l'algorithme à utiliser ainsi que ses paramètres * l'algorithme à utiliser ainsi que ses paramètres
* @return Une ResponseEntity * @return Une ResponseEntity
*/ */
@GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE, @GetMapping(value = "/images/{id}", produces = { "application/json" })
"application/json" })
public ResponseEntity<?> getImage(@PathVariable("id") long id, public ResponseEntity<?> getImage(@PathVariable("id") long id,
@RequestParam(required = false) Map<String, String> parameters) { @RequestParam(required = false) Map<String, String> parameters) {
@@ -210,6 +209,7 @@ public class ImageController {
} }
try { try {
a.apply(input); a.apply(input);
} catch (Exception e) { } catch (Exception e) {
return JSONError("There was an internal server error", HttpStatus.INTERNAL_SERVER_ERROR); return JSONError("There was an internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
} }
@@ -227,8 +227,23 @@ public class ImageController {
return JSONError("There was an internal server error", HttpStatus.INTERNAL_SERVER_ERROR); return JSONError("There was an internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
} }
byte[] imageData = baos.toByteArray(); byte[] imageData = baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream( String name = image.getName() + "_filtered";
imageData); Image newImg;
return ResponseEntity.ok().contentType(image.getMediaType()).body(new InputStreamResource(inputStream)); try {
if (image.isFiltered()) {
newImg = new Image(name, imageData, image.getParent());
imageDao.delete(image);
} else {
newImg = new Image(name, imageData, image);
}
} catch (IOException e) {
return JSONError("There was an internal server error", HttpStatus.INTERNAL_SERVER_ERROR);
}
if (image.isFiltered()) {
imageDao.delete(image);
}
newImg.setFiltered(true);
imageDao.create(newImg);
return ResponseEntity.ok().body(newImg.getId());
} }
} }
@@ -18,8 +18,10 @@ public class Image {
private final int width; private final int width;
private final int height; private final int height;
private final int nbColors; private final int nbColors;
private boolean filtered;
private Image parent;
public Image(final String name, final byte[] data) throws IOException { public Image(final String name, final byte[] data, Image parent) throws IOException {
id = count++; id = count++;
this.name = name; this.name = name;
this.data = data; this.data = data;
@@ -28,7 +30,8 @@ public class Image {
this.width = bufImg.getWidth(); this.width = bufImg.getWidth();
this.height = bufImg.getHeight(); this.height = bufImg.getHeight();
this.nbColors = bufImg.getColorModel().getNumComponents(); this.nbColors = bufImg.getColorModel().getNumComponents();
this.filtered = false;
this.parent = parent;
} }
public MediaType getMediaType() { public MediaType getMediaType() {
@@ -40,6 +43,9 @@ public class Image {
} }
public String getName() { public String getName() {
if (filtered)
return name + "_filtered";
else
return name; return name;
} }
@@ -70,4 +76,16 @@ public class Image {
public String getSize() { public String getSize() {
return this.getWidth() + "*" + this.getHeight() + "*" + this.getNbColors(); return this.getWidth() + "*" + this.getHeight() + "*" + this.getNbColors();
} }
public boolean isFiltered() {
return filtered;
}
public void setFiltered(final boolean filtered) {
this.filtered = filtered;
}
public Image getParent() {
return parent;
}
} }
@@ -58,7 +58,7 @@ public class ImageDao implements Dao<Image> {
byte[] fileContent; byte[] fileContent;
try { try {
fileContent = Files.readAllBytes(file.toPath()); fileContent = Files.readAllBytes(file.toPath());
Image img = new Image(file.getName(), fileContent); Image img = new Image(file.getName(), fileContent, null);
images.put(img.getId(), img); images.put(img.getId(), img);
} catch (final IOException e) { } catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();