feat(Sync for Reddit): Add `Fix /s/ links` patch

This commit is contained in:
oSumAtrIX 2024-02-09 03:36:04 +01:00
parent 85504f6f65
commit a8c82ad27b
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package app.revanced.integrations.syncforreddit;
import android.os.StrictMode;
import app.revanced.integrations.shared.Logger;
import java.net.HttpURLConnection;
import java.net.URL;
public final class FixSLinksPatch {
public static String resolveSLink(String link) {
if (link.matches(".*reddit\\.com/r/[^/]+/s/[^/]+")) {
Logger.printInfo(() -> "Resolving " + link);
try {
URL url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("HEAD");
// Disable strict mode in order to allow network access on the main thread.
// This is not ideal, but it's the easiest solution for now.
final var currentPolicy = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
connection.connect();
String location = connection.getHeaderField("location");
connection.disconnect();
// Restore the original strict mode policy.
StrictMode.setThreadPolicy(currentPolicy);
Logger.printInfo(() -> "Resolved " + link + " -> " + location);
return location;
} catch (Exception e) {
Logger.printException(() -> "Failed to resolve " + link, e);
}
}
return link;
}
}