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.java67
1 files changed, 64 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 dc9607f0..acd73575 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;
@@ -12,6 +13,7 @@ import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.FileParams;
import de.thedevstack.conversationsplus.entities.Message;
+import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.enums.FileStatus;
import de.thedevstack.conversationsplus.persistance.DatabaseBackend;
import de.thedevstack.conversationsplus.persistance.FileBackend;
@@ -21,10 +23,69 @@ import de.thedevstack.conversationsplus.persistance.FileBackend;
*/
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;
}
@@ -121,6 +182,7 @@ public final class MessageUtil {
FileParams fileParams = message.getFileParams();
if (null == fileParams) {
fileParams = new FileParams();
+ message.setFileParams(fileParams);
}
fileParams.setSize(size);
if (null != url) {
@@ -136,7 +198,6 @@ public final class MessageUtil {
if (null != relativeFilePathFromMessage && relativeFilePathFromMessage.startsWith("/")) {
fileParams.setPath(relativeFilePathFromMessage);
}
- message.setFileParams(fileParams);
}
private MessageUtil() {