use of org.thoughtcrime.securesms.net.CallRequestController in project Signal-Android by WhisperSystems.
the class LinkPreviewRepository method fetchThumbnail.
@NonNull
private RequestController fetchThumbnail(@NonNull String imageUrl, @NonNull Consumer<Optional<Attachment>> callback) {
Call call = client.newCall(new Request.Builder().url(imageUrl).build());
CallRequestController controller = new CallRequestController(call);
SignalExecutors.UNBOUNDED.execute(() -> {
try {
Response response = call.execute();
if (!response.isSuccessful() || response.body() == null) {
return;
}
InputStream bodyStream = response.body().byteStream();
controller.setStream(bodyStream);
byte[] data = OkHttpUtil.readAsBytes(bodyStream, FAILSAFE_MAX_IMAGE_SIZE);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Optional<Attachment> thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.JPEG, MediaUtil.IMAGE_JPEG);
if (bitmap != null)
bitmap.recycle();
callback.accept(thumbnail);
} catch (IOException | IllegalArgumentException e) {
Log.w(TAG, "Exception during link preview image retrieval.", e);
controller.cancel();
callback.accept(Optional.absent());
}
});
return controller;
}
use of org.thoughtcrime.securesms.net.CallRequestController in project Signal-Android by WhisperSystems.
the class LinkPreviewRepository method fetchMetadata.
@NonNull
private RequestController fetchMetadata(@NonNull String url, Consumer<Metadata> callback) {
Call call = client.newCall(new Request.Builder().url(url).cacheControl(NO_CACHE).build());
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.w(TAG, "Request failed.", e);
callback.accept(Metadata.empty());
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (!response.isSuccessful()) {
Log.w(TAG, "Non-successful response. Code: " + response.code());
callback.accept(Metadata.empty());
return;
} else if (response.body() == null) {
Log.w(TAG, "No response body.");
callback.accept(Metadata.empty());
return;
}
String body = OkHttpUtil.readAsString(response.body(), FAILSAFE_MAX_TEXT_SIZE);
OpenGraph openGraph = LinkPreviewUtil.parseOpenGraphFields(body);
Optional<String> title = openGraph.getTitle();
Optional<String> description = openGraph.getDescription();
Optional<String> imageUrl = openGraph.getImageUrl();
long date = openGraph.getDate();
if (imageUrl.isPresent() && !LinkPreviewUtil.isValidPreviewUrl(imageUrl.get())) {
Log.i(TAG, "Image URL was invalid or for a non-whitelisted domain. Skipping.");
imageUrl = Optional.absent();
}
callback.accept(new Metadata(title, description, date, imageUrl));
}
});
return new CallRequestController(call);
}
use of org.thoughtcrime.securesms.net.CallRequestController in project Signal-Android by signalapp.
the class LinkPreviewRepository method fetchThumbnail.
@NonNull
private RequestController fetchThumbnail(@NonNull String imageUrl, @NonNull Consumer<Optional<Attachment>> callback) {
Call call = client.newCall(new Request.Builder().url(imageUrl).build());
CallRequestController controller = new CallRequestController(call);
SignalExecutors.UNBOUNDED.execute(() -> {
try {
Response response = call.execute();
if (!response.isSuccessful() || response.body() == null) {
return;
}
InputStream bodyStream = response.body().byteStream();
controller.setStream(bodyStream);
byte[] data = OkHttpUtil.readAsBytes(bodyStream, FAILSAFE_MAX_IMAGE_SIZE);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Optional<Attachment> thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.JPEG, MediaUtil.IMAGE_JPEG);
if (bitmap != null)
bitmap.recycle();
callback.accept(thumbnail);
} catch (IOException | IllegalArgumentException e) {
Log.w(TAG, "Exception during link preview image retrieval.", e);
controller.cancel();
callback.accept(Optional.absent());
}
});
return controller;
}
use of org.thoughtcrime.securesms.net.CallRequestController in project Signal-Android by signalapp.
the class LinkPreviewRepository method fetchMetadata.
@NonNull
private RequestController fetchMetadata(@NonNull String url, Consumer<Metadata> callback) {
Call call = client.newCall(new Request.Builder().url(url).cacheControl(NO_CACHE).build());
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.w(TAG, "Request failed.", e);
callback.accept(Metadata.empty());
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (!response.isSuccessful()) {
Log.w(TAG, "Non-successful response. Code: " + response.code());
callback.accept(Metadata.empty());
return;
} else if (response.body() == null) {
Log.w(TAG, "No response body.");
callback.accept(Metadata.empty());
return;
}
String body = OkHttpUtil.readAsString(response.body(), FAILSAFE_MAX_TEXT_SIZE);
OpenGraph openGraph = LinkPreviewUtil.parseOpenGraphFields(body);
Optional<String> title = openGraph.getTitle();
Optional<String> description = openGraph.getDescription();
Optional<String> imageUrl = openGraph.getImageUrl();
long date = openGraph.getDate();
if (imageUrl.isPresent() && !LinkPreviewUtil.isValidPreviewUrl(imageUrl.get())) {
Log.i(TAG, "Image URL was invalid or for a non-whitelisted domain. Skipping.");
imageUrl = Optional.absent();
}
callback.accept(new Metadata(title, description, date, imageUrl));
}
});
return new CallRequestController(call);
}
Aggregations