mobile: fix image filename in picker

This commit is contained in:
Ammar Ahmed
2024-05-10 12:10:34 +05:00
parent 2e8b234b63
commit 152794ccd0
4 changed files with 28 additions and 172 deletions

View File

@@ -17,7 +17,13 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React, { useState } from "react";
import { Image, ScrollView, TouchableOpacity, View } from "react-native";
import {
Image,
Platform,
ScrollView,
TouchableOpacity,
View
} from "react-native";
import { Image as ImageType } from "react-native-image-crop-picker";
import { useThemeColors } from "../../../../../../packages/theme/dist";
import { presentSheet } from "../../../services/event-manager";
@@ -65,7 +71,7 @@ export default function AttachImage({
<TouchableOpacity key={item.filename} activeOpacity={0.9}>
<Image
source={{
uri: item.sourceURL || item.path
uri: Platform.OS === "ios" ? item.sourceURL : item.path
}}
style={{
width: 100,

View File

@@ -41,6 +41,7 @@ import { FILE_SIZE_LIMIT, IMAGE_SIZE_LIMIT } from "../../../utils/constants";
import { eCloseSheet } from "../../../utils/events";
import { useTabStore } from "./use-tab-store";
import { editorController, editorState } from "./utils";
import { basename } from "pathe";
const showEncryptionSheet = (file: DocumentPickerResponse) => {
presentSheet({
@@ -265,6 +266,7 @@ const handleImageResponse = async (
response: Image[],
options: PickerOptions
) => {
console.log(response, "result-file-picker");
const result = await AttachImage.present(response, options.context);
if (!result) return;
const compress = result.compress;
@@ -300,7 +302,12 @@ const handleImageResponse = async (
type: "url"
});
const fileName = image.filename || "image";
const fileName = image.sourceURL
? basename(image.sourceURL)
: image.filename || "image";
console.log("attaching image...", fileName);
console.log("attaching file...");
if (!(await attachFile(uri, hash, image.mime, fileName, options))) return;

View File

@@ -0,0 +1,12 @@
diff --git a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
index 5de0845..1b158d8 100644
--- a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
+++ b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
@@ -692,6 +692,7 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
image.putString("mime", options.outMimeType);
image.putInt("size", (int) new File(compressedImagePath).length());
image.putString("modificationDate", String.valueOf(modificationDate));
+ image.putString("sourceURL", path);
if (includeBase64) {
image.putString("data", getBase64StringFromFile(compressedImagePath));

View File

@@ -1,169 +0,0 @@
diff --git a/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/Utils.java b/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/Utils.java
index 8e59033..c87febe 100644
--- a/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/Utils.java
+++ b/node_modules/react-native-image-picker/android/src/main/java/com/imagepicker/Utils.java
@@ -1,6 +1,7 @@
package com.imagepicker;
import android.Manifest;
+import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ContentResolver;
@@ -21,6 +22,7 @@ import android.provider.OpenableColumns;
import android.util.Base64;
import android.webkit.MimeTypeMap;
+import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import androidx.exifinterface.media.ExifInterface;
@@ -36,6 +38,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -266,7 +269,9 @@ public class Utils {
MediaMetadataRetriever m = new MediaMetadataRetriever();
m.setDataSource(context, uri);
int duration = Math.round(Float.parseFloat(m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))) / 1000;
- m.release();
+ try {
+ m.release();
+ } catch(Exception e) {}
return duration;
}
@@ -383,7 +388,7 @@ public class Utils {
return fileUris;
}
- static ReadableMap getImageResponseMap(Uri uri, Options options, Context context) {
+ static WritableMap getImageResponseMap(Uri uri, Options options, Context context) {
String fileName = uri.getLastPathSegment();
int[] dimensions = getImageDimensions(uri, context);
@@ -418,13 +423,16 @@ public class Utils {
for(int i = 0; i < fileUris.size(); ++i) {
Uri uri = fileUris.get(i);
+ String fileName = getNameFromURI(context,uri);
if (isImageType(uri, context)) {
if (uri.getScheme().contains("content")) {
uri = getAppSpecificStorageUri(uri, context);
}
uri = resizeImage(uri, context, options);
- assets.pushMap(getImageResponseMap(uri, options, context));
+ WritableMap resMap = getImageResponseMap(uri, options, context);
+ resMap.putString("originalFileName",fileName);
+ assets.pushMap(resMap);
} else if (isVideoType(uri, context)) {
assets.pushMap(getVideoResponseMap(uri, context));
} else {
@@ -438,6 +446,32 @@ public class Utils {
return response;
}
+ /**
+ * Return file name from Uri given.
+ * @param context the context, cannot be null.
+ * @param uri uri request for file name, cannot be null
+ * @return the corresponding display name for file defined in uri or null if error occurs.
+ */
+ @SuppressLint("Range")
+ static String getNameFromURI(@NonNull Context context, @NonNull Uri uri) {
+ String result = null;
+ Cursor c = null;
+ try {
+ c = context.getContentResolver().query(uri, null, null, null, null);
+ c.moveToFirst();
+ result = c.getString(c.getColumnIndex(OpenableColumns.DISPLAY_NAME));
+ }
+ catch (Exception e){
+ // error occurs
+ }
+ finally {
+ if(c != null){
+ c.close();
+ }
+ }
+ return result;
+ }
+
static ReadableMap getErrorMap(String errCode, String errMsg) {
WritableMap map = Arguments.createMap();
map.putString("errorCode", errCode);
diff --git a/node_modules/react-native-image-picker/ios/ImagePickerManager.m b/node_modules/react-native-image-picker/ios/ImagePickerManager.m
index b634ffb..18b8950 100644
--- a/node_modules/react-native-image-picker/ios/ImagePickerManager.m
+++ b/node_modules/react-native-image-picker/ios/ImagePickerManager.m
@@ -96,6 +96,39 @@ - (void) showPickerViewController:(UIViewController *)picker
});
}
+-(NSString *)cachesDirectoryName
+{
+ static NSString *cachePath = nil;
+ if(!cachePath) {
+ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+ cachePath = [paths objectAtIndex:0];
+ }
+
+ return cachePath;
+}
+
+///
+/// Builds paths for identifiers in the cache directory
+///
+-(NSString *)pathForName:(NSString *)name
+{
+ NSString *cachePath = [self cachesDirectoryName];
+ NSString *path = [cachePath stringByAppendingPathComponent:name];
+ return path;
+}
+
+#pragma mark - NSData Cache methods
+
+///
+/// Saves the given data to the cache directory
+///
+-(NSString *)saveToCacheDirectory:(NSData *)data withName:(NSString *)name
+{
+ NSString *path = [self pathForName:name];
+ [data writeToFile:path atomically:YES];
+ return path;
+}
+
#pragma mark - Helpers
-(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data {
@@ -121,22 +154,14 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data {
asset[@"type"] = [@"image/" stringByAppendingString:fileType];
NSString *fileName = [self getImageFileName:fileType];
- NSString *path = [[NSTemporaryDirectory() stringByStandardizingPath] stringByAppendingPathComponent:fileName];
- [data writeToFile:path atomically:YES];
+ NSString *path = [self saveToCacheDirectory:data withName:fileName];
if ([self.options[@"includeBase64"] boolValue]) {
asset[@"base64"] = [data base64EncodedStringWithOptions:0];
}
- NSURL *fileURL = [NSURL fileURLWithPath:path];
- asset[@"uri"] = [fileURL absoluteString];
-
- NSNumber *fileSizeValue = nil;
- NSError *fileSizeError = nil;
- [fileURL getResourceValue:&fileSizeValue forKey:NSURLFileSizeKey error:&fileSizeError];
- if (fileSizeValue){
- asset[@"fileSize"] = fileSizeValue;
- }
+ asset[@"uri"] = path;
+ asset[@"fileSize"] = [NSNumber numberWithInteger:[data length]];
asset[@"fileName"] = fileName;
asset[@"width"] = @(image.size.width);