File tree Expand file tree Collapse file tree 3 files changed +18
-2
lines changed
contexts/messageInputContext Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ import { removeReservedFields } from '../../utils/removeReservedFields';
8787import {
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 ( / ^ ( f i l e : \/ \/ | c o n t e n t : \/ \/ ) / , '' ) ;
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 ) {
Original file line number Diff line number Diff 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 ( / ^ ( f i l e : \/ \/ | c o n t e n t : \/ \/ ) / , '' ) ;
1112+ const filename = file . name ?? getFileNameFromPath ( uri ) ;
11121113
11131114 try {
11141115 const compressedUri = await compressedImageURI ( file , value . compressImageQuality ) ;
Original file line number Diff line number Diff 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+ } ;
You can’t perform that action at this time.
0 commit comments