Truncate large messages

pull/174/head
M66B 5 years ago
parent 10ae87d7cd
commit e0f7cc9fa7

@ -1734,6 +1734,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
// Format message
if (show_full) {
if (HtmlHelper.truncate(document, false))
document.body()
.appendElement("p")
.appendElement("em")
.text(context.getString(R.string.title_truncated));
HtmlHelper.setViewport(document);
if (inline || show_images)
HtmlHelper.embedInlineImages(context, message.id, document);

@ -5789,6 +5789,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
String html = Helper.readText(file);
Document document = JsoupEx.parse(html);
HtmlHelper.truncate(document, false);
HtmlHelper.embedInlineImages(context, id, document);
Element p = document.createElement("p");

@ -76,7 +76,8 @@ public class HtmlHelper {
private static final float MIN_LUMINANCE = 0.5f;
private static final int TAB_SIZE = 2;
private static final int MAX_AUTO_LINK = 250;
private static final int MAX_TEXT_SIZE = 50 * 1024; // characters
private static final int MAX_FORMAT_TEXT_SIZE = 50 * 1024; // characters
private static final int MAX_FULL_TEXT_SIZE = 1024 * 1024; // characters
private static final int TRACKING_PIXEL_SURFACE = 25; // pixels
private static final List<String> heads = Collections.unmodifiableList(Arrays.asList(
@ -327,17 +328,7 @@ public class HtmlHelper {
}
// Limit length
int length = 0;
for (Element elm : parsed.select("*")) {
for (Node child : elm.childNodes())
if (child instanceof TextNode)
length += ((TextNode) child).text().length();
if (length > MAX_TEXT_SIZE)
elm.remove();
}
if (length > MAX_TEXT_SIZE) {
if (truncate(parsed, true)) {
parsed.body()
.appendElement("p")
.appendElement("em")
@ -1082,7 +1073,11 @@ public class HtmlHelper {
if (body == null)
return null;
String text = JsoupEx.parse(body).text();
Document d = JsoupEx.parse(body);
truncate(d, !full);
String text = d.text();
if (full)
return text;
@ -1098,6 +1093,10 @@ public class HtmlHelper {
html = html.replace("<br> ", "<br>");
Document d = JsoupEx.parse(html);
truncate(d, true);
NodeTraversor.traverse(new NodeVisitor() {
private int qlevel = 0;
private int tlevel = 0;
@ -1176,7 +1175,7 @@ public class HtmlHelper {
for (int i = 0; i < qlevel; i++)
sb.append("> ");
}
}, JsoupEx.parse(html));
}, d);
sb.append("\n");
@ -1198,6 +1197,20 @@ public class HtmlHelper {
return ssb;
}
static boolean truncate(Document d, boolean reformat) {
int at = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE);
int length = 0;
for (Element elm : d.select("*")) {
for (Node child : elm.childNodes())
if (child instanceof TextNode)
length += ((TextNode) child).text().length();
if (length > at)
elm.remove();
}
return (length > at);
}
static Spanned fromHtml(@NonNull String html) {
return fromHtml(html, null, null);
}

@ -21,6 +21,7 @@ package eu.faircode.email;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class JsoupEx {
static Document parse(String html) {
@ -35,6 +36,15 @@ org.jsoup.UncheckedIOException: java.io.IOException: Input is binary and unsuppo
at org.jsoup.parser.Parser.parse(SourceFile:107)
at org.jsoup.Jsoup.parse(SourceFile:58)
*/
return Jsoup.parse(html.replace("\0", ""));
try {
return Jsoup.parse(html.replace("\0", ""));
} catch (OutOfMemoryError ex) {
Log.e(ex);
Document document = Document.createShell("");
Element strong = document.createElement("strong");
strong.text(Log.formatThrowable(ex));
document.body().appendChild(strong);
return document;
}
}
}

@ -697,6 +697,7 @@
<string name="title_no_image">Image could not be decoded</string>
<string name="title_no_search">Search on server is not available for this account</string>
<string name="title_too_large">Message too large to completely reformat</string>
<string name="title_truncated">Message too large to display completely</string>
<string name="title_show_full">Show full message</string>
<string name="title_unused_inline">Unused inline images will be removed on send</string>
<string name="title_accross_remark">Messages moved across accounts will be downloaded again resulting in extra data usage</string>

Loading…
Cancel
Save