1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

stream files when adding

This commit is contained in:
Matthias Kretschmann 2019-10-24 13:31:35 +02:00
parent 28c1d2e331
commit 77deaf063e
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 27 additions and 17 deletions

View File

@ -3,7 +3,7 @@ import React, { useState, useEffect } from 'react'
import useIpfsApi, { IpfsConfig } from '../../../../hooks/use-ipfs-api' import useIpfsApi, { IpfsConfig } from '../../../../hooks/use-ipfs-api'
import Spinner from '../../../../components/atoms/Spinner' import Spinner from '../../../../components/atoms/Spinner'
import Dropzone from '../../../../components/molecules/Dropzone' import Dropzone from '../../../../components/molecules/Dropzone'
import { formatBytes, pingUrl } from '../../../../utils/utils' import { formatBytes, pingUrl, streamFiles } from '../../../../utils/utils'
import { ipfsGatewayUri, ipfsNodeUri } from '../../../../config' import { ipfsGatewayUri, ipfsNodeUri } from '../../../../config'
import Form from './Form' import Form from './Form'
@ -32,14 +32,7 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) {
async function addToIpfs(data: any) { async function addToIpfs(data: any) {
try { try {
const response = await ipfs.add(data, { const cid = await streamFiles(ipfs, data)
wrapWithDirectory: true
// progress: (length: number) =>
// setFileSizeReceived(formatBytes(length, 0))
})
// CID of wrapping directory is returned last
const cid = response[response.length - 1].hash
console.log(`File added: ${cid}`) console.log(`File added: ${cid}`)
return cid return cid
} catch (error) { } catch (error) {
@ -60,10 +53,7 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) {
setFileSize(totalSize) setFileSize(totalSize)
// Add file to IPFS node // Add file to IPFS node
const fileDetails = { const fileDetails = { path, content: file }
path,
content: file
}
const cid = await addToIpfs(fileDetails) const cid = await addToIpfs(fileDetails)
if (!cid) return if (!cid) return

View File

@ -1,5 +1,5 @@
/* eslint-disable no-console */
import axios from 'axios' import axios from 'axios'
import { Logger } from '@oceanprotocol/squid'
export function formatBytes(a: number, b: number) { export function formatBytes(a: number, b: number) {
if (a === 0) return '0 Bytes' if (a === 0) return '0 Bytes'
@ -15,15 +15,35 @@ export function arraySum(array: number[]) {
return array.reduce((a, b) => a + b, 0) return array.reduce((a, b) => a + b, 0)
} }
export function streamFiles(ipfs: any, files: any) {
return new Promise((resolve, reject) => {
const stream = ipfs.addReadableStream({
wrapWithDirectory: true
// progress: (length: number) =>
// setFileSizeReceived(formatBytes(length, 0))
})
stream.on('data', (data: any) => {
Logger.log(`Added ${data.path} hash: ${data.hash}`)
// The last data event will contain the directory hash
if (data.path === '') resolve(data.hash)
})
stream.on('error', reject)
stream.write(files)
stream.end()
})
}
export async function pingUrl(url: string) { export async function pingUrl(url: string) {
try { try {
const response = await axios(url) const response = await axios(url)
if (response.status !== 200) console.error(`Not found: ${url}`) if (response.status !== 200) Logger.error(`Not found: ${url}`)
console.log(`File found: ${url}`) Logger.log(`File found: ${url}`)
return true return true
} catch (error) { } catch (error) {
console.error(error.message) Logger.error(error.message)
} }
return false return false
} }