diff --git a/app/containers/message/Audio.tsx b/app/containers/message/Audio.tsx index 4eab262ed..cf2abb2d9 100644 --- a/app/containers/message/Audio.tsx +++ b/app/containers/message/Audio.tsx @@ -124,11 +124,12 @@ class MessageAudio extends React.Component { + console.log('🚀 ~ file: Audio.tsx:162 ~ MessageAudio ~ awaitthis.sound.loadAsync ~ value:', value); + }) + .catch(er => { + console.log('🚀 ~ file: Audio.tsx:158 ~ MessageAudio ~ awaitthis.sound.loadAsync ~ er:', er); + }); + return this.setState({ loading: false }); + } await this.handleAutoDownload(); } @@ -161,24 +184,14 @@ class MessageAudio extends React.Component { - const { messageId, author, file } = this.props; + const { author } = this.props; const { user } = this.context; const url = this.getUrl(); try { if (url) { - const fileSearch = await searchMediaFileAsync({ - type: MediaTypes.audio, - mimeType: file.audio_type, - messageId - }); - if (fileSearch?.file?.exists) { - await this.sound.loadAsync({ uri: fileSearch.file.uri }); - return this.setState({ loading: false }); - } - const autoDownload = await isAutoDownloadEnabled('audioPreferenceDownload', { author, user }); if (autoDownload) { - await this.startDownload(); + return await this.startDownload(); } // MediaDownloadOption.NEVER or MediaDownloadOption.WIFI and the mobile is using mobile data @@ -190,7 +203,7 @@ class MessageAudio extends React.Component { - const { messageId, file } = this.props; + const { messageId } = this.props; const { user } = this.context; this.setState({ loading: true }); - const url = this.getUrl(); - - if (url) { - const fileSearch = await searchMediaFileAsync({ - type: MediaTypes.audio, - mimeType: file.audio_type, - messageId - }); + if (url && this.filePath) { const audio = await downloadMediaFile({ - url: `${url}?rc_uid=${user.id}&rc_token=${user.token}`, - filePath: fileSearch.filePath + downloadUrl: `${url}?rc_uid=${user.id}&rc_token=${user.token}`, + mediaType: MediaTypes.audio, + messageId, + path: this.filePath }); - await this.sound.loadAsync({ uri: audio }); + if (!audio) { + return this.setState({ loading: false, toDownload: true }); + } + await this.sound.loadAsync({ uri: this.filePath }); return this.setState({ loading: false, toDownload: false }); } }; diff --git a/app/containers/message/Image.tsx b/app/containers/message/Image.tsx index 127e59ff0..fb4ebe58d 100644 --- a/app/containers/message/Image.tsx +++ b/app/containers/message/Image.tsx @@ -4,7 +4,6 @@ import FastImage from 'react-native-fast-image'; import { dequal } from 'dequal'; import { createImageProgress } from 'react-native-image-progress'; import * as Progress from 'react-native-progress'; -import * as FileSystem from 'expo-file-system'; import { BlurView } from '@react-native-community/blur'; import Touchable from './Touchable'; @@ -16,7 +15,13 @@ import { TGetCustomEmoji } from '../../definitions/IEmoji'; import { IAttachment, IUserMessage } from '../../definitions'; import { TSupportedThemes, useTheme } from '../../theme'; import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl'; -import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload'; +import { + MediaTypes, + cancelDownload, + downloadMediaFile, + isDownloadActive, + searchMediaFileAsync +} from '../../lib/methods/handleMediaDownload'; import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference'; import RCActivityIndicator from '../ActivityIndicator'; import { CustomIcon } from '../CustomIcon'; @@ -93,28 +98,35 @@ export const MessageImage = React.memo( const ImageContainer = React.memo( ({ file, imageUrl, showAttachment, getCustomEmoji, style, isReply, author, messageId }: IMessageImage) => { + const [newFile, setNewFile] = useState(file); const [toDownload, setToDownload] = useState(true); const [loading, setLoading] = useState(false); const { theme } = useTheme(); const { baseUrl, user } = useContext(MessageContext); - const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl); + const img = imageUrl || formatAttachmentUrl(newFile.image_url, user.id, user.token, baseUrl); const filePath = useRef(''); - const downloadResumable = useRef(null); useLayoutEffect(() => { const handleAutoDownload = async () => { if (img) { - const searchImageBestQuality = await searchMediaFileAsync({ + const searchImageCached = await searchMediaFileAsync({ type: MediaTypes.image, - mimeType: file.image_type, + mimeType: newFile.image_type, messageId }); - filePath.current = searchImageBestQuality.filePath; - if (searchImageBestQuality.file?.exists) { - file.title_link = searchImageBestQuality.file.uri; + filePath.current = searchImageCached.filePath; + if (searchImageCached.file?.exists) { + setNewFile(prev => ({ + ...prev, + title_link: searchImageCached.file?.uri + })); return setToDownload(false); } + if (isDownloadActive(MediaTypes.image, messageId)) { + return setLoading(true); + } + const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload', { user, author }); if (autoDownload) { await handleDownload(); @@ -130,25 +142,32 @@ const ImageContainer = React.memo( const handleDownload = async () => { setLoading(true); - const imgUrl = imageUrl || formatAttachmentUrl(file.title_link || file.image_url, user.id, user.token, baseUrl); - downloadResumable.current = FileSystem.createDownloadResumable(imgUrl, filePath.current); + // The param file.title_link is the one that point to image with best quality, however we still need to test the imageUrl + // And we don't have sure that exists the file.title_link + const imgUrl = imageUrl || formatAttachmentUrl(newFile.title_link || newFile.image_url, user.id, user.token, baseUrl); const imageUri = await downloadMediaFile({ - url: imgUrl, - filePath: filePath.current, - downloadResumable: downloadResumable.current + downloadUrl: imgUrl, + mediaType: MediaTypes.image, + messageId, + path: filePath.current }); if (!imageUri) { setLoading(false); return setToDownload(true); } - file.title_link = imageUri; + setNewFile(prev => ({ + ...prev, + title_link: filePath.current + })); setToDownload(false); setLoading(false); }; const onPress = () => { - if (loading && downloadResumable.current) { - return downloadResumable.current.cancelAsync(); + if (loading && isDownloadActive(MediaTypes.image, messageId)) { + cancelDownload(MediaTypes.image, messageId); + setLoading(false); + return setToDownload(true); } if (toDownload && !loading) { @@ -159,15 +178,15 @@ const ImageContainer = React.memo( return; } - return showAttachment(file); + return showAttachment(newFile); }; - if (file.description) { + if (newFile.description) { return (