update gateway persist

This commit is contained in:
Barry Gitarts 2019-06-04 11:16:14 -04:00 committed by Barry G
parent 14f2d5e27a
commit 480f201c1c
3 changed files with 30 additions and 14 deletions

View File

@ -73,6 +73,7 @@
"babel-plugin-named-asset-import": "^0.3.1",
"babel-preset-react-app": "^7.0.2",
"bfj": "6.1.1",
"buffer": "^5.2.1",
"case-sensitive-paths-webpack-plugin": "2.2.0",
"chai": "^4.1.0",
"chart.js": "^2.7.3",

View File

@ -1,4 +1,3 @@
/*global Buffer*/
import React, { createRef, useState, useContext } from 'react'
import { Formik } from 'formik'
import classnames from 'classnames'
@ -12,7 +11,7 @@ import Button from '@material-ui/core/Button'
import InputAdornment from '@material-ui/core/InputAdornment'
import CloudUpload from '@material-ui/icons/CloudUpload'
import { withStyles } from '@material-ui/core/styles'
import { formatForIpfs, uploadToIpfsGateway, formatMedia, isWeb } from '../../utils/ipfs'
import { uploadFilesToIpfs, formatMedia, isWeb } from '../../utils/ipfs'
import { FundingContext } from '../../context'
import {ZERO_ADDRESS} from '../../utils/address'
import CurrencySelect from '../base/CurrencySelect'
@ -154,14 +153,7 @@ const SubmissionSection = ({ classes, history }) => {
onSubmit={async (values, { resetForm }) => {
const { title, commitTime } = values
const manifest = createJSON(values)
let fileLists = []
Object.keys(uploads).forEach(k => {
fileLists = [...fileLists, formatForIpfs(uploads[k][0])]
})
fileLists.push({
path: '/root/manifest.json', content: Buffer.from(manifest)
})
const contentHash = await uploadToIpfsGateway(fileLists)
const contentHash = await uploadFilesToIpfs(uploads, manifest, true)
const args = [title, contentHash, account, 0, hoursToSeconds(commitTime), ZERO_ADDRESS]
addProject(...args)
.estimateGas({ from: account })
@ -178,7 +170,7 @@ const SubmissionSection = ({ classes, history }) => {
})
.catch(e => openSnackBar('error', e))
})
console.log({manifest, values, uploads, fileLists, contentHash})
console.log({manifest, values, uploads, contentHash})
}}
>

View File

@ -1,6 +1,7 @@
import IPFS from 'ipfs'
import ipfsClient from 'ipfs-http-client'
import fileReaderPullStream from 'pull-file-reader'
import { Buffer } from 'buffer'
import { Matcher } from '@areknawo/rex'
import { getImageType } from './images'
@ -42,6 +43,16 @@ export const formatForIpfs = file => {
content
}
}
export const formatForIpfsGateway = file => {
const { name, type: _type } = file
const content = file
return {
path: `/root/${name}`,
content
}
}
export const saveToIpfs = (files, cb, imgCb) => {
let ipfsId
ipfs.add(files, { progress: (prog) => console.log(`received: ${prog}`) })
@ -55,15 +66,27 @@ export const saveToIpfs = (files, cb, imgCb) => {
})
}
export const uploadFilesToIpfs = async (files, manifest, gateway = false) => {
let fileLists = []
const formatFn = gateway ? formatForIpfsGateway : formatForIpfs
const uploadFn = gateway ? uploadToIpfsGateway : uploadToIpfs
Object.keys(files).forEach(k => {
fileLists = [...fileLists, formatFn(files[k][0])]
})
fileLists.push({
path: '/root/manifest.json', content: Buffer.from(manifest)
})
const res = await uploadFn(fileLists)
return res
}
export const uploadToIpfs = async files => {
const res = await ipfs.add(files, { progress: (prog) => console.log(`received: ${prog}`) })
return `ipfs/${res[0].hash}`
}
export const uploadToIpfsGateway = async files => {
const res = await ipfs.add(files, { progress: (prog) => console.log(`received: ${prog}`) })
const gatewayUpload = await ipfsHttp.add(files)
console.log({gatewayUpload})
const res = await ipfsHttp.add(files, { progress: (prog) => console.log(`received: ${prog}`) })
return `ipfs/${res[0].hash}`
}