Skip to content

Instantly share code, notes, and snippets.

@romreed
Created March 3, 2018 09:24
Show Gist options
  • Save romreed/afc73a16a0abe2ffd946eff0591035b3 to your computer and use it in GitHub Desktop.
Save romreed/afc73a16a0abe2ffd946eff0591035b3 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 {Toast} from "native-base"
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 dirs = RNFetchBlob.fs.dirs
filePath = `${dirs.DownloadDir}/jpg.jpg`
// let downloaded = getState().files.downloaded
// if (downloaded!==undefined && downloaded[id] === undefined) {
dispatch({type: 'DOWNLOAD_FILE_BEGIN'})
//
RNFetchBlob
.config({
path : filePath,
'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 {
console.log("RNFetchBlob",RNFetchBlob)
console.log(",type,res.path()",type,res.path())
// RNFetchBlob.android.actionViewIntent(res.path(), type)
RNFetchBlob.android.actionViewIntent(res.path(), 'image/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);
// }
// const fileNameWithExtension = `${title}${(extension.startsWith('.') ? extension : `.${extension}`)}`;
// const downloadFilePath = `${downloadPath}/LogosEducacional/${fileNameWithExtension}`;
// const mimeType = mime.lookup(fileNameWithExtension);
// const task = RNFetchBlob
// .config({
//
// addAndroidDownloads: {
// useDownloadManager: true,
// notification: false,
// title: "jpg.jpg",
// description: 'Click to open file.',
// mime: 'image/jpg',
// path: url,
// },
// })
// .fetch('GET', url, {'Authorization': "Bearer " + getState().auth.token});
//
// task
// .progress((received, total) => {
// if (onProgress) {
// onProgress(received / total);
// }
// })
// .then((res) => {
// onProgress(1);
// if (Platform.OS === 'ios') {
// RNFetchBlob.ios.openDocument(res.path());
// } else {
// RNFetchBlob.android.actionViewIntent(res.path(), 'image/jpg');
// }
// })
//
// .catch((error) => {
// if (error.message === 'cancelled') {
// onProgress(0);
// } else {
// onError(error);
// }
// });
// let dirs = RNFetchBlob.fs.dirs
//
// const android = RNFetchBlob.android
//
// console.log("dirs",dirs)
//
//
// // RNFetchBlob
// // .config({
// // //fileCache : true,
// //
// // addAndroidDownloads : {
// // useDownloadManager : true,
// // title : 'jpg.jpg',
// // description : 'An APK that will be installed',
// // mime : 'IMAGE/JPG',
// // mediaScannable : true,
// // notification : true,
// // path: dirs.DCIMDir+"/jpg.jpg",
// // }
// // })
//
// filePath = `${dirs.DownloadDir}/jpg.jpg`
//
// RNFetchBlob.config({
// path : filePath,
// addAndroidDownloads : {
// title : 'RNFetchBlob test download success',
// description : `File description added by RNFetchblob`,
// mediaScannable : true,
// mime : "image/jpeg",
// notification : true
// }
// })
//
//
// .fetch('GET', url,{'Authorization': "Bearer " + getState().auth.token})
// .then((res) => {
// android.actionViewIntent(res.path(), 'IMAGE/JPG')
// })
// .then(()=>
// Toast.show({
// text: 'Загрузка завершена',
// position: 'bottom',
// duration:2000
// })
// )
// .catch((error)=>console.log("error",error))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment