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

No files matched your search

@@ -100,7 +100,7 @@ public class ImageController {
|| (!contentType.equals(MediaType.IMAGE_JPEG_VALUE) && !contentType.equals(MediaType.IMAGE_PNG_VALUE)))
return JSONError("Content type " + contentType + "is not supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
try {
Image newImg = new Image(file.getOriginalFilename(), file.getBytes());
Image newImg = new Image(file.getOriginalFilename(), file.getBytes(), null);
imageDao.create(newImg);
URI newURI = ServletUriComponentsBuilder.fromCurrentRequest().path("/" + newImg.getId()).build().toUri();
return ResponseEntity.created(newURI).build();
@@ -165,8 +165,7 @@ public class ImageController {
* l'algorithme à utiliser ainsi que ses paramètres
* @return Une ResponseEntity
*/
@GetMapping(value = "/images/{id}", produces = { MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE,
"application/json" })
@GetMapping(value = "/images/{id}", produces = { "application/json" })
public ResponseEntity<?> getImage(@PathVariable("id") long id,
@RequestParam(required = false) Map<String, String> parameters) {
@@ -210,6 +209,7 @@ public class ImageController {
}
try {
a.apply(input);
} catch (Exception e) {
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);
}
byte[] imageData = baos.toByteArray();
InputStream inputStream = new ByteArrayInputStream(
imageData);
return ResponseEntity.ok().contentType(image.getMediaType()).body(new InputStreamResource(inputStream));
String name = image.getName() + "_filtered";
Image newImg;
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 height;
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++;
this.name = name;
this.data = data;
@@ -28,7 +30,8 @@ public class Image {
this.width = bufImg.getWidth();
this.height = bufImg.getHeight();
this.nbColors = bufImg.getColorModel().getNumComponents();
this.filtered = false;
this.parent = parent;
}
public MediaType getMediaType() {
@@ -40,7 +43,10 @@ public class Image {
}
public String getName() {
return name;
if (filtered)
return name + "_filtered";
else
return name;
}
public void setName(final String name) {
@@ -70,4 +76,16 @@ public class Image {
public String getSize() {
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;
try {
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);
} catch (final IOException e) {
e.printStackTrace();