From 1d6c992e8db6ceb2a5e327905c73ce21cdee70ac Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Sat, 2 Feb 2019 12:11:09 -0500 Subject: [PATCH] store file name with ipfs upload --- app/components/AddFunder.jsx | 2 +- app/utils/ipfs.js | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/components/AddFunder.jsx b/app/components/AddFunder.jsx index 9e37142..18fa90f 100644 --- a/app/components/AddFunder.jsx +++ b/app/components/AddFunder.jsx @@ -162,7 +162,7 @@ const AddFunder = ({ appendFundProfile }) => ( onBlur={handleBlur} value={values.funderDescription || ''} /> - {status && status.profileImg && ipfs} + {status && status.profileImg && ipfs} { event.stopPropagation() event.preventDefault() const file = event.target.files[0] + console.log({file}) saveToIpfs(file, cb, imgCb) } - +const formatForIpfs = file => { + const { name, type } = file + const content = fileReaderPullStream(file) + return [{ + path: `/root/${name}`, + content + }] +} const saveToIpfs = (file, cb, imgCb) => { let ipfsId - const fileStream = fileReaderPullStream(file) - ipfs.add(fileStream, { progress: (prog) => console.log(`received: ${prog}`) }) + ipfs.add(formatForIpfs(file), { progress: (prog) => console.log(`received: ${prog}`) }) .then((response) => { console.log(response) ipfsId = response[0].hash @@ -30,12 +37,13 @@ const saveToIpfs = (file, cb, imgCb) => { } export const getImageFromIpfs = async (hash, cb) => { - const files = await getFile(hash); - const { content } = files[0]; - const arrayBufferView = new Uint8Array(content); - const blob = new Blob([ arrayBufferView ], { type: "image/jpeg" }); - const img = URL.createObjectURL(blob); - cb(img) + const files = await getFile(hash) + const file = files.slice(-1)[0] + const { content } = file + const arrayBufferView = new Uint8Array(content) + const blob = new Blob([ arrayBufferView ], { type: "image/jpeg" }) + const img = URL.createObjectURL(blob) + cb({ ...file, img }) }; export const getFile = CID => {