Skip to content

Commit

Permalink
fix(ipfs): Adding getStatus() to check file upload status
Browse files Browse the repository at this point in the history
  • Loading branch information
christroutner committed May 22, 2020
1 parent fa984e0 commit 8220fee
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 37 additions & 1 deletion test/unit/fixtures/ipfs-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
49 changes: 49 additions & 0 deletions test/unit/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
})

0 comments on commit 8220fee

Please sign in to comment.