mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(android upload): handle transient imgur errors
This commit is contained in:
@@ -40,7 +40,7 @@ public class ImgurLiveUploadTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Live provider diagnostic; may fail when Imgur is unavailable from the current network.")
|
||||
@Ignore("Live provider diagnostic; may fail when Imgur is unavailable or temporarily rejecting anonymous uploads.")
|
||||
public void imgur_liveUpload_fromGeneratedPng_succeeds() throws Exception {
|
||||
Intent launchIntent = new Intent(appContext, MainActivity.class);
|
||||
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
@@ -81,6 +81,7 @@ public class MediaUploadAutomationRunner {
|
||||
static final String STAGE_SUBMIT_CLICKED = "submit_clicked";
|
||||
static final String STAGE_SUCCESS_SELECTOR_MATCHED = "success_selector_matched";
|
||||
static final String STAGE_BLOCKED_DETECTED = "blocked_detected";
|
||||
static final String STAGE_PROVIDER_ERROR = "provider_error";
|
||||
static final String STAGE_INPUT_NOT_FOUND = "input_not_found";
|
||||
static final String STAGE_CHOOSER_NOT_TRIGGERED = "chooser_not_triggered";
|
||||
static final String STAGE_FILE_PAYLOAD_UNAVAILABLE = "file_payload_unavailable";
|
||||
@@ -500,6 +501,7 @@ public class MediaUploadAutomationRunner {
|
||||
|
||||
String successJs = MediaUploadRecipes.getSuccessJs(provider);
|
||||
String blockedJs = MediaUploadRecipes.getBlockedJs(provider);
|
||||
String providerErrorJs = MediaUploadRecipes.getProviderErrorJs(provider);
|
||||
if (successJs == null || blockedJs == null) {
|
||||
finish(
|
||||
new MediaUploadResult(
|
||||
@@ -523,21 +525,53 @@ public class MediaUploadAutomationRunner {
|
||||
null));
|
||||
return;
|
||||
}
|
||||
webView.evaluateJavascript(
|
||||
successJs,
|
||||
url -> {
|
||||
if (finished) return;
|
||||
if (url != null && !"null".equals(url) && url.length() > 2) {
|
||||
if (providerErrorJs != null) {
|
||||
webView.evaluateJavascript(
|
||||
providerErrorJs,
|
||||
providerError -> {
|
||||
if (finished) return;
|
||||
String cleaned =
|
||||
url.replaceAll("^\"|\"$", "").replace("\\u003d", "=");
|
||||
if (cleaned.startsWith("http")) {
|
||||
logStage(STAGE_SUCCESS_SELECTOR_MATCHED);
|
||||
finish(new MediaUploadResult(true, cleaned, null));
|
||||
providerError == null
|
||||
? ""
|
||||
: providerError
|
||||
.replaceAll("^\"|\"$", "")
|
||||
.replace("\\u003d", "=")
|
||||
.replace("\\\"", "\"")
|
||||
.trim();
|
||||
if (!cleaned.isEmpty() && !"null".equals(cleaned)) {
|
||||
finish(
|
||||
new MediaUploadResult(
|
||||
false,
|
||||
null,
|
||||
"Provider error: " + cleaned,
|
||||
STAGE_PROVIDER_ERROR,
|
||||
elapsedMs(),
|
||||
lastMatchedSelector));
|
||||
return;
|
||||
}
|
||||
}
|
||||
schedulePoll();
|
||||
});
|
||||
evaluateSuccess(successJs);
|
||||
});
|
||||
return;
|
||||
}
|
||||
evaluateSuccess(successJs);
|
||||
});
|
||||
}
|
||||
|
||||
private void evaluateSuccess(String successJs) {
|
||||
webView.evaluateJavascript(
|
||||
successJs,
|
||||
url -> {
|
||||
if (finished) return;
|
||||
if (url != null && !"null".equals(url) && url.length() > 2) {
|
||||
String cleaned =
|
||||
url.replaceAll("^\"|\"$", "").replace("\\u003d", "=");
|
||||
if (cleaned.startsWith("http")) {
|
||||
logStage(STAGE_SUCCESS_SELECTOR_MATCHED);
|
||||
finish(new MediaUploadResult(true, cleaned, null));
|
||||
return;
|
||||
}
|
||||
}
|
||||
schedulePoll();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -339,6 +339,21 @@ public final class MediaUploadRecipes {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* JS to detect visible provider-side upload failures that are not CAPTCHA/login blocks.
|
||||
* Returns an error string when a known provider failure is present, null otherwise.
|
||||
*/
|
||||
public static String getProviderErrorJs(String provider) {
|
||||
if (PROVIDER_IMGUR.equals(provider)) {
|
||||
return "(function(){"
|
||||
+ "var text=((document.body&&document.body.innerText)||'');"
|
||||
+ "if(text.indexOf('CREATE_ALBUM_FAIL')!==-1)return 'CREATE_ALBUM_FAIL';"
|
||||
+ "if(text.indexOf('Something went wrong')!==-1)return 'Something went wrong';"
|
||||
+ "return null;})()";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String buildBlockedJs(String[] selectors) {
|
||||
StringBuilder sb = new StringBuilder("(function(){var s=[");
|
||||
for (int i = 0; i < selectors.length; i++) {
|
||||
|
||||
@@ -50,6 +50,12 @@ public class MediaUploadRecipesTest {
|
||||
assertNull(MediaUploadRecipes.getUploadUrl(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAndroidUploadProvider_includesAndroidUploadProviders() {
|
||||
assertTrue(MediaUploadRecipes.isAndroidUploadProvider(MediaUploadRecipes.PROVIDER_IMGUR));
|
||||
assertTrue(MediaUploadRecipes.isAndroidUploadProvider(MediaUploadRecipes.PROVIDER_IMGBB));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTriggerFileInputJs_imgur_containsSelectors() {
|
||||
String js = MediaUploadRecipes.getTriggerFileInputJs(MediaUploadRecipes.PROVIDER_IMGUR);
|
||||
@@ -110,6 +116,13 @@ public class MediaUploadRecipesTest {
|
||||
assertTrue(js.contains("challenge") || js.contains("captcha") || js.contains("recaptcha"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProviderErrorJs_imgur_detectsCreateAlbumFail() {
|
||||
String js = MediaUploadRecipes.getProviderErrorJs(MediaUploadRecipes.PROVIDER_IMGUR);
|
||||
assertNotNull(js);
|
||||
assertTrue(js.contains("CREATE_ALBUM_FAIL"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failureClassification_inputNotFound_stageConstant() {
|
||||
assertEquals(MediaUploadAutomationRunner.STAGE_INPUT_NOT_FOUND, "input_not_found");
|
||||
@@ -127,6 +140,11 @@ public class MediaUploadRecipesTest {
|
||||
assertEquals(MediaUploadAutomationRunner.STAGE_BLOCKED_DETECTED, "blocked_detected");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failureClassification_providerError_stageConstant() {
|
||||
assertEquals(MediaUploadAutomationRunner.STAGE_PROVIDER_ERROR, "provider_error");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failureClassification_uploadTimedOut_stageConstant() {
|
||||
assertEquals(MediaUploadAutomationRunner.STAGE_UPLOAD_TIMED_OUT, "upload_timed_out");
|
||||
|
||||
Reference in New Issue
Block a user