some gzip refactoring

This commit is contained in:
gdlbo
2024-02-21 17:06:20 +03:00
parent e5b5467b27
commit 0194f5ede1
7 changed files with 42 additions and 155 deletions
@@ -18,6 +18,7 @@ import ru.vtosters.lite.downloaders.messages.items.MiniUser;
import ru.vtosters.lite.proxy.ProxyUtils;
import ru.vtosters.lite.utils.AccountManagerUtils;
import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.sponsorpost.utils.GzipDecompressor;
import java.io.File;
import java.io.FileOutputStream;
@@ -104,6 +105,7 @@ public class MessagesDownloader {
AccountManagerUtils.getUserToken());
var req = new Request.a()
.b(url)
.a("Accept-Encoding", "gzip")
.a();
VtOkHttpClient.getInstance().a(req).a(new Callback() {
@Override
@@ -114,7 +116,7 @@ public class MessagesDownloader {
@Override
public void a(Call call, Response response) {
try {
JSONObject obj = new JSONObject(response.a().g()).getJSONObject("response");
JSONObject obj = new JSONObject(GzipDecompressor.decompressResponse(response)).getJSONObject("response");
SparseArray<MiniUser> users = new SparseArray<>();
parseUsers(users, obj.optJSONArray("profiles"), false);
@@ -48,17 +48,8 @@ public class OTAUtils {
});
}
void setData(Response response, boolean isManualCheck) throws IOException, JSONException {
String encoding = response.a("Content-Encoding");
String payload;
if (encoding != null && encoding.equals("gzip")) {
payload = GzipDecompressor.decompress(response.a().b());
} else {
payload = response.a().g();
}
mReleaseJson = new JSONObject(payload);
void setData(Response response, boolean isManualCheck) throws JSONException, IOException {
mReleaseJson = new JSONObject(GzipDecompressor.decompressResponse(response));
String tag = mReleaseJson.getString("tag_name");
Request commit = new Request.a()
@@ -74,22 +65,15 @@ public class OTAUtils {
}
@Override
public void a(Call call, Response response) throws IOException {
public void a(Call call, Response response) {
try {
String payload;
if (encoding != null && encoding.equals("gzip")) {
payload = GzipDecompressor.decompress(response.a().b());
} else {
payload = response.a().g();
}
mCommitJson = new JSONObject(payload);
mCommitJson = new JSONObject(GzipDecompressor.decompressResponse(response));
mCommitSHA = mCommitJson.getJSONObject("object").getString("sha");
if (isNewVersion())
mListener.onUpdateApplied(isManualCheck);
else
mListener.onUpdateLatest(isManualCheck);
} catch (NullPointerException | JSONException e) {
} catch (NullPointerException | JSONException | IOException e) {
e.printStackTrace();
mListener.onUpdateError();
}
@@ -3,6 +3,7 @@ package ru.vtosters.lite.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.vk.core.network.Network;
import com.vk.navigation.NavigatorKeys;
import okhttp3.*;
import org.json.JSONArray;
@@ -15,8 +16,6 @@ import ru.vtosters.sponsorpost.utils.GzipDecompressor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;
import static ru.vtosters.hooks.other.Preferences.getBoolValue;
@@ -44,24 +43,15 @@ public class VTVerifications {
Request request = new Request.a()
.b("https://vtosters.app/vktoaster/getGalo4kiBatch")
.a(Headers.a("User-Agent", Network.l.c().a(), "Content-Type", "application/x-www-form-urlencoded; charset=utf-8", "Accept-Encoding", "gzip"))
.a(RequestBody.a(MediaType.b("application/json; charset=UTF-8"), "{\"types\":[0,228,404]}"))
.a("Accept-Encoding", "gzip")
.a();
sClient.a(request).a(new Callback() {
@Override
public void a(Call call, Response response) {
try {
String encoding = response.a("Content-Encoding");
String payload;
if (encoding != null && encoding.equals("gzip")) {
payload = GzipDecompressor.decompress(response.a().b());
} else {
// Retrieve the response body directly
payload = response.a().g();
}
String payload = GzipDecompressor.decompressResponse(response);
parseJson(payload);
prefs.edit()
.putString("ids", payload)
@@ -33,12 +33,7 @@ public class VotesService {
.a();
try (Response response = client.a(request).execute()) {
if (response.h()) {
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
return parseJSON(new JSONObject(GzipDecompressor.decompress(response.a().b())));
} else {
return parseJSON(new JSONObject(response.a().g()));
}
return parseJSON(new JSONObject(GzipDecompressor.decompressResponse(response)));
} else {
throw new RuntimeException("Failed to get posts: " + response.l());
}
@@ -81,12 +76,7 @@ public class VotesService {
.a();
try (Response response = client.a(request).execute()) {
if (response.h()) {
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
return parseJSON(new JSONObject(GzipDecompressor.decompress(response.a().b())));
} else {
return parseJSON(new JSONObject(response.a().g()));
}
return parseJSON(new JSONObject(GzipDecompressor.decompressResponse(response)));
} else {
throw new RuntimeException("Failed to get postIdsByOwnerId: " + response.l());
}
@@ -104,12 +94,7 @@ public class VotesService {
.a();
try (Response response = client.a(request).execute()) {
if (response.h()) {
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
return parseJSON(new JSONObject(GzipDecompressor.decompress(response.a().b())));
} else {
return parseJSON(new JSONObject(response.a().g()));
}
return parseJSON(new JSONObject(GzipDecompressor.decompressResponse(response)));
} else {
throw new RuntimeException("Failed to get ownersIds: " + response.l());
}
@@ -126,12 +111,7 @@ public class VotesService {
.a("Content-Type", "application/json")
.a();
try (Response response = client.a(request).execute()) {
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
return new JSONObject(GzipDecompressor.decompress(response.a().b()));
} else {
return new JSONObject(response.a().g());
}
return new JSONObject(GzipDecompressor.decompressResponse(response));
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
}
@@ -146,15 +126,8 @@ public class VotesService {
.a("Signature", Native.sig())
.a();
try (Response response = client.a(request).execute()) {
String encoding = response.a("Content-Encoding");
String userToken;
if (encoding != null && encoding.equals("gzip")) {
userToken = new JSONObject(GzipDecompressor.decompress(response.a().b())).getString("token");
VotesPreferences.setUserToken(userToken);
} else {
userToken = new JSONObject(response.a().g()).getString("token");
VotesPreferences.setUserToken(userToken);
}
String userToken = new JSONObject(GzipDecompressor.decompressResponse(response)).getString("token");
VotesPreferences.setUserToken(userToken);
return userToken;
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
@@ -169,14 +142,7 @@ public class VotesService {
.a();
try (Response response = client.a(request).execute()) {
if (response.h()) {
String encoding = response.a("Content-Encoding");
String resp;
if (encoding != null && encoding.equals("gzip")) {
resp = GzipDecompressor.decompress(response.a().b());
} else {
resp = response.a().g();
}
return Boolean.parseBoolean(resp);
return Boolean.parseBoolean(GzipDecompressor.decompressResponse(response));
} else {
throw new RuntimeException("Failed to check post for ad status: " + response.l());
}
@@ -42,20 +42,7 @@ public class FilterService {
try (Response response = client.a(request).execute()) {
// Check if the response is successful
if (response.h()) {
// Determine the response encoding
String encoding = response.a("Content-Encoding");
String resp;
if (encoding != null && encoding.equals("gzip")) {
// Decompress the response body
resp = GzipDecompressor.decompress(response.a().b());
} else {
// Retrieve the response body directly
resp = response.a().g();
}
// Parse the response body as a JSONArray and extract filter objects
return parseJSON(new JSONArray(resp));
return parseJSON(new JSONArray(GzipDecompressor.decompressResponse(response)));
} else {
if (!FiltersPreferences.getAllFilterIds().isEmpty()) {
return FiltersPreferences.getAllDownloadedFilters(); // check for offline server to get local filters
@@ -75,11 +62,12 @@ public class FilterService {
public static Set<String> downloadFilter(String link) {
Request request = new Request.a()
.b(link)
.a("Accept-Encoding", "gzip")
.a();
try (Response response = client.a(request).execute()) {
if (response.h()) {
return stringToSet(response.a().g());
return stringToSet(GzipDecompressor.decompressResponse(response));
} else {
throw new RuntimeException("Failed to download filter: " + response.l());
}
@@ -39,17 +39,7 @@ public class PostService {
try (Response response = client.a(request).execute()) {
// Check if the response is successful
if (response.h()) {
// Determine the response encoding
String encoding = response.a("Content-Encoding");
// Handle GZIP-compressed responses
if (encoding != null && encoding.equals("gzip")) {
// Decompress the response body and parse JSON data
return parseJSONPosts(new JSONArray(GzipDecompressor.decompress(response.a().b())));
} else {
// Parse JSON data from the response body
return parseJSONPosts(new JSONArray(response.a().g()));
}
return parseJSONPosts(new JSONArray(GzipDecompressor.decompressResponse(response)));
} else {
// Handle error response
throw new RuntimeException("Failed to get posts: " + response.l());
@@ -78,20 +68,7 @@ public class PostService {
try (Response response = client.a(request).execute()) {
// Check if the response is successful
if (response.h()) {
// Determine the response encoding
String encoding = response.a("Content-Encoding");
String resp;
if (encoding != null && encoding.equals("gzip")) {
// Decompress the response body
resp = GzipDecompressor.decompress(response.a().b());
} else {
// Retrieve the response body directly
resp = response.a().g();
}
// Extract the boolean value indicating whether the post is an ad
return Boolean.parseBoolean(resp);
return Boolean.parseBoolean(GzipDecompressor.decompressResponse(response));
} else {
// Handle error response
throw new RuntimeException("Failed to check post for ad status: " + response.l());
@@ -121,17 +98,7 @@ public class PostService {
try (Response response = client.a(request).execute()) {
// Check if the response is successful
if (response.h()) {
// Determine the response encoding
String encoding = response.a("Content-Encoding");
// Handle GZIP-compressed responses
if (encoding != null && encoding.equals("gzip")) {
// Decompress the response body and extract the first element as a list of post IDs
return new ArrayList<>(Collections.singleton(GzipDecompressor.decompress(response.a().b())));
} else {
// Extract the first element as a list of post IDs
return new ArrayList<>(Collections.singleton(response.a().g()));
}
return new ArrayList<>(Collections.singleton(GzipDecompressor.decompressResponse(response)));
} else {
// Handle error response
throw new RuntimeException("Failed to get posts IDs: " + response.l());
@@ -161,20 +128,7 @@ public class PostService {
try (Response response = client.a(request).execute()) {
// Check if the response is successful
if (response.h()) {
// Determine the response encoding
String encoding = response.a("Content-Encoding");
String resp;
if (encoding != null && encoding.equals("gzip")) {
// Decompress the response body
resp = GzipDecompressor.decompress(response.a().b());
} else {
// Retrieve the response body directly
resp = response.a().g();
}
// Parse the response body as a JSONArray and extract post IDs
return parseJSONIds(new JSONArray(resp));
return parseJSONIds(new JSONArray(GzipDecompressor.decompressResponse(response)));
} else {
// Handle error response
throw new RuntimeException("Failed to get postIdsByOwnerId: " + response.l());
@@ -201,20 +155,7 @@ public class PostService {
try (Response response = client.a(request).execute()) {
// Check if the response is successful
if (response.h()) {
// Determine the response encoding
String encoding = response.a("Content-Encoding");
String resp;
if (encoding != null && encoding.equals("gzip")) {
// Decompress the response body
resp = GzipDecompressor.decompress(response.a().b());
} else {
// Retrieve the response body directly
resp = response.a().g();
}
// Parse the response body as a JSONArray and extract owner IDs
return parseJSONIds(new JSONArray(resp));
return parseJSONIds(new JSONArray(GzipDecompressor.decompressResponse(response)));
} else {
// Handle error response
throw new RuntimeException("Failed to get ownersIds: " + response.l());
@@ -1,5 +1,8 @@
package ru.vtosters.sponsorpost.utils;
import android.util.Log;
import okhttp3.Response;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -26,4 +29,17 @@ public class GzipDecompressor {
throw new RuntimeException(e);
}
}
public static String decompressResponse(Response response) throws IOException {
String encoding = response.a("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
// Decompress the response body
Log.d("GZIP", "true, link " + response.o().g());
return GzipDecompressor.decompress(response.a().b());
} else {
// Retrieve the response body directly
Log.d("GZIP", "false, link " + response.o().g());
return response.a().g();
}
}
}