Add link preview support for raw images.

This commit is contained in:
Jordan Rose
2025-06-30 07:35:05 -07:00
committed by Greyson Parrelli
parent 3b0878f493
commit 1ce6aacec6

View File

@@ -64,6 +64,7 @@ import org.whispersystems.signalservice.api.util.OptionalUtil;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
@@ -190,6 +191,17 @@ public class LinkPreviewRepository {
return;
}
if (MediaUtil.isImageType(response.header("Content-Type"))) {
// We've been linked directly to an image.
okhttp3.HttpUrl imageUrl = response.request().url();
// The best we can do for a title is the filename in the URL itself,
// but that's no worse than the body of the message.
List<String> requestedUrlPathSegments = imageUrl.pathSegments();
String filename = requestedUrlPathSegments.get(requestedUrlPathSegments.size() - 1);
callback.accept(new Metadata(Optional.of(filename), Optional.empty(), 0, Optional.of(imageUrl.toString())));
return;
}
String body;
try {
body = OkHttpUtil.readAsString(response.body(), FAILSAFE_MAX_TEXT_SIZE);