Added code to catch situations where a url is not actually a url, before it escalates and crashes the app.
This commit is contained in:
parent
380e6f902f
commit
a0542a39fb
|
@ -15,11 +15,16 @@
|
|||
package fr.gouv.etalab.mastodon.asynctasks;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.util.Patterns;
|
||||
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.select.Elements;
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface;
|
||||
|
||||
|
||||
|
@ -43,23 +48,38 @@ public class RetrieveMetaDataAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
|
||||
String potentialUrl = new String();
|
||||
try {
|
||||
Document document = Jsoup.connect(url).userAgent(userAgent).get();
|
||||
Elements metaOgTitle = document.select("meta[property=og:title]");
|
||||
if (metaOgTitle != null) {
|
||||
title = metaOgTitle.attr("content");
|
||||
} else {
|
||||
title = document.title();
|
||||
Matcher matcher;
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
|
||||
matcher = Patterns.WEB_URL.matcher(url);
|
||||
else
|
||||
matcher = Helper.urlPattern.matcher(url);
|
||||
while (matcher.find()){
|
||||
int matchStart = matcher.start(1);
|
||||
int matchEnd = matcher.end();
|
||||
if(matchStart < matchEnd && url.length() >= matchEnd)
|
||||
potentialUrl = url.substring(matchStart, matchEnd);
|
||||
}
|
||||
Elements metaOgDescription = document.select("meta[property=og:description]");
|
||||
if (metaOgDescription != null) {
|
||||
description = metaOgDescription.attr("content");
|
||||
} else {
|
||||
description = "";
|
||||
}
|
||||
Elements metaOgImage = document.select("meta[property=og:image]");
|
||||
if (metaOgImage != null) {
|
||||
image = metaOgImage.attr("content");
|
||||
// If we actually have a URL then make use of it.
|
||||
if (potentialUrl.length() > 0) {
|
||||
Document document = Jsoup.connect(potentialUrl).userAgent(userAgent).get();
|
||||
Elements metaOgTitle = document.select("meta[property=og:title]");
|
||||
if (metaOgTitle != null) {
|
||||
title = metaOgTitle.attr("content");
|
||||
} else {
|
||||
title = document.title();
|
||||
}
|
||||
Elements metaOgDescription = document.select("meta[property=og:description]");
|
||||
if (metaOgDescription != null) {
|
||||
description = metaOgDescription.attr("content");
|
||||
} else {
|
||||
description = "";
|
||||
}
|
||||
Elements metaOgImage = document.select("meta[property=og:image]");
|
||||
if (metaOgImage != null) {
|
||||
image = metaOgImage.attr("content");
|
||||
}
|
||||
}
|
||||
} catch (IOException | IndexOutOfBoundsException e) {
|
||||
error = true;
|
||||
|
|
Loading…
Reference in New Issue