1import RNFS, { exists, mkdir } from 'react-native-fs'; 2 3const basePath = `${RNFS.PicturesDirectoryPath}/MusicFree/`; 4export async function saveToGallery(src: string) { 5 const fileName = `${basePath}${Date.now()}.png`; 6 if (!(await RNFS.exists(basePath))) { 7 await RNFS.mkdir(basePath); 8 } 9 if (await RNFS.exists(src)) { 10 try { 11 await RNFS.copyFile(src, fileName); 12 } catch (e) { 13 console.log('... ', e); 14 } 15 } 16 if (src.startsWith('http')) { 17 await RNFS.downloadFile({ 18 fromUrl: src, 19 toFile: fileName, 20 background: true, 21 }); 22 } 23 if (src.startsWith('data')) { 24 await RNFS.writeFile(fileName, src); 25 } 26} 27 28export function sizeFormatter(bytes: number) { 29 if (bytes === 0) return '0B'; 30 let k = 1024, 31 sizes = ['B', 'KB', 'MB', 'GB'], 32 i = Math.floor(Math.log(bytes) / Math.log(k)); 33 return (bytes / Math.pow(k, i)).toFixed(1) + sizes[i]; 34} 35 36export async function checkAndCreateDir(path: string) { 37 const filePath = path; 38 if (!(await exists(filePath))) { 39 await mkdir(filePath); 40 } 41}