feat(ipfs): Added IPFS file upload to bch-js

This commit is contained in:
danielhumgon
2020-05-13 14:06:18 -04:00
parent a91e6f1920
commit f314b64461
2 changed files with 26 additions and 8 deletions
+12 -4
View File
@@ -47,7 +47,6 @@ class IPFS {
allowedFileTypes: null //type of files allowed to load
}
})
uppy.use(Tus, { endpoint: `${_this.IPFS_API}/uppy-files` })
return uppy
@@ -66,7 +65,7 @@ class IPFS {
// Convert the node.js Buffer to a Blob
// https://stackoverflow.com/questions/14653349/node-js-can%C2%B4t-create-blobs
const blob = Uint8Array.from(fileBuf).buffer
// const blob = Uint8Array.from(fileBuf).buffer
// Get the file name from the path.
const splitPath = path.split("/")
@@ -74,7 +73,7 @@ class IPFS {
const id = _this.uppy.addFile({
name: fileName,
data: blob,
data: fileBuf,
source: "Local",
isRemote: false
})
@@ -82,7 +81,16 @@ class IPFS {
console.log(`id: ${JSON.stringify(id, null, 2)}`)
const upData = await _this.uppy.upload()
console.log(`upData: ${JSON.stringify(upData, null, 2)}`)
//console.log(`upData: ${JSON.stringify(upData, null, 2)}`)
console.log(`
Files Successful : ${upData.successful.length}
Files Failed : ${upData.failed.length}
`)
if (upData.failed.length) {
throw new Error('The file could not be uploaded')
}
return true
} catch (err) {
+14 -4
View File
@@ -36,14 +36,24 @@ describe(`#IPFS`, () => {
assert.equal(true, false, "Unexpected result")
} catch (err) {
console.log(`err.message: ${err.message}`)
//console.log(`err.message: ${err.message}`)
assert.include(err.message,`Could not find this file`)
}
})
it("should do something", async () => {
const path = `/home/trout/work/personal/bch-js/test/unit/ipfs.js`
it("should return true if the file is uploaded", async () => {
//const path = `/home/trout/work/personal/bch-js/test/unit/ipfs.js`
try {
const path = `${__dirname}/ipfs.js`
const result = await bchjs.IPFS.uploadFile(path)
await bchjs.IPFS.uploadFile(path)
assert.isBoolean(result)
assert.isTrue(result)
} catch (err) {
//console.log(err)
assert.equal(true, false, "Unexpected result")
}
})
})
})