[NEW] Persist audio files (#4448)

* downloads and store the audios instead of loading them

* remove unused function
This commit is contained in:
Gleidson Daniel Silva 2022-08-19 10:24:29 -03:00 committed by GitHub
parent f0ed501450
commit 6b3006d29f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View File

@ -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<IMessageAudioProps, IMessageAudioStat
this.setState({ loading: true });
try {
await this.sound.loadAsync({ uri: `${url}?rc_uid=${user.id}&rc_token=${user.token}` });
if (url) {
const audio = await downloadAudioFile(`${url}?rc_uid=${user.id}&rc_token=${user.token}`, url);
await this.sound.loadAsync({ uri: audio });
}
} catch {
// Do nothing
}

View File

@ -0,0 +1,44 @@
import * as FileSystem from 'expo-file-system';
import { store } from '../store/auxStore';
import log from './helpers/log';
const ensureDirAsync = async (dir: string, intermediates = true): Promise<void> => {
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<string> => {
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<void> => {
try {
const serverUrlParsed = serverUrl.substring(serverUrl.lastIndexOf('/') + 1);
const path = `${FileSystem.documentDirectory}audios/${serverUrlParsed}`;
await FileSystem.deleteAsync(path);
} catch (error) {
log(error);
}
};

View File

@ -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<ISettingsViewProps> {
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();