From 8220fee639bb378b8fb05766b0d0bce1e0f55f4b Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 21 May 2020 19:57:49 -0700 Subject: [PATCH] fix(ipfs): Adding getStatus() to check file upload status --- src/ipfs.js | 27 ++++++++++++++++++ test/unit/fixtures/ipfs-mock.js | 38 ++++++++++++++++++++++++- test/unit/ipfs.js | 49 +++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/ipfs.js b/src/ipfs.js index eb44600..6214b29 100644 --- a/src/ipfs.js +++ b/src/ipfs.js @@ -158,6 +158,33 @@ class IPFS { throw err } } + + // Gets the status of the uploaded file. Will return an object that indicates + // the payment status. If the file is paid, it should contain an IPFS hash. + async getStatus(modelId) { + try { + if (!modelId) throw new Error(`Must include a file model ID.`) + + const fileData = await _this.axios.get( + `${this.IPFS_API}/files/${modelId}` + ) + // console.log(`fileData.data: ${JSON.stringify(fileData.data, null, 2)}`) + + const fileObj = { + hasBeenPaid: fileData.data.file.hasBeenPaid, + satCost: fileData.data.file.hostingCost, + bchAddr: fileData.data.file.bchAddr, + ipfsHash: fileData.data.file.payloadLink, + fileId: fileData.data.file._id, + fileName: fileData.data.file.fileName + } + + return fileObj + } catch (err) { + console.error(`Error in getStatus()`) + throw err + } + } } module.exports = IPFS diff --git a/test/unit/fixtures/ipfs-mock.js b/test/unit/fixtures/ipfs-mock.js index 0a04e24..25f628a 100644 --- a/test/unit/fixtures/ipfs-mock.js +++ b/test/unit/fixtures/ipfs-mock.js @@ -84,8 +84,44 @@ mockNewFileModel = { } } +const unpaidFileData = { + file: { + payloadLink: "", + hasBeenPaid: false, + _id: "5ec7392c2acfe57aa62e945a", + schemaVersion: 1, + size: 726, + fileName: "ipfs-e2e.js", + fileExtension: "js", + createdTimestamp: "1590114604.986", + hostingCost: 4403, + walletIndex: 56, + bchAddr: "bchtest:qz5z82u0suqh80x5tfx4ht8kdrkkw664vcy44uz0wk", + __v: 0 + } +} + +const paidFileData = { + file: { + payloadLink: "QmRDHPhY5hCNVRMVQvS2H9uty8P1skdwgLaHpUAkEvsjcE", + hasBeenPaid: true, + _id: "5ec7392c2acfe57aa62e945a", + schemaVersion: 1, + size: 726, + fileName: "ipfs-e2e.js", + fileExtension: "js", + createdTimestamp: "1590114604.986", + hostingCost: 4403, + walletIndex: 56, + bchAddr: "bchtest:qz5z82u0suqh80x5tfx4ht8kdrkkw664vcy44uz0wk", + __v: 0 + } +} + module.exports = { uploadData, paymentInfo, - mockNewFileModel + mockNewFileModel, + unpaidFileData, + paidFileData } diff --git a/test/unit/ipfs.js b/test/unit/ipfs.js index 11f5604..cc95458 100644 --- a/test/unit/ipfs.js +++ b/test/unit/ipfs.js @@ -143,4 +143,53 @@ describe(`#IPFS`, () => { } }) }) + + describe("#getStatus", () => { + it("should throw an error if modelId is not included", async () => { + try { + await bchjs.IPFS.getStatus() + + assert.equal(true, false, "Unexpected result") + } catch (err) { + //console.log(`err.message: ${err.message}`) + assert.include(err.message, `Must include a file model ID`) + } + }) + + it("should get data on an unpaid file", async () => { + const modelId = "5ec7392c2acfe57aa62e945a" + + sandbox + .stub(bchjs.IPFS.axios, "get") + .resolves({ data: mockData.unpaidFileData }) + + const result = await bchjs.IPFS.getStatus(modelId) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, "hasBeenPaid") + assert.property(result, "satCost") + assert.property(result, "bchAddr") + assert.property(result, "ipfsHash") + assert.property(result, "fileId") + assert.property(result, "fileName") + }) + + it("should get data on an unpaid file", async () => { + const modelId = "5ec7392c2acfe57aa62e945a" + + sandbox + .stub(bchjs.IPFS.axios, "get") + .resolves({ data: mockData.paidFileData }) + + const result = await bchjs.IPFS.getStatus(modelId) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, "hasBeenPaid") + assert.property(result, "satCost") + assert.property(result, "bchAddr") + assert.property(result, "ipfsHash") + assert.property(result, "fileId") + assert.property(result, "fileName") + }) + }) })