organize the code

This commit is contained in:
Gleidson Daniel 2022-11-09 16:40:22 -03:00
parent a73afa9494
commit 84db57e405
2 changed files with 31 additions and 27 deletions

View File

@ -4,13 +4,11 @@ import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av';
import { BorderlessButton } from 'react-native-gesture-handler';
import { getInfoAsync } from 'expo-file-system';
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
import * as FileSystem from 'expo-file-system';
import { FFmpegKit, ReturnCode } from 'ffmpeg-kit-react-native';
import styles from './styles';
import I18n from '../../i18n';
import { themes } from '../../lib/constants';
import { parseFilename, sanitizeString } from '../../lib/methods/audioFile';
import { encodeAudio } from '../../lib/methods/audioFile';
import { events, logEvent } from '../../lib/methods/helpers/log';
import { TSupportedThemes } from '../../theme';
import { CustomIcon } from '../CustomIcon';
@ -160,28 +158,17 @@ export default class RecordAudio extends React.PureComponent<IMessageBoxRecordAu
await this.recording.stopAndUnloadAsync();
const fileURI = this.recording.getURI();
const fileName = sanitizeString(fileURI!.substring(fileURI!.lastIndexOf('/') + 1).split('.')[0]);
const newFilePath = `${FileSystem.cacheDirectory}/${fileName}.mp3`;
const ffmpegCommand = `-hide_banner -y -i ${fileURI} -c:a libmp3lame -qscale:a 2 ${newFilePath}`;
FFmpegKit.execute(ffmpegCommand).then(async session => {
const returnCode = await session.getReturnCode();
if (ReturnCode.isSuccess(returnCode)) {
const fileData = await getInfoAsync(newFilePath);
console.log(fileData)
const audioEncodedPath = await encodeAudio(fileURI!);
const fileData = await getInfoAsync(audioEncodedPath);
const fileInfo = {
name: parseFilename(fileData.uri),
name: `${Date.now()}.mp3`,
mime: 'audio/mpeg',
type: 'audio/mpeg',
store: 'Uploads',
path: newFilePath,
path: fileData.uri,
size: fileData.size
};
onFinish(fileInfo);
} else {
console.log('error');
}
});
} catch (error) {
logEvent(events.ROOM_AUDIO_FINISH_F);
}

View File

@ -1,12 +1,13 @@
import * as FileSystem from 'expo-file-system';
import { FFmpegKit, ReturnCode } from 'ffmpeg-kit-react-native';
import { sanitizeLikeString } from '../database/utils';
import { store } from '../store/auxStore';
import log from './helpers/log';
export const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1));
const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1));
export const parseFilename = (value: string) => {
const parseFilename = (value: string) => {
const extension = value.substring(value.lastIndexOf('.') + 1);
const filename = sanitizeString(value.substring(value.lastIndexOf('/') + 1).split('.')[0]);
return `${filename}.${extension}`;
@ -52,3 +53,19 @@ export const deleteAllAudioFiles = async (serverUrl: string): Promise<void> => {
log(error);
}
};
export const encodeAudio = (fileURI: string): Promise<string> =>
new Promise((resolve, reject) => {
const fileName = sanitizeString(fileURI.substring(fileURI.lastIndexOf('/') + 1).split('.')[0]);
const newFilePath = `${FileSystem.cacheDirectory}${fileName}.mp3`;
const ffmpegCommand = `-hide_banner -y -i ${fileURI} -c:a libmp3lame -qscale:a 2 ${newFilePath}`;
FFmpegKit.execute(ffmpegCommand).then(async session => {
const returnCode = await session.getReturnCode();
if (ReturnCode.isSuccess(returnCode)) {
return resolve(newFilePath);
}
if (ReturnCode.isCancel(returnCode)) {
return reject();
}
});
});