liquid-funding/app/utils/ipfs.js

82 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-02-01 21:22:27 +00:00
import IPFS from 'ipfs'
import fileReaderPullStream from 'pull-file-reader'
2019-02-01 22:35:14 +00:00
import { Matcher } from '@areknawo/rex'
2019-02-02 19:56:08 +00:00
import { getImageType } from './images'
2019-02-01 21:22:27 +00:00
const ipfsMatcher = new Matcher().begin().find('ipfs/')
export const ipfs = new IPFS()
ipfs.on('ready', () => {
console.log('Node is ready to use!')
})
2019-02-01 21:22:27 +00:00
2019-02-01 22:35:14 +00:00
export const isIpfs = str => ipfsMatcher.test(str)
export const captureFile = (event, cb, imgCb) => {
2019-02-01 21:22:27 +00:00
event.stopPropagation()
event.preventDefault()
const file = event.target.files[0]
2019-02-13 16:28:26 +00:00
const files = event.target.files
const formattedFiles = formatFileList(files)
console.log({files, formattedFiles})
saveToIpfs(formattedFiles, cb, imgCb)
}
2019-04-03 18:57:50 +00:00
export const formatFileList = files => {
2019-02-13 16:28:26 +00:00
const formattedList = []
for (let i=0; i<files.length; i++) {
formattedList.push(formatForIpfs(files[i]))
}
return formattedList
2019-02-01 21:22:27 +00:00
}
2019-04-03 18:57:50 +00:00
export const formatForIpfs = file => {
2019-02-02 17:11:09 +00:00
const { name, type } = file
const content = fileReaderPullStream(file)
2019-02-13 16:28:26 +00:00
return {
2019-02-02 17:11:09 +00:00
path: `/root/${name}`,
content
2019-02-13 16:28:26 +00:00
}
2019-02-02 17:11:09 +00:00
}
2019-04-03 18:57:50 +00:00
export const saveToIpfs = (files, cb, imgCb) => {
2019-02-01 21:22:27 +00:00
let ipfsId
2019-02-13 16:28:26 +00:00
ipfs.add(files, { progress: (prog) => console.log(`received: ${prog}`) })
2019-02-01 21:22:27 +00:00
.then((response) => {
console.log(response)
ipfsId = response[0].hash
cb(`ipfs/${ipfsId}`)
2019-02-01 22:35:14 +00:00
getImageFromIpfs(ipfsId, imgCb)
2019-02-01 21:22:27 +00:00
}).catch((err) => {
console.error(err)
})
}
2019-02-01 22:35:14 +00:00
2019-04-03 18:57:50 +00:00
export const uploadToIpfs = async files => {
const res = await ipfs.add(files, { progress: (prog) => console.log(`received: ${prog}`) })
return `ipfs/${res[0].hash}`
}
2019-02-01 22:35:14 +00:00
export const getImageFromIpfs = async (hash, cb) => {
const res = await getFromIpfs(hash)
cb(res)
2019-02-13 21:02:43 +00:00
}
export const getFromIpfs = async hash => {
2019-02-13 16:28:26 +00:00
const files = await getFiles(hash)
2019-02-02 17:11:09 +00:00
const file = files.slice(-1)[0]
const { content } = file
const arrayBufferView = new Uint8Array(content)
2019-02-02 19:56:08 +00:00
const blob = new Blob([ arrayBufferView ], { type: getImageType(file) })
2019-02-02 17:11:09 +00:00
const img = URL.createObjectURL(blob)
return { ...file, img }
}
2019-02-01 22:35:14 +00:00
2019-02-13 16:28:26 +00:00
export const getFiles = CID => {
const clean = CID.split('/').slice(-1)[0]
2019-02-01 22:35:14 +00:00
return new Promise(function(resolve, reject) {
2019-03-06 15:11:58 +00:00
ipfs.get(clean, (err, files) => {
2019-02-01 22:35:14 +00:00
if (err) reject(err)
else resolve(files)
})
})
}