Search in sources :

Example 1 with CallRequestController

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;
}
Also used : Call(okhttp3.Call) InputStream(java.io.InputStream) Request(okhttp3.Request) Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) IOException(java.io.IOException) CallRequestController(org.thoughtcrime.securesms.net.CallRequestController) Response(okhttp3.Response) Bitmap(android.graphics.Bitmap) NonNull(androidx.annotation.NonNull)

Example 2 with CallRequestController

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);
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Optional(org.whispersystems.libsignal.util.guava.Optional) IOException(java.io.IOException) OpenGraph(org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph) CallRequestController(org.thoughtcrime.securesms.net.CallRequestController) NonNull(androidx.annotation.NonNull)

Example 3 with CallRequestController

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;
}
Also used : Call(okhttp3.Call) InputStream(java.io.InputStream) Request(okhttp3.Request) Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) IOException(java.io.IOException) CallRequestController(org.thoughtcrime.securesms.net.CallRequestController) Response(okhttp3.Response) Bitmap(android.graphics.Bitmap) NonNull(androidx.annotation.NonNull)

Example 4 with CallRequestController

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);
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Optional(org.whispersystems.libsignal.util.guava.Optional) IOException(java.io.IOException) OpenGraph(org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph) CallRequestController(org.thoughtcrime.securesms.net.CallRequestController) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)4 IOException (java.io.IOException)4 Call (okhttp3.Call)4 Response (okhttp3.Response)4 CallRequestController (org.thoughtcrime.securesms.net.CallRequestController)4 Bitmap (android.graphics.Bitmap)2 InputStream (java.io.InputStream)2 Request (okhttp3.Request)2 Attachment (org.thoughtcrime.securesms.attachments.Attachment)2 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)2 OpenGraph (org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph)2 Optional (org.whispersystems.libsignal.util.guava.Optional)2