Created
January 19, 2019 13:22
-
-
Save steniowagner/50dcc2204aa7e5b4cf3d3ae9157dff48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import { Platform } from 'react-native'; | |
import RNFetchBlob from 'rn-fetch-blob'; | |
import shorthash from 'shorthash'; | |
const BASE_DIRECTORY_PATH = `${RNFetchBlob.fs.dirs.DocumentDir}/bon_appetit`; | |
const FILE_PREFIX = Platform.OS === 'ios' ? '' : 'file://'; | |
const verifyResourceAlreadyExists = async (path: string): any => { | |
const resourceAlreadyExists = await RNFetchBlob.fs.exists(path); | |
return resourceAlreadyExists; | |
}; | |
const getItemFromCache = async ( | |
uri: string, | |
folder: string = 'images', | |
fileExtension: string = 'jpg', | |
): any => { | |
const fileName = `${shorthash.unique(uri)}`; | |
const fileDirectory = `${BASE_DIRECTORY_PATH}/${folder}`; | |
const pathToFile = `${fileDirectory}/${fileName}.${fileExtension}`; | |
const isResourceAlreadyCached = await verifyResourceAlreadyExists(pathToFile); | |
if (isResourceAlreadyCached) { | |
return pathToFile; | |
} | |
const isDirectoryAlreadyCreated = await verifyResourceAlreadyExists( | |
fileDirectory, | |
); | |
if (!isDirectoryAlreadyCreated) { | |
await RNFetchBlob.fs.mkdir(fileDirectory); | |
} | |
const result = await RNFetchBlob.config({ | |
path: pathToFile, | |
appendExt: fileExtension, | |
trusty: true, | |
}).fetch('GET', uri); | |
const { respInfo, data } = result; | |
if (respInfo.status === 200) { | |
return FILE_PREFIX + data; | |
} | |
return ''; | |
}; | |
export default { | |
getItemFromCache, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment