From 48669af562d6d3ed7835816d60a2fb400a22a19b Mon Sep 17 00:00:00 2001
From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com>
Date: Thu, 1 Aug 2024 12:04:42 -0700
Subject: [PATCH] Android: Fix uploading GIF URI (#45826)
Summary:
In Android, when constructing a multipart body for a file and that file source is a uri (base64-encoded) we do the following:
1. Decode the base64 string into bytes
2. Create a bitmap object
3. Compress the bitmap object as PNG into new bytes
The process does an unnecessary work (bytes -> bitmap -> bytes) and creates unexpected results e.g. a GIF file will be converted into PNG when uploaded. This PR removes the unnecessary steps (2 and 3).
## Changelog:
[ANDROID] [FIXED] - Fix uploading GIF URI
Pull Request resolved: https://github.com/facebook/react-native/pull/45826
Test Plan:
1. Upload a GIF; use URI (base64-encoded)
2. Verify that the uploaded file is a GIF
```js
const formData = new FormData();
formData.append('photo', {
uri: GIFURI,
type: 'image/gif',
name: 'photo.gif',
});
fetch(UPLOAD_URL,
{
body: formData,
method: "POST",
}):
```
| Before | After |
|:------:|:-----:|
| | |
Reviewed By: cortinico
Differential Revision: D60515478
Pulled By: tdn120
fbshipit-source-id: d6ad1c42631c184c3dcdf3a956641e25d0c1b926
---
.../facebook/react/modules/network/RequestBodyUtil.java | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java
index eab727448f8719..4394fbb2dc2074 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java
@@ -8,8 +8,6 @@
package com.facebook.react.modules.network;
import android.content.Context;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Base64;
import androidx.annotation.Nullable;
@@ -64,11 +62,7 @@ public static boolean isGzipEncoding(@Nullable final String encodingType) {
if (fileContentUriStr.startsWith("data:")) {
byte[] decodedDataUrString = Base64.decode(fileContentUriStr.split(",")[1], Base64.DEFAULT);
- Bitmap bitMap =
- BitmapFactory.decodeByteArray(decodedDataUrString, 0, decodedDataUrString.length);
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- bitMap.compress(Bitmap.CompressFormat.PNG, 0, bytes);
- return new ByteArrayInputStream(bytes.toByteArray());
+ return new ByteArrayInputStream(decodedDataUrString);
}
return context.getContentResolver().openInputStream(fileContentUri);