Skip to content

Instantly share code, notes, and snippets.

@elinteerie
Created June 5, 2023 23:36
Show Gist options
  • Save elinteerie/f378e1ba836f588a61a59c89d8097a51 to your computer and use it in GitHub Desktop.
Save elinteerie/f378e1ba836f588a61a59c89d8097a51 to your computer and use it in GitHub Desktop.
code to implement hiding of the videos in app-specific directory React Native
//Install the react-native-fs library by running the following command in your project's root directory
npm install react-native-fs --save
react-native link react-native-fs
import RNFS from 'react-native-fs';
// Function to download the Video From Cloud or Storage
async function downloadVideo(videoUrl, savePath) {
try {
const response = await RNFS.downloadFile({
fromUrl: videoUrl,
toFile: savePath,
});
if (response.statusCode === 200) {
console.log('Video downloaded successfully');
} else {
console.error('Failed to download video:', response.statusCode);
}
} catch (error) {
console.error('Error downloading video:', error);
}
}
//Call the downloadVideo function
const videoUrl = 'https://sirangle.com/video.mp4'; // Replace with the actual video URL u are working with
const savePath = `${RNFS.DocumentDirectoryPath}/video.mp4`; // Replace with the desired save path dat u created
downloadVideo(videoUrl, savePath);
// Function to Hide the downloaded Video
async function hideVideo(videoUrl) {
try {
const hiddenDirPath = `${RNFS.DocumentDirectoryPath}/.hiddenVideos`;
// Check if the hidden directory exists, if not, create it
const hiddenDirExists = await RNFS.exists(hiddenDirPath);
if (!hiddenDirExists) {
await RNFS.mkdir(hiddenDirPath);
}
// Extract the video file name from the URL
const videoFileName = videoUrl.split('/').pop();
// Set the save path in the hidden directory
const savePath = `${hiddenDirPath}/${videoFileName}`;
// Download the video file
const response = await RNFS.downloadFile({
fromUrl: videoUrl,
toFile: savePath,
});
if (response.statusCode === 200) {
console.log('Video downloaded and hidden successfully');
} else {
console.error('Failed to download video:', response.statusCode);
}
} catch (error) {
console.error('Error downloading video:', error);
}
}
//Call the hideVideo
const videoUrl = 'https://sirangle.com/video.mp4'; // Replace with the actual video URL u are working with
hideVideo(videoUrl);
@ifeanyiifeanyi
Copy link

Thanks. But please don't delete the link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment