xref: /MusicFree/src/utils/fileUtils.ts (revision d1f226e6a7fb0e668b71a5e9dfcb5e85d25344f3)
1import RNFS 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