Added getPaymentInfo() to IPFS class

This commit is contained in:
Chris Troutner
2020-05-16 09:17:44 -07:00
parent e02a5d37e7
commit 83c1e7f449
3 changed files with 77 additions and 8 deletions
+16 -5
View File
@@ -63,10 +63,6 @@ class IPFS {
// Read in the file.
const fileBuf = _this.fs.readFileSync(path)
// 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
// Get the file name from the path.
const splitPath = path.split("/")
const fileName = splitPath[splitPath.length - 1]
@@ -92,7 +88,7 @@ class IPFS {
if (upData.successful.length) {
delete upData.successful[0].data
// console.log(`upData: ${JSON.stringify(upData, null, 2)}`)
console.log(`upData: ${JSON.stringify(upData, null, 2)}`)
const fileObj = {
schemaVersion: 1,
@@ -113,6 +109,21 @@ class IPFS {
throw err
}
}
// Call this after uploadFile().
async getPaymentInfo(fileObj) {
try {
const fileData = await _this.axios.post(`${this.IPFS_API}/files`, {
file: fileObj
})
// console.log(`fileData.data: ${JSON.stringify(fileData.data, null, 2)}`)
return fileData.data
} catch (err) {
console.error(`Error in getPaymentInfo()`)
throw err
}
}
}
module.exports = IPFS
+24 -2
View File
@@ -42,6 +42,28 @@ const uploadData = {
uploadID: "cka90u4m300000fi0cldg6lrf"
}
module.exports = {
uploadData
const paymentInfo = {
success: true,
hostingCostBCH: 0.00004201,
hostingCostUSD: 0.01,
file: {
payloadLink: "",
hasBeenPaid: false,
_id: "5ebf5d04cba8b038394e0d62",
schemaVersion: 1,
size: 2374,
fileId: "uppy-ipfs/js-1e-application/octet-stream",
fileName: "ipfs.js",
fileExtension: "js",
createdTimestamp: "1589599492.952",
hostingCost: 4201,
walletIndex: 36,
bchAddr: "bchtest:qqymyk4zfvnw8rh6hcvl922ewudkyrn9jvk6gtuae6",
__v: 0
}
}
module.exports = {
uploadData,
paymentInfo
}
+37 -1
View File
@@ -65,7 +65,7 @@ describe(`#IPFS`, () => {
}
})
it("should return true if the file is uploaded", async () => {
it("should return file object if the file is uploaded", async () => {
try {
sandbox.stub(bchjs.IPFS.uppy, "upload").resolves(mockData.uploadData)
@@ -84,4 +84,40 @@ describe(`#IPFS`, () => {
}
})
})
describe("#getPaymentInfo", () => {
it("should return payment information", async () => {
sandbox
.stub(bchjs.IPFS.axios, "post")
.resolves({ data: mockData.paymentInfo })
const fileObj = {
schemaVersion: 1,
size: 2374,
fileId: "uppy-ipfs/js-1e-application/octet-stream",
fileName: "ipfs.js",
fileExtension: "js"
}
const result = await bchjs.IPFS.getPaymentInfo(fileObj)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.property(result, "success")
assert.property(result, "hostingCostBCH")
assert.property(result, "hostingCostUSD")
assert.property(result, "file")
assert.property(result.file, "payloadLink")
assert.property(result.file, "hasBeenPaid")
assert.property(result.file, "_id")
assert.property(result.file, "schemaVersion")
assert.property(result.file, "size")
assert.property(result.file, "fileId")
assert.property(result.file, "fileName")
assert.property(result.file, "fileExtension")
assert.property(result.file, "createdTimestamp")
assert.property(result.file, "hostingCost")
assert.property(result.file, "walletIndex")
assert.property(result.file, "bchAddr")
})
})
})