1
0
mirror of https://github.com/oceanprotocol/ipfs synced 2024-11-22 01:36:59 +01:00

rewrite to use ipfs.add()

This commit is contained in:
Matthias Kretschmann 2020-04-07 19:47:23 +02:00
parent c6df7bc99d
commit 4ce836784b
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -9,33 +9,27 @@ export function formatBytes(a: number, b: number) {
return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
}
const streamFiles = (ipfs: any, file: any) =>
new Promise((resolve, reject) => {
const stream = ipfs.addReadableStream({
wrapWithDirectory: true
// progress: (length: number) => setFileSizeReceived(formatBytes(length, 0))
})
stream.on('data', (data: any) => {
console.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(file)
stream.end()
})
export async function addToIpfs(files: File[], ipfs: any) {
const file = [...files][0]
const fileDetails = { path: file.name, content: file }
const directoryCid = await streamFiles(ipfs, fileDetails)
const cid = `${directoryCid}/${file.name}`
const source = await ipfs.add(fileDetails, {
wrapWithDirectory: true,
progress: (prog: number) => console.log(`received: ${prog}`)
})
try {
for await (const ipfsFile of source) {
// The last path will contain the directory hash
if (ipfsFile.path === '') {
const cid = `${ipfsFile.cid.toString()}/${file.name}`
return cid
}
}
} catch (err) {
console.error(err)
}
}
export async function pingUrl(url: string) {
try {