Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created July 5, 2020 17:29
Show Gist options
  • Save fakenickels/02076fec7dec9dd0f9f42d9de032753e to your computer and use it in GitHub Desktop.
Save fakenickels/02076fec7dec9dd0f9f42d9de032753e to your computer and use it in GitHub Desktop.
import RNFetchBlob from 'rn-fetch-blob'
import gql from 'graphql-tag'
import client from '../apollo'
const UPLOAD_FILE_QUERY = gql`
query UploadFileQuery($input: GetSignedPutURLInput!) {
getSignedPutUrl(input: $input) {
error
path
url
}
}
`
const getSignedUrl = async ({ fileName, mimeType }) => {
const result = await client.query({
query: UPLOAD_FILE_QUERY,
variables: {
input: { fileName, mimeType },
},
})
const error = result.data.getSignedPutUrl.error
if (error) throw error
return result.data.getSignedPutUrl
}
export default async file => {
const fileName = file.path.match(/([^/]+)$/)[0]
const { url, path } = await getSignedUrl({
fileName,
mimeType: file.mime,
})
return RNFetchBlob.fetch(
'PUT',
url,
{
'Content-Type': file.mime,
},
RNFetchBlob.wrap(file.path.replace('file://', '')),
)
.then(res => {
if (res.data && res.data.includes('Error')) {
console.log('Uploader', res)
throw res.data
}
return { name: path, path, fileType: file.mime }
})
.catch(err => {
return null
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment