Skip to content

Commit cc9de07

Browse files
committed
fix: prefer local uri when sharing if there
1 parent 41940f0 commit cc9de07

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

package/src/components/ImageGallery/components/ImageGalleryFooter.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,29 @@ export const ImageGalleryFooterWithContext = (props: ImageGalleryFooterPropsWith
142142
return;
143143
}
144144
const extension = photo.mime_type?.split('/')[1] || 'jpg';
145-
setSavingInProgress(true);
146-
const localFile = await NativeHandlers.saveFile({
147-
fileName: `${photo.user?.id || 'ChatPhoto'}-${
148-
photo.messageId
149-
}-${selectedIndex}.${extension}`,
150-
fromUrl: photo.uri,
151-
});
152-
setSavingInProgress(false);
145+
const shouldDownload = photo.uri && photo.uri.includes('http');
146+
let localFile;
147+
// If the file is already uploaded to a CDN, create a local reference to
148+
// it first; otherwise just use the local file
149+
if (shouldDownload) {
150+
setSavingInProgress(true);
151+
localFile = await NativeHandlers.saveFile({
152+
fileName: `${photo.user?.id || 'ChatPhoto'}-${
153+
photo.messageId
154+
}-${selectedIndex}.${extension}`,
155+
fromUrl: photo.uri,
156+
});
157+
setSavingInProgress(false);
158+
} else {
159+
localFile = photo.uri;
160+
}
161+
153162
// `image/jpeg` is added for the case where the mime_type isn't available for a file/image
154163
await NativeHandlers.shareImage({ type: photo.mime_type || 'image/jpeg', url: localFile });
155-
await NativeHandlers.deleteFile({ uri: localFile });
164+
// Only delete the file if a local reference has been created beforehand
165+
if (shouldDownload) {
166+
await NativeHandlers.deleteFile({ uri: localFile });
167+
}
156168
} catch (error) {
157169
setSavingInProgress(false);
158170
console.log(error);

0 commit comments

Comments
 (0)