Skip to content

Instantly share code, notes, and snippets.

@romreed
Created March 2, 2018 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romreed/38921fa120377f43fca177a79900030d to your computer and use it in GitHub Desktop.
Save romreed/38921fa120377f43fca177a79900030d to your computer and use it in GitHub Desktop.
import {resetActionToMain, resetActionAuth} from './navigate'
import {NavigationActions} from 'react-navigation'
import {CALL_API, API_ROOT} from '../middleware/api'
import RNFetchBlob from 'react-native-fetch-blob'
import {Platform} from "react-native";
import IntentModule from '../native/intent'
export const DOWNLOAD_FILE_BEGIN = 'DOWNLOAD_FILE_BEGIN'
export const DOWNLOAD_FILE_SUCCESS = 'DOWNLOAD_FILE_SUCCESS'
export const DOWNLOAD_FILE_ERROR = 'DOWNLOAD_FILE_ERROR'
export const downloadFile = (id, type, url) => (dispatch, getState) => {
console.log("downloadFile",id, type, url,getState().files.downloaded,getState().files.downloaded[0])
let downloaded = getState().files.downloaded
// if (downloaded!==undefined && downloaded[id] === undefined) {
dispatch({type: 'DOWNLOAD_FILE_BEGIN'})
console.log('IntentModule', IntentModule)
RNFetchBlob
.config({
//path : RNFetchBlob.fs.dirs.DocumentDir + '/userThumbnails/test.jpeg',
'Authorization': "Bearer " + getState().auth.token,
appendExt: type,
fileCache: true,
})
.fetch('GET', url, {
'Cache-Control': 'no-store',
})
.then((res) => {
res.path() // where the file is
// console.log("res", res.path())
//
if (Platform.OS === 'ios') {
RNFetchBlob.ios.openDocument(res.path()); // results in path/to/file.jpg
}else {
let base64Str = res.base64();
console.log("base64Str",base64Str,res)
console.log("RNFetchBlob",RNFetchBlob)
console.log(",type,res.path()",type,res.path())
// RNFetchBlob.android.actionViewIntent(res.path(), type)
// RNFetchBlob.android.actionViewIntent(res.path(), 'image/jpeg');
IntentModule.open(res.path(), 'image/jpeg')
///data/user/0/com.asist.android/files/RNFetchBlobTmp_0h4tveiun5ujzias07c93.jpg
}
dispatch({type: DOWNLOAD_FILE_SUCCESS, payload: {id: id, location: res.path()}})
})
.catch((errorMessage, statusCode) => {
dispatch({type: DOWNLOAD_FILE_ERROR, payload: {message: errorMessage}})
})
// }else{
// RNFetchBlob.ios.openDocument(downloaded[id].location);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment