variable isAutoDownloadEnable name and handleMediaDownload getExtension

This commit is contained in:
Reinaldo Neto 2023-06-07 12:36:30 -03:00
parent c244dd4906
commit 6f3dc60994
2 changed files with 19 additions and 23 deletions

View File

@ -178,13 +178,12 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
try { try {
if (url) { if (url) {
const isCurrentUserAuthor = author?._id === user.id; const isCurrentUserAuthor = author?._id === user.id;
const autoDownload = fetchAutoDownloadEnabled('audioPreferenceDownload'); const isAutoDownloadEnabled = fetchAutoDownloadEnabled('audioPreferenceDownload');
if (autoDownload || isCurrentUserAuthor) { if (isAutoDownloadEnabled || isCurrentUserAuthor) {
await this.startDownload(); await this.handleDownload();
return; return;
} }
// MediaDownloadOption.NEVER or MediaDownloadOption.WIFI and the mobile is using mobile data
return this.setState({ loading: false, cached: false }); return this.setState({ loading: false, cached: false });
} }
} catch { } catch {
@ -286,7 +285,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
this.setState({ paused: !paused }, this.playPause); this.setState({ paused: !paused }, this.playPause);
}; };
startDownload = async () => { handleDownload = async () => {
const { messageId } = this.props; const { messageId } = this.props;
// @ts-ignore can't use declare to type this // @ts-ignore can't use declare to type this
const { user } = this.context; const { user } = this.context;
@ -311,7 +310,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
onPress = () => { onPress = () => {
const { cached } = this.state; const { cached } = this.state;
return cached ? this.togglePlayPause() : this.startDownload(); return cached ? this.togglePlayPause() : this.handleDownload();
}; };
playPause = async () => { playPause = async () => {

View File

@ -77,23 +77,20 @@ const getExtension = (type: MediaTypes, mimeType?: string) => {
if (!mimeType) { if (!mimeType) {
return defaultType[type]; return defaultType[type];
} }
const extensionFromMime = () => { // The library is returning mpag instead of mp3 for audio/mpeg
// The library is returning mpag instead of mp3 for audio/mpeg if (mimeType === 'audio/mpeg') {
if (mimeType === 'audio/mpeg') { return 'mp3';
return 'mp3'; }
} // For older audios the server is returning the type audio/aac and we can't play it as mp3
// For older audios the server is returning the type audio/aac and we can't play it as mp3 if (mimeType === 'audio/aac') {
if (mimeType === 'audio/aac') { return 'm4a';
return 'm4a'; }
} const extension = mime.extension(mimeType);
const extension = mime.extension(mimeType); // The mime.extension can return false when there aren't any extension
// The mime.extension can return false when there aren't any extension if (!extension) {
if (!extension) { return defaultType[type];
return defaultType[type]; }
} return extension;
return extension;
};
return extensionFromMime();
}; };
const ensureDirAsync = async (dir: string, intermediates = true): Promise<void> => { const ensureDirAsync = async (dir: string, intermediates = true): Promise<void> => {