That fuck shit the fascists are using
1package org.tm.archive.glide;
2
3import androidx.annotation.NonNull;
4
5import com.bumptech.glide.Priority;
6import com.bumptech.glide.load.DataSource;
7import com.bumptech.glide.load.data.DataFetcher;
8import com.bumptech.glide.load.model.GlideUrl;
9import com.bumptech.glide.util.ContentLengthInputStream;
10
11import org.signal.core.util.logging.Log;
12
13import java.io.IOException;
14import java.io.InputStream;
15import java.util.Map;
16
17import okhttp3.OkHttpClient;
18import okhttp3.Request;
19import okhttp3.Response;
20import okhttp3.ResponseBody;
21
22/**
23 * Fetches an {@link InputStream} using the okhttp library.
24 */
25class OkHttpStreamFetcher implements DataFetcher<InputStream> {
26
27 private static final String TAG = Log.tag(OkHttpStreamFetcher.class);
28
29 private final OkHttpClient client;
30 private final GlideUrl url;
31 private InputStream stream;
32 private ResponseBody responseBody;
33
34 OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) {
35 this.client = client;
36 this.url = url;
37 }
38
39 @Override
40 public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
41 try {
42 Request.Builder requestBuilder = new Request.Builder()
43 .url(url.toStringUrl());
44
45 for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
46 String key = headerEntry.getKey();
47 requestBuilder.addHeader(key, headerEntry.getValue());
48 }
49
50 Request request = requestBuilder.build();
51 Response response = client.newCall(request).execute();
52
53 responseBody = response.body();
54
55 if (!response.isSuccessful()) {
56 throw new IOException("Request failed with code: " + response.code());
57 }
58
59 long contentLength = responseBody.contentLength();
60 stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
61
62 callback.onDataReady(stream);
63 } catch (IOException e) {
64 callback.onLoadFailed(e);
65 }
66 }
67
68 @Override
69 public void cleanup() {
70 if (stream != null) {
71 try {
72 stream.close();
73 } catch (IOException e) {
74 // Ignored
75 }
76 }
77 if (responseBody != null) {
78 responseBody.close();
79 }
80 }
81
82 @Override
83 public void cancel() {
84 // TODO: call cancel on the client when this method is called on a background thread. See #257
85 }
86
87 @Override
88 public @NonNull Class<InputStream> getDataClass() {
89 return InputStream.class;
90 }
91
92 @Override
93 public @NonNull DataSource getDataSource() {
94 return DataSource.REMOTE;
95 }
96}