diff --git a/src/ipfs.js b/src/ipfs.js index 45bcd77..d71cdd9 100644 --- a/src/ipfs.js +++ b/src/ipfs.js @@ -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 diff --git a/test/unit/fixtures/ipfs-mock.js b/test/unit/fixtures/ipfs-mock.js index 1dfe18b..8970768 100644 --- a/test/unit/fixtures/ipfs-mock.js +++ b/test/unit/fixtures/ipfs-mock.js @@ -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 } diff --git a/test/unit/ipfs.js b/test/unit/ipfs.js index 535326e..506ace8 100644 --- a/test/unit/ipfs.js +++ b/test/unit/ipfs.js @@ -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") + }) + }) })