How to copy file using expo filesystem - react-native

I am trying to create a simple whatsapp status downloader app for android using expo and I am using expo-file-system for copying file but I am unable to get the file anywhere.
My functions looks something like this :
export const saveFile = async (filePath) => {
const res = await FileSystem.copyAsync({
from: filePath,
to: `${FileSystem.documentDirectory}saved/test.mp4`,
});
};
where filepath uses SAF file path.

Related

Download txt file from text in React Native

I'm trying to Download txt file from a text but that function is not working fine.
Txt Function
import RNFS from 'react-native-fs';
const txtDownload = () => {
let path = `${RNFS.DocumentDirectoryPath}/${filename}.txt`;
RNFS.writeFile(path, `Here is text`, 'utf8').then((res) => {
Toast('File saved successfully');
}
).catch((err) => {
Toast(err);
});
}
It returns File saved successfully but I can't find file
Log the path and check the location where it's trying to store. For me, it shows the location as below when I try to store in ${RNFS.DocumentDirectoryPath}/help.txt
/data/user/0/com.sample/files/help.txt
I suspect you were checking in the Documents folder. I don't know if you can write to the Documents folder. But it works for the Download folder using RNFS.DownloadDirectoryPath.

Download m3u8 using Using react-native-fs in react native

I have m3u8 url link and i want to download stream using download url but not able to download it the complete folder
const destination_url = RNFS.LibraryDirectoryPath + "filelink"
const dnd_url = url
RNFS.downloadFile({
fromUrl: dnd_url,
toFile: destination_url
}).promise.then(res => {
if (res.statusCode == 200) {
}
})
But not getting that how i will get full folder in my storage. Let me know if anybody have a good idea about it.

When using Expo for React Native, is there any way to read the mime-type from a file?

React Native - File Type is great but needs to be linked, and thus, won't work with a managed Expo project.
How can you read the file mime type when using Expo managed projects?
You can simply use the mime Javascript library to get the mime-type from the file name: https://www.npmjs.com/package/mime
import * as mime from 'mime';
const mimeType = mime.getType('my-doc.pdf') // => 'application/pdf'
If you are using the DocumentPicker to get the file, you can obtain the file name from the result directly:
const result = await DocumentPicker.getDocumentAsync();
if (result.type === 'cancel') {
return;
}
const fileName = result.name;
const mimeType = mime.getType(fileName);

How to mkdir with React-Native-FS

I'm trying to add functionality to my RN app that will allow users to create a new directory within their phone's file system.
I have tried to write the code so that the function creates a directory in the path /storage/emulated/0/AppName/NewFolder, since /storage/emulated/0 is the same path that is used by other apps I use to store user data (such as recording apps)
makeDirectory = () => {
const { currentFolder, units } = this.props;
const directoryName = 'New Folder'
const currentDirectory = units
const absolutePath = `/storage/emulated/0/MyApp/${currentDirectory}`
RNFS.mkdir(absolutePath)
.then((result) => {
console.log('result', result)
})
.catch((err) => {
console.warn('err', err)
})
}
However, this is just giving me an error: directory could not be created. I feel I am missing something here, and am not supposed to be saving the files like this to the phone's system.
My ultimate goal is to have the app with it's own folder system that will be mirrored within /storage/emulated/0/MyApp/home
const AppFolder = 'DirNameyouwant';
const DirectoryPath= RNFS.ExternalStorageDirectoryPath +'/'+ AppFolder;
RNFS.mkdir(DirectoryPath);
I've just tried out and my folder is created successfully.
use
const absolutePath = `/storage/emulated/0/${currentDirectory}`
instead
const absolutePath = `/storage/emulated/0/MyApp/${currentDirectory}`
You need to ask permission from the user for accessing his phone storage.
See this comment by Lylest on this reported issue from GitHub
I have resolved my issue by adding this line in the Android Manifest
file
android:requestLegacyExternalStorage="true"

Implementing Google Cloud Speech-to-Text on React-Native

I'm trying to implement google's speech-to-text on a react native app but I can't find an example or documentation about it, I'm fairly new to react-native so I'm kinda lost, there's a sample using node.js on google official docs and I'm trying to 'copy' it to react-native but I didn't had any success.
Here's the Node.js example:
async function main() {
// Imports the Google Cloud client library
const speech = require('#google-cloud/speech');
const fs = require('fs');
// Creates a client
const client = new speech.SpeechClient();
// The name of the audio file to transcribe
const fileName = './resources/audio.raw';
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
}
main().catch(console.error);
First of all, the 'fs' package doesn't work on react-native, so I had to use 'react-native-fs' which has different functions
Second, should I really use the 'require' to call the speech package? I guess react-native would use 'import' instead, right?
Any tips on how can I implement it on react-native? Thanks!
You can't run #google-cloud/speech on react native as RN runs on JV environment and the library (#google-cloud/speech) runs on nodejs environment. You could create an api and deploy it or else use this library react-native-google-speech-api