Massive/write.ts

22 lines
765 B
TypeScript
Raw Normal View History

2023-06-27 03:16:59 +00:00
import { PermissionsAndroid, Platform } from 'react-native'
import { Dirs, FileSystem } from 'react-native-file-access'
import { toast } from './toast'
export const write = async (name: string, data: string) => {
2022-10-31 04:22:08 +00:00
const filePath = `${Dirs.DocumentDir}/${name}`
const permission = async () => {
if (Platform.OS !== 'android') return true
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
2022-10-31 04:22:08 +00:00
)
return granted === PermissionsAndroid.RESULTS.GRANTED
}
const granted = await permission()
if (!granted) return
await FileSystem.writeFile(filePath, data)
2023-06-27 03:16:59 +00:00
if (Platform.OS === 'android') {
await FileSystem.cpExternal(filePath, name, 'downloads')
2023-06-27 03:16:59 +00:00
}
toast(`Downloaded ${name}`)
2022-10-31 04:22:08 +00:00
}