Skip image probing when dimensions are supplied

Caller-supplied dimensions replace the probed values, so don't buffer and decode the image to measure it.
This commit is contained in:
Dmytro L 2026-09-17 10:23:04 +03:00
parent 74a81b8f96
commit 2e8bf22065
2 changed files with 19 additions and 3 deletions

View File

@ -33,7 +33,9 @@ public class AttachmentUtils {
ResumableUploadSpec resumableUploadSpec
) throws ResumeLocationInvalidException, IOException {
final var uploadTimestamp = System.currentTimeMillis();
final var probedStream = probeImageDimensions(streamDetails);
final var probedStream = dimensions != null
? new ProbedStream(streamDetails.getStream(), dimensions.width(), dimensions.height())
: probeImageDimensions(streamDetails);
return SignalServiceAttachmentStream.newStreamBuilder()
.withStream(probedStream.inputStream())
.withContentType(streamDetails.getContentType())
@ -41,8 +43,8 @@ public class AttachmentUtils {
.withFileName(name.orElse(null))
.withVoiceNote(voiceNote)
.withBlurHash(blurHash)
.withWidth(dimensions != null ? dimensions.width() : probedStream.width())
.withHeight(dimensions != null ? dimensions.height() : probedStream.height())
.withWidth(probedStream.width())
.withHeight(probedStream.height())
.withUploadTimestamp(uploadTimestamp)
.withResumableUploadSpec(resumableUploadSpec)
.withUuid(UUID.randomUUID())

View File

@ -58,6 +58,20 @@ class AttachmentUtilsTest {
assertArrayEquals(bytes, attachment.getInputStream().readAllBytes());
}
@Test
public void createAttachmentStream_skipsProbingWhenDimensionsSupplied() throws Exception {
final var imageBytes = pngBytes(37, 21);
final var stream = new ByteArrayInputStream(imageBytes);
final var details = new StreamDetails(stream, "image/png", imageBytes.length);
final var attachment = AttachmentUtils.createAttachmentStream(details,
Optional.of("meme.png"), false, new AttachmentDimensions(100, 200), null, null);
assertEquals(imageBytes.length, stream.available());
assertEquals(100, attachment.getWidth());
assertEquals(200, attachment.getHeight());
assertArrayEquals(imageBytes, attachment.getInputStream().readAllBytes());
}
private static byte[] pngBytes(final int width, final int height) throws Exception {
final var image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final var out = new ByteArrayOutputStream();