diff --git a/app/containers/message/Audio.tsx b/app/containers/message/Audio.tsx index bbb3e1d3b..99acaa117 100644 --- a/app/containers/message/Audio.tsx +++ b/app/containers/message/Audio.tsx @@ -19,6 +19,7 @@ import { withDimensions } from '../../dimensions'; import { TGetCustomEmoji } from '../../definitions/IEmoji'; import { IAttachment } from '../../definitions'; import { TSupportedThemes } from '../../theme'; +import { downloadAudioFile } from '../../lib/methods/audioFile'; interface IButton { loading: boolean; @@ -137,7 +138,10 @@ class MessageAudio extends React.Component => { + const info = await FileSystem.getInfoAsync(dir); + if (info.exists && info.isDirectory) { + return; + } + await FileSystem.makeDirectoryAsync(dir, { intermediates }); + return ensureDirAsync(dir, intermediates); +}; + +export const downloadAudioFile = async (url: string, fileUrl: string): Promise => { + let path = ''; + try { + const serverUrl = store.getState().server.server; + const serverUrlParsed = serverUrl.substring(serverUrl.lastIndexOf('/') + 1); + const folderPath = `${FileSystem.documentDirectory}audios/${serverUrlParsed}`; + const filePath = `${folderPath}/${fileUrl.substring(fileUrl.lastIndexOf('/') + 1)}`; + await ensureDirAsync(folderPath); + const file = await FileSystem.getInfoAsync(filePath); + if (!file.exists) { + const downloadedFile = await FileSystem.downloadAsync(url, filePath); + path = downloadedFile.uri; + } else { + path = file.uri; + } + } catch (error) { + log(error); + } + return path; +}; + +export const deleteAllAudioFiles = async (serverUrl: string): Promise => { + try { + const serverUrlParsed = serverUrl.substring(serverUrl.lastIndexOf('/') + 1); + const path = `${FileSystem.documentDirectory}audios/${serverUrlParsed}`; + await FileSystem.deleteAsync(path); + } catch (error) { + log(error); + } +}; diff --git a/app/views/SettingsView/index.tsx b/app/views/SettingsView/index.tsx index caf7e1884..e98fe394e 100644 --- a/app/views/SettingsView/index.tsx +++ b/app/views/SettingsView/index.tsx @@ -31,6 +31,7 @@ import { onReviewPress } from '../../lib/methods/helpers/review'; import SidebarView from '../SidebarView'; import { clearCache } from '../../lib/methods'; import { Services } from '../../lib/services'; +import { deleteAllAudioFiles } from '../../lib/methods/audioFile'; type TLogScreenName = 'SE_GO_LANGUAGE' | 'SE_GO_DEFAULTBROWSER' | 'SE_GO_THEME' | 'SE_GO_PROFILE' | 'SE_GO_SECURITYPRIVACY'; @@ -98,6 +99,7 @@ class SettingsView extends React.Component { dispatch } = this.props; dispatch(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Clear_cache_loading') })); + await deleteAllAudioFiles(server); await clearCache({ server }); await FastImage.clearMemoryCache(); await FastImage.clearDiskCache();