Skip to content

Instantly share code, notes, and snippets.

@warodri-sendbird
Created September 3, 2021 08:56
Show Gist options
  • Save warodri-sendbird/01c7073f281d7d3962ef637d077922ac to your computer and use it in GitHub Desktop.
Save warodri-sendbird/01c7073f281d7d3962ef637d077922ac to your computer and use it in GitHub Desktop.
const selectFile = async () => {
try {
if (Platform.OS === 'android') {
const permission = await check(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE);
if (permission !== RESULTS.GRANTED) {
const result = await request(PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE);
if (result !== RESULTS.GRANTED) {
throw new Error('Please allow the storage access permission request.');
}
}
} else if (Platform.OS === 'ios') {
// TODO:
}
const result = await DocumentPicker.pick({
type: [
DocumentPicker.types.images,
DocumentPicker.types.video,
DocumentPicker.types.audio,
DocumentPicker.types.plainText,
DocumentPicker.types.zip
]
});
const copyPath = `${RNFS.TemporaryDirectoryPath}/${result.name}`;
await RNFS.copyFile(result.uri, copyPath);
const fileStat = await RNFS.stat(copyPath);
const params = new sendbird.FileMessageParams();
params.file = {
...result,
uri: `file://${fileStat.path}`
};
dispatch({ type: 'start-loading' });
channel.sendFileMessage(params, (err, message) => {
dispatch({ type: 'end-loading' });
if (!err) {
dispatch({ type: 'send-message', payload: { message } });
} else {
setTimeout(() => {
dispatch({ type: 'error', payload: { error: 'Failed to send a message.' } });
}, 500);
}
});
} catch (err) {
console.log(err);
if (!DocumentPicker.isCancel(err)) {
dispatch({ type: 'error', payload: { error: err.message } });
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment