From 480f201c1cfdbeeee87cbe4ab708daa4f9aa0192 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Tue, 4 Jun 2019 11:16:14 -0400 Subject: [PATCH] update gateway persist --- package.json | 1 + src/components/projects/CreateProject.jsx | 14 +++-------- src/utils/ipfs.js | 29 ++++++++++++++++++++--- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index cbb2465..2f9c3e8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/components/projects/CreateProject.jsx b/src/components/projects/CreateProject.jsx index e0fe63c..ce96716 100644 --- a/src/components/projects/CreateProject.jsx +++ b/src/components/projects/CreateProject.jsx @@ -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}) }} > diff --git a/src/utils/ipfs.js b/src/utils/ipfs.js index c6dca74..47ed98d 100644 --- a/src/utils/ipfs.js +++ b/src/utils/ipfs.js @@ -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}` }