mirror of
https://github.com/oceanprotocol/commons.git
synced 2023-03-15 18:03:00 +01:00
commit
a8cde6499b
@ -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, readFileAsync } 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'
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) {
|
|||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [message, setMessage] = useState('')
|
const [message, setMessage] = useState('')
|
||||||
const [fileSize, setFileSize] = useState('')
|
const [fileSize, setFileSize] = useState('')
|
||||||
const [fileSizeReceived, setFileSizeReceived] = useState('')
|
const [fileSizeReceived] = useState('')
|
||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -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) {
|
||||||
@ -54,17 +47,13 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) {
|
|||||||
setLoading(true)
|
setLoading(true)
|
||||||
setError('')
|
setError('')
|
||||||
|
|
||||||
const { path, size } = acceptedFiles[0]
|
const file = acceptedFiles[0]
|
||||||
|
const { path, size } = file
|
||||||
const totalSize = formatBytes(size, 0)
|
const totalSize = formatBytes(size, 0)
|
||||||
setFileSize(totalSize)
|
setFileSize(totalSize)
|
||||||
|
|
||||||
// Add file to IPFS node
|
// Add file to IPFS node
|
||||||
const content: any = await readFileAsync(acceptedFiles[0])
|
const fileDetails = { path, content: file }
|
||||||
const data = Buffer.from(content)
|
|
||||||
const fileDetails = {
|
|
||||||
path,
|
|
||||||
content: data
|
|
||||||
}
|
|
||||||
|
|
||||||
const cid = await addToIpfs(fileDetails)
|
const cid = await addToIpfs(fileDetails)
|
||||||
if (!cid) return
|
if (!cid) return
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
import React, { FormEvent, PureComponent, ChangeEvent } from 'react'
|
import React, {
|
||||||
|
lazy,
|
||||||
|
Suspense,
|
||||||
|
FormEvent,
|
||||||
|
PureComponent,
|
||||||
|
ChangeEvent
|
||||||
|
} from 'react'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { Logger, File } from '@oceanprotocol/squid'
|
import { Logger, File } from '@oceanprotocol/squid'
|
||||||
import shortid from 'shortid'
|
import shortid from 'shortid'
|
||||||
@ -6,11 +12,13 @@ import Button from '../../../components/atoms/Button'
|
|||||||
import Help from '../../../components/atoms/Form/Help'
|
import Help from '../../../components/atoms/Form/Help'
|
||||||
import ItemForm from './ItemForm'
|
import ItemForm from './ItemForm'
|
||||||
import Item from './Item'
|
import Item from './Item'
|
||||||
import Ipfs from './Ipfs'
|
|
||||||
import styles from './index.module.scss'
|
import styles from './index.module.scss'
|
||||||
|
|
||||||
import { serviceUri } from '../../../config'
|
import { serviceUri } from '../../../config'
|
||||||
import cleanupContentType from '../../../utils/cleanupContentType'
|
import cleanupContentType from '../../../utils/cleanupContentType'
|
||||||
|
import Spinner from '../../../components/atoms/Spinner'
|
||||||
|
|
||||||
|
const Ipfs = lazy(() => import('./Ipfs'))
|
||||||
|
|
||||||
export interface FilePublish extends File {
|
export interface FilePublish extends File {
|
||||||
found: boolean // non-standard
|
found: boolean // non-standard
|
||||||
@ -202,7 +210,11 @@ export default class Files extends PureComponent<FilesProps, FilesStates> {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{isIpfsFormShown && <Ipfs addFile={this.addFile} />}
|
{isIpfsFormShown && (
|
||||||
|
<Suspense fallback={<Spinner message="Loading..." />}>
|
||||||
|
<Ipfs addFile={this.addFile} />
|
||||||
|
</Suspense>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user