Skip to content

Commit d4b90fc

Browse files
authored
fix: upload image issue when clicking a photo using expo-image-picker on Expo 51 (#2559)
* fix: upload image issue when clicking a photo using expo-image-picker * fix: handle the filename using regex
1 parent abf6f2a commit d4b90fc

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import { removeReservedFields } from '../../utils/removeReservedFields';
8787
import {
8888
defaultEmojiSearchIndex,
8989
generateRandomId,
90+
getFileNameFromPath,
9091
isBouncedMessage,
9192
isLocalUrl,
9293
MessageStatusTypes,
@@ -1574,7 +1575,7 @@ const ChannelWithContext = <
15741575
attachment.image_url &&
15751576
isLocalUrl(attachment.image_url)
15761577
) {
1577-
const filename = image.name ?? image.uri.replace(/^(file:\/\/|content:\/\/)/, '');
1578+
const filename = image.name ?? getFileNameFromPath(image.uri);
15781579
// if any upload is in progress, cancel it
15791580
const controller = uploadAbortControllerRef.current.get(filename);
15801581
if (controller) {

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
FileState,
6565
FileStateValue,
6666
generateRandomId,
67+
getFileNameFromPath,
6768
isBouncedMessage,
6869
TriggerSettings,
6970
} from '../../utils/utils';
@@ -1108,7 +1109,7 @@ export const MessageInputProvider = <
11081109
let response = {} as SendFileAPIResponse;
11091110

11101111
const uri = file.uri || '';
1111-
const filename = file.name ?? uri.replace(/^(file:\/\/|content:\/\/)/, '');
1112+
const filename = file.name ?? getFileNameFromPath(uri);
11121113

11131114
try {
11141115
const compressedUri = await compressedImageURI(file, value.compressImageQuality);

package/src/utils/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,17 @@ export const reduceMessagesToString = <
638638
>(
639639
messages: FormatMessageResponse<StreamChatGenerics>[],
640640
): string => messages.map(stringifyMessage).join();
641+
642+
/**
643+
* Utility to get the file name from the path using regex.
644+
* `[^/]+` matches one or more characters that are not a slash (/), ensuring we capture the filename part.
645+
* `\.` matches the period before the file extension.
646+
* `[^/]+$` matches one or more characters that are not a slash (/) until the end of the string, capturing the file extension.
647+
* @param path string
648+
* @returns string
649+
*/
650+
export const getFileNameFromPath = (path: string) => {
651+
const pattern = /[^/]+\.[^/]+$/;
652+
const match = path.match(pattern);
653+
return match ? match[0] : '';
654+
};

0 commit comments

Comments
 (0)