use fallback to ipfs funcs for gets

This commit is contained in:
Barry Gitarts 2019-08-21 20:07:30 -04:00 committed by Barry G
parent 501a1bfdd7
commit 192d2af943
2 changed files with 28 additions and 25 deletions

View File

@ -1,13 +1,13 @@
import { useState, useEffect, useMemo, useContext } from 'react'
import { unnest } from 'ramda'
import { timeSinceBlock } from '../../utils/dates'
import { getFiles, getFilesWeb, ipfs } from '../../utils/ipfs'
import { databaseExists } from '../../utils/db'
import { getFiles, getFilesWeb, ipfs, getFilesWebTheGraph } from '../../utils/ipfs'
import { arrayToObject } from '../../utils/array'
import { FundingContext } from '../../context'
import { getDelegateProfiles } from '../../actions/profiles'
import { getDelegatePledgesByProfile } from '../../actions/delegates'
const callOrderFns = [getFilesWeb, getFilesWebTheGraph, getFiles]
async function getProjectAge(data, setState){
if (data.profile) {
setState(timeSinceBlock(data.profile.creationTime, 'days'))
@ -22,32 +22,24 @@ async function getProjectCreator(data, setState){
setState(addr)
}
async function tryIpfsGets(CID, setState, index=0){
const ipfsFn = callOrderFns[index]
ipfsFn(CID)
.then((files) => {
setState(files)
})
.catch(async (err) => {
console.log('IPFS getFilesWeb error: ', err, 'Attempt: ', index + 1)
if (callOrderFns[index + 1]) tryIpfsGets(CID, setState, index + 1)
})
}
async function getProjectAssets(data, setState, debug=false){
if (!data.profile) return
const { profile: { url } } = data
const CID = url.split('/').slice(-1)[0]
if (debug) console.log({CID, data, ipfs})
getFilesWeb(CID)
.then((files) => {
setState(files)
const manifest = files[2]
if (debug) console.log({files}, JSON.parse(manifest.content))
})
.catch(async (err) => {
console.log('IPFS getFiles error: ', err)
databaseExists('ipfs')
.catch(() => window.location.reload())
getFiles(CID)
.then((files) => {
setState(files)
const manifest = files[2]
if (debug) console.log({files}, JSON.parse(manifest.content))
})
.catch((err) => {
console.log('IPFS FAILED ON READY', err)
})
})
tryIpfsGets(CID, setState)
}
async function getPledge(dPledge) {

View File

@ -7,10 +7,11 @@ import { getImageType } from './images'
const ipfsMatcher = new Matcher().begin().find('ipfs/')
export const ipfs = new IPFS()
//const ipfsHttp = ipfsClient('test-ipfs.status.im', '2053', { protocol: 'https' })
const ipfsHttp = ipfsClient({ host: 'api.thegraph.com', 'api-path': '/ipfs/api/v0/', protocol: 'https', port: '443' })
const ipfsHttp = ipfsClient('test-ipfs.status.im', '2053', { protocol: 'https' })
const ipfsHttpTheGraph = ipfsClient({ host: 'api.thegraph.com', 'api-path': '/ipfs/api/v0/', protocol: 'https', port: '443' })
window.ipfsHttp = ipfsHttp
window.ipfsHttpTheGraph = ipfsHttpTheGraph
window.jsIPFS = ipfs
ipfs.on('ready', () => {
@ -140,6 +141,16 @@ export const getFilesWeb = CID => {
})
}
export const getFilesWebTheGraph = CID => {
const clean = CID.split('/').slice(-1)[0]
return new Promise(function(resolve, reject) {
ipfsHttpTheGraph.get(clean, (err, files) => {
if (err) reject(err)
else resolve(files)
})
})
}
export const isWeb = str => str.includes('http')
export const formatMedia = str => {
return isWeb(str) ? str : `/root/${str}`