Error when moving file with react-native-fs, file already exist - react-native

I am trying to move an image to the library using react-native-fs in iOS
const originalPath = "/private/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/tmp/ReactNative/E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png"
const destinationPath = RNFS.LibraryDirectoryPath + "/kj3.jpg";
RNFS.moveFile(screenShotPath, destinationPath).then((data) => {
console.log(data)
}).catch((err) => {
throw err
})
The destination path in this case is
/var/mobile/Containers/Data/Application/7EB7B0CB-FCA8-49EE-843A-04BBB0B286B1/Library/kj3.jpg
I get an error
Error: “E70143FD-21A8-42AA-BFD2-A8FA45D7D93A.png” couldn’t be moved to “Library” because an item with the same name already exists.
I also get the same error when I try to copy the file.

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.

How to copy file using expo filesystem

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.

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"

Move file from tmp to documents using react-native-fs

I'm trying to move a file selected from a document picker to the Document Directory using react-native-fs and react-native-document picker.
However, I get the error below:
Error: “file name.mp3” couldn’t be moved to “Documents” because either the former doesn't exist, or the folder containing the latter doesn't exist.
What am I doing wrong?
FYI, I'm using iOS.
openDocumentPicker() {
DocumentPicker.show({
filetype: ['public.audio'],
},(error,url) => {
console.log(url);
this.saveAudio(url);
});
}
saveAudio(url) {
var destPath = RNFS.DocumentDirectoryPath + '/' + 'name';
RNFS.moveFile(url, destPath)
.then((success) => {
console.log('file moved!');
})
.catch((err) => {
console.log("Error: " + err.message);
});
}
I believe I've found the error. The issue was that the file I was uploading had a space in it. I needed to decode the URL first before uploading, like so:
var decodedURL = decodeURIComponent(url)
Then I could move the file over.
RNFS.copyFile(decodedURL, destPath)
It happened to me when destination folder doesn't exist.
[tid:com.facebook.react.JavaScript] 'error!', { [Error: The file “source.jpg” doesn’t exist.]
It's a wrong error message from react-native-fs. It should tell target path folder not exists.

How to change mp3 file to wav file in node.js

I am trying to convert mp3 file to wav file but I am not getting idea how to do that, I tried using fluent-ffmpeg library but I don't know how to use that.
I finally figured it out using 'fluent-ffmpeg' library. Here is my code.
const ffmpeg = require('fluent-ffmpeg');
let track = './source.mp3';//your path to source file
ffmpeg(track)
.toFormat('wav')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished !');
})
.save('./hello.wav');//path where you want to save your file
if you are facing
An error occurred: Cannot find ffmpeg
then add the ffmpeg path in system environment variables. Your VSCode still may dont recognise the ffmpeg command so in that case re-start VSCode.