Skip to content

Commit

Permalink
1.1.3.2 - Add lib to lower image resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
FunixG committed Nov 11, 2024
1 parent fdd4d0e commit 81e152b
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Lien du repository Maven : [FunixProductions - Core spring package](https://mvn.
<parent>
<groupId>com.funixproductions.core</groupId>
<artifactId>funixproductions-core</artifactId>
<version>(maven version)</version>
<version>1.1.3.2</version>
</parent>

<repository>
Expand Down
2 changes: 1 addition & 1 deletion crud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.funixproductions.core</groupId>
<artifactId>funixproductions-core</artifactId>
<version>1.1.3.1</version>
<version>1.1.3.2</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion exceptions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.funixproductions.core</groupId>
<artifactId>funixproductions-core</artifactId>
<version>1.1.3.1</version>
<version>1.1.3.2</version>
</parent>

<groupId>com.funixproductions.core.exceptions</groupId>
Expand Down
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>com.funixproductions.core</groupId>
<artifactId>funixproductions-core</artifactId>
<version>1.1.3.1</version>
<version>1.1.3.2</version>
<name>funixproductions-core</name>
<description>funixproductions-core for all company projects</description>
<url>https://github.com/FunixProductions/spring-core</url>
Expand All @@ -29,7 +29,7 @@

<properties>
<java.version>21</java.version>
<com.funixproductions.core.version>1.1.3.1</com.funixproductions.core.version>
<com.funixproductions.core.version>1.1.3.2</com.funixproductions.core.version>

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<com.google.guava.version>33.3.1-jre</com.google.guava.version>
Expand Down Expand Up @@ -58,6 +58,8 @@
<org.apache.pdfbox.version>3.0.3</org.apache.pdfbox.version>
<!-- https://mvnrepository.com/artifact/io.sentry/sentry-spring-boot-starter-jakarta -->
<io.sentry.version>7.16.0</io.sentry.version>
<!-- https://mvnrepository.com/artifact/org.imgscalr/imgscalr-lib -->
<org.imgscalr-lib.version>4.2</org.imgscalr-lib.version>
</properties>

<licenses>
Expand Down
9 changes: 8 additions & 1 deletion storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.funixproductions.core</groupId>
<artifactId>funixproductions-core</artifactId>
<version>1.1.3.1</version>
<version>1.1.3.2</version>
</parent>

<groupId>com.funixproductions.core.files</groupId>
Expand All @@ -26,6 +26,13 @@
<version>${com.funixproductions.core.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.imgscalr/imgscalr-lib -->
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>${org.imgscalr-lib.version}</version>
</dependency>

<dependency>
<groupId>com.funixproductions.core.test</groupId>
<artifactId>test-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
import jakarta.transaction.Transactional;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.imgscalr.Scalr;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -146,7 +150,7 @@ public void beforeDeletingEntity(@NonNull Iterable<ENTITY> filesEnt) {
}
}

@Scheduled(fixedRate = 3, timeUnit = TimeUnit.MINUTES)
@Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
public void checkCache() {
final List<String> uuids = new ArrayList<>(resourceCache.asMap().keySet());

Expand All @@ -159,6 +163,22 @@ public void checkCache() {
}
}

public static byte[] createThumbnailFromImage(@NonNull MultipartFile originalFile, int width) {
if (width <= 0) {
throw new ApiBadRequestException("La redimention d'une image ne peut pas se faire avec une largeur de 0 ou moins.");
}

try (final ByteArrayOutputStream thumbOutput = new ByteArrayOutputStream()) {
BufferedImage img = ImageIO.read(originalFile.getInputStream());
BufferedImage thumbImg = Scalr.resize(img, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, width, Scalr.OP_ANTIALIAS);

ImageIO.write(thumbImg, originalFile.getContentType().split("/")[1], thumbOutput);
return thumbOutput.toByteArray();
} catch (Exception e) {
throw new ApiException("Erreur interne lors de la création d'une image en basse résolution.", e);
}
}

@NonNull
private DTO storeNewFile(MultipartFile multipartFile, DTO fileDto) {
final String originalFileName = multipartFile.getResource().getFilename();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.funixproductions.core.files;

import com.funixproductions.core.TestApp;
import com.funixproductions.core.exceptions.ApiBadRequestException;
import com.funixproductions.core.files.doc.dtos.TestStorageFileDTO;
import com.funixproductions.core.files.services.ApiStorageService;
import com.funixproductions.core.test.beans.JsonHelper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,11 +14,15 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -211,4 +217,39 @@ void testDeleteFile() throws Exception {
.andExpect(status().isNotFound());
}

@Test
void testMakeThumbnailOfImage() throws Exception {
InputStream imageStream = getClass().getResourceAsStream("/test-image.jpeg");

MockMultipartFile testImage = new MockMultipartFile(
"image",
"test-image.jpg",
MediaType.IMAGE_JPEG_VALUE,
imageStream
);

ApiStorageService.createThumbnailFromImage(testImage, 200);
ApiStorageService.createThumbnailFromImage(testImage, 100);

assertThrowsExactly(ApiBadRequestException.class, () -> {
ApiStorageService.createThumbnailFromImage(testImage, 0);
ApiStorageService.createThumbnailFromImage(testImage, -1);
});

final byte[] bytes = ApiStorageService.createThumbnailFromImage(testImage, 30);

File outputFile = new File("./target/test.jpeg");
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
fos.write(bytes);
}

// Verify if the saved file is a readable image
BufferedImage img = ImageIO.read(outputFile);
if (img != null) {
System.out.println("Image written successfully! Width: " + img.getWidth() + ", Height: " + img.getHeight());
} else {
fail("Failed to read the image. The file might not be a valid image format.");
}
}

}
Binary file added storage/src/test/resources/test-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.funixproductions.core</groupId>
<artifactId>funixproductions-core</artifactId>
<version>1.1.3.1</version>
<version>1.1.3.2</version>
</parent>

<groupId>com.funixproductions.core.test</groupId>
Expand Down
2 changes: 1 addition & 1 deletion tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.funixproductions.core</groupId>
<artifactId>funixproductions-core</artifactId>
<version>1.1.3.1</version>
<version>1.1.3.2</version>
</parent>

<groupId>com.funixproductions.core.tools</groupId>
Expand Down

0 comments on commit 81e152b

Please sign in to comment.