aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java69
1 files changed, 66 insertions, 3 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
index 37e39285..ed144465 100644
--- a/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
@@ -3,6 +3,7 @@ package de.thedevstack.conversationsplus.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -14,18 +15,80 @@ import de.thedevstack.conversationsplus.enums.FileStatus;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.persistance.FileBackend;
+import eu.siacs.conversations.utils.FileUtils;
+import eu.siacs.conversations.utils.MimeUtils;
/**
* Utility class to work with messages.
*/
public final class MessageUtil {
+ public static void extractFileParamsFromBody(Message message) {
+ if (null == message) {
+ return;
+ }
+ String body = message.getBody();
+ /**
+ * there are a few cases where spaces result in an unwanted behavior, e.g.
+ * "http://example.com/image.jpg" text that will not be shown /abc.png"
+ * or more than one image link in one message.
+ */
+ if (null == body || body.isEmpty() || body.contains(" ")) {
+ return;
+ }
+
+ FileParams fileParams = message.getFileParams();
+ if (null == fileParams) {
+ fileParams = new FileParams();
+ message.setFileParams(fileParams);
+ }
+
+ try {
+ URL url = new URL(body);
+ if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ return;
+ }
+ if (message.isHttpUploaded()) {
+ fileParams.setUrl(url.toString());
+ message.setTreatAsDownloadable(Message.Decision.MUST);
+ return;
+ }
+ String extension = FileUtils.getRelevantExtension(url);
+ if (extension == null) {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ return;
+ }
+ byte[] ivAndKey = UrlUtil.getIvAndKeyFromURL(url);
+
+ if (null != ivAndKey) {
+ if (MimeUtils.guessMimeTypeFromExtension(extension) != null) {
+ message.setTreatAsDownloadable(Message.Decision.MUST);
+ fileParams.setKeyAndIv(ivAndKey);
+ fileParams.setUrl(url.toString());
+ } else {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ }
+ } else if (Transferable.VALID_IMAGE_EXTENSIONS.contains(extension)
+ || Transferable.WELL_KNOWN_EXTENSIONS.contains(extension)) {
+ message.setTreatAsDownloadable(Message.Decision.SHOULD);
+ fileParams.setUrl(url.toString());
+ } else {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ }
+
+ } catch (MalformedURLException e) {
+ message.setTreatAsDownloadable(Message.Decision.NEVER);
+ }
+ }
+
public static boolean needsDownload(Message message) {
FileStatus fileStatus = (null != message.getFileParams()) ? message.getFileParams().getFileStatus() : null;
- return (null != fileStatus && (fileStatus == FileStatus.NEEDS_DOWNLOAD
- || fileStatus == FileStatus.UNDEFINED))
+ return (null == fileStatus
+ || (null != fileStatus && (fileStatus == FileStatus.NEEDS_DOWNLOAD || fileStatus == FileStatus.UNDEFINED)))
&& message.treatAsDownloadable() != Message.Decision.NEVER;
}
@@ -122,6 +185,7 @@ public final class MessageUtil {
FileParams fileParams = message.getFileParams();
if (null == fileParams) {
fileParams = new FileParams();
+ message.setFileParams(fileParams);
}
fileParams.setSize(size);
if (null != url) {
@@ -137,7 +201,6 @@ public final class MessageUtil {
if (null != relativeFilePathFromMessage && relativeFilePathFromMessage.startsWith("/")) {
fileParams.setPath(relativeFilePathFromMessage);
}
- message.setFileParams(fileParams);
}
private MessageUtil() {