2019-10-19 15:20:42 +02:00
|
|
|
import axios from 'axios'
|
|
|
|
|
2019-10-21 11:52:51 +02:00
|
|
|
export function formatBytes(a: number, b: number) {
|
|
|
|
if (a === 0) return '0 Bytes'
|
|
|
|
const c = 1024
|
|
|
|
const d = b || 2
|
|
|
|
const e = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
const f = Math.floor(Math.log(a) / Math.log(c))
|
|
|
|
return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:46:06 +02:00
|
|
|
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)
|
|
|
|
})
|
2019-10-21 11:52:51 +02:00
|
|
|
|
2019-10-24 13:46:06 +02:00
|
|
|
stream.on('error', reject)
|
|
|
|
stream.write(file)
|
|
|
|
stream.end()
|
2019-10-21 11:52:51 +02:00
|
|
|
})
|
|
|
|
|
2019-10-24 13:46:06 +02:00
|
|
|
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}`
|
2019-10-21 11:52:51 +02:00
|
|
|
return cid
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:20:42 +02:00
|
|
|
export async function pingUrl(url: string) {
|
|
|
|
try {
|
|
|
|
const response = await axios(url, { timeout: 5000 })
|
|
|
|
if (!response || response.status !== 200) return false
|
|
|
|
return true
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 11:52:51 +02:00
|
|
|
|
|
|
|
export function parseHTML(str: string) {
|
|
|
|
const tmp = document.implementation.createHTMLDocument()
|
|
|
|
tmp.body.innerHTML = str
|
|
|
|
return tmp.body.children
|
|
|
|
}
|