Support data uri for sending file

Stephen Paul Weber created

Change summary

src/main/java/eu/siacs/conversations/persistance/FileBackend.java | 26 
src/main/java/eu/siacs/conversations/utils/MimeUtils.java         |  4 
2 files changed, 26 insertions(+), 4 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/persistance/FileBackend.java 🔗

@@ -53,6 +53,7 @@ import com.madebyevan.thumbhash.ThumbHash;
 
 import com.wolt.blurhashkt.BlurHashDecoder;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.File;
@@ -713,6 +714,26 @@ public class FileBackend {
         }
     }
 
+    private InputStream openInputStream(Uri uri) throws IOException {
+        if (uri != null && "data".equals(uri.getScheme())) {
+            String[] parts = uri.getSchemeSpecificPart().split(",", 2);
+            byte[] data;
+            if (Arrays.asList(parts[0].split(";")).contains("base64")) {
+                String[] parts2 = parts[0].split(";", 2);
+                parts[0] = parts2[0];
+                data = Base64.decode(parts[1], 0);
+            } else {
+                try {
+                    data = parts[1].getBytes("UTF-8");
+                } catch (final IOException e) {
+                    data = new byte[0];
+                }
+            }
+            return new ByteArrayInputStream(data);
+        }
+        return mXmppConnectionService.getContentResolver().openInputStream(uri);
+    }
+
     private void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
         Log.d(
                 Config.LOGTAG,
@@ -724,8 +745,7 @@ public class FileBackend {
             throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
         }
         try (final OutputStream os = new FileOutputStream(file);
-                final InputStream is =
-                        mXmppConnectionService.getContentResolver().openInputStream(uri)) {
+                final InputStream is = openInputStream(uri)) {
             if (is == null) {
                 throw new FileCopyException(R.string.error_file_not_found);
             }
@@ -951,7 +971,7 @@ public class FileBackend {
 
     public void setupRelativeFilePath(final Message message, final Uri uri, final String extension) throws FileCopyException, XmppConnectionService.BlockedMediaException {
         try {
-            setupRelativeFilePath(message, mXmppConnectionService.getContentResolver().openInputStream(uri), extension);
+            setupRelativeFilePath(message, openInputStream(uri), extension);
         } catch (final FileNotFoundException e) {
             throw new FileCopyException(R.string.error_file_not_found);
         } catch (final IOException e) {

src/main/java/eu/siacs/conversations/utils/MimeUtils.java 🔗

@@ -572,7 +572,9 @@ public final class MimeUtils {
         }
         // sometimes this works (as with the commit content api)
         if (mimeType == null) {
-            mimeType = uri.getQueryParameter("mimeType");
+            try {
+                mimeType = uri.getQueryParameter("mimeType");
+            } catch (final Throwable throwable) { }
         }
         return mimeType;
     }