fix the try catch download media

This commit is contained in:
Reinaldo Neto 2023-05-19 11:35:36 -03:00
parent 02ba6e9961
commit 99973c0d28
4 changed files with 44 additions and 38 deletions

View File

@ -300,19 +300,21 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
const { messageId } = this.props; const { messageId } = this.props;
const { user } = this.context; const { user } = this.context;
this.setState({ loading: true }); this.setState({ loading: true });
const url = this.getUrl(); try {
if (url && this.filePath) { const url = this.getUrl();
const audio = await downloadMediaFile({ if (url && this.filePath) {
downloadUrl: `${url}?rc_uid=${user.id}&rc_token=${user.token}`, const audio = await downloadMediaFile({
mediaType: MediaTypes.audio, downloadUrl: `${url}?rc_uid=${user.id}&rc_token=${user.token}`,
messageId, mediaType: MediaTypes.audio,
path: this.filePath messageId,
}); path: this.filePath
if (!audio) { });
return this.setState({ loading: false, toDownload: true });
await this.sound.loadAsync({ uri: audio });
return this.setState({ loading: false, toDownload: false });
} }
await this.sound.loadAsync({ uri: this.filePath }); } catch {
return this.setState({ loading: false, toDownload: false }); return this.setState({ loading: false, toDownload: true });
} }
}; };

View File

@ -142,25 +142,28 @@ const ImageContainer = React.memo(
const handleDownload = async () => { const handleDownload = async () => {
setLoading(true); setLoading(true);
// The param file.title_link is the one that point to image with best quality, however we still need to test the imageUrl try {
// And we don't have sure that exists the file.title_link // The param file.title_link is the one that point to image with best quality, however we still need to test the imageUrl
const imgUrl = imageUrl || formatAttachmentUrl(newFile.title_link || newFile.image_url, user.id, user.token, baseUrl); // And we don't have sure that exists the file.title_link
const imageUri = await downloadMediaFile({ const imgUrl = imageUrl || formatAttachmentUrl(newFile.title_link || newFile.image_url, user.id, user.token, baseUrl);
downloadUrl: imgUrl, const imageUri = await downloadMediaFile({
mediaType: MediaTypes.image, downloadUrl: imgUrl,
messageId, mediaType: MediaTypes.image,
path: filePath.current messageId,
}); path: filePath.current
if (!imageUri) { });
setNewFile(prev => ({
...prev,
title_link: imageUri
}));
setToDownload(false);
setLoading(false);
} catch (e) {
console.log('🚀 ~ file: Image.tsx:164 ~ handleDownload ~ e:', e);
setLoading(false); setLoading(false);
return setToDownload(true); return setToDownload(true);
} }
setNewFile(prev => ({
...prev,
title_link: filePath.current
}));
setToDownload(false);
setLoading(false);
}; };
const onPress = () => { const onPress = () => {

View File

@ -123,19 +123,20 @@ const Video = React.memo(
const handleDownload = async () => { const handleDownload = async () => {
setLoading(true); setLoading(true);
const videoUri = await downloadMediaFile({ try {
downloadUrl: video, const videoUri = await downloadMediaFile({
mediaType: MediaTypes.video, downloadUrl: video,
messageId, mediaType: MediaTypes.video,
path: filePath.current messageId,
}); path: filePath.current
if (videoUri) { });
setNewFile(prev => ({ setNewFile(prev => ({
...prev, ...prev,
video_url: filePath.current video_url: videoUri
})); }));
} finally {
setLoading(false);
} }
setLoading(false);
}; };
const onPress = async () => { const onPress = async () => {

View File

@ -56,7 +56,7 @@ export function downloadMediaFile({
messageId: string; messageId: string;
downloadUrl: string; downloadUrl: string;
path: string; path: string;
}): Promise<string | void> { }): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const downloadKey = mediaDownloadKey(mediaType, messageId); const downloadKey = mediaDownloadKey(mediaType, messageId);
const options = { const options = {