compress uploaded images and cache proxied files
Photos from iPhone (3-10 MB, 12-24 MP) were stored and served as-is, so the feed loaded slowly on first open. Images are now downscaled to 1600px, rotated by EXIF and re-encoded as progressive JPEG on upload. The /api/v1/files proxy now sends Cache-Control instead of Spring Security's no-store, so images are not re-downloaded on every app open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -49,7 +49,7 @@ class FileStorageServicePresignedUrlTest {
|
||||
MinioProperties properties = mock(MinioProperties.class);
|
||||
when(properties.bucket()).thenReturn("bucket");
|
||||
|
||||
service = new FileStorageService(mock(S3Client.class), presigner, properties, redis);
|
||||
service = new FileStorageService(mock(S3Client.class), presigner, properties, redis, new ImageOptimizer());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.krylov.refound.service;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import javax.imageio.ImageIO;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ImageOptimizerTest {
|
||||
|
||||
private ImageOptimizer optimizer;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
optimizer = new ImageOptimizer();
|
||||
optimizer.registerImageIoPlugins();
|
||||
}
|
||||
|
||||
@Test
|
||||
void largePhotoIsDownscaledToJpeg() throws IOException {
|
||||
byte[] source = encode(noise(4032, 3024, BufferedImage.TYPE_INT_RGB), "jpg");
|
||||
|
||||
Optional<ImageOptimizer.OptimizedImage> result = optimizer.optimize(source, "image/jpeg");
|
||||
|
||||
assertThat(result).isPresent();
|
||||
assertThat(result.get().contentType()).isEqualTo("image/jpeg");
|
||||
assertThat(result.get().extension()).isEqualTo(".jpg");
|
||||
assertThat(result.get().content().length).isLessThan(source.length);
|
||||
BufferedImage decoded = decode(result.get().content());
|
||||
assertThat(decoded.getWidth()).isEqualTo(ImageOptimizer.MAX_DIMENSION);
|
||||
assertThat(decoded.getHeight()).isEqualTo(1200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void smallFileIsKeptAsIs() throws IOException {
|
||||
byte[] source = encode(noise(200, 150, BufferedImage.TYPE_INT_RGB), "jpg");
|
||||
assertThat(source.length).isLessThanOrEqualTo((int) ImageOptimizer.SKIP_BELOW_BYTES);
|
||||
|
||||
assertThat(optimizer.optimize(source, "image/jpeg")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void transparentPngStaysPng() throws IOException {
|
||||
byte[] source = encode(noise(2400, 1800, BufferedImage.TYPE_INT_ARGB), "png");
|
||||
|
||||
Optional<ImageOptimizer.OptimizedImage> result = optimizer.optimize(source, "image/png");
|
||||
|
||||
assertThat(result).isPresent();
|
||||
assertThat(result.get().contentType()).isEqualTo("image/png");
|
||||
BufferedImage decoded = decode(result.get().content());
|
||||
assertThat(decoded.getColorModel().hasAlpha()).isTrue();
|
||||
assertThat(Math.max(decoded.getWidth(), decoded.getHeight())).isEqualTo(ImageOptimizer.MAX_DIMENSION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unreadableFileFallsBackToOriginal() {
|
||||
byte[] garbage = new byte[(int) ImageOptimizer.SKIP_BELOW_BYTES + 1];
|
||||
new Random(1).nextBytes(garbage);
|
||||
|
||||
assertThat(optimizer.optimize(garbage, "image/jpeg")).isEmpty();
|
||||
}
|
||||
|
||||
/** Шум плохо сжимается — так файл гарантированно тяжелее порога. */
|
||||
private static BufferedImage noise(int width, int height, int type) {
|
||||
BufferedImage image = new BufferedImage(width, height, type);
|
||||
Random random = new Random(42);
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
image.setRGB(x, y, random.nextInt());
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
private static byte[] encode(BufferedImage image, String format) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, format, out);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static BufferedImage decode(byte[] content) throws IOException {
|
||||
return ImageIO.read(new ByteArrayInputStream(content));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user