fix(ipfs): Adding getStatus() to check file upload status

This commit is contained in:
Chris Troutner
2020-05-21 19:57:49 -07:00
parent fa984e0fa6
commit 8220fee639
3 changed files with 113 additions and 1 deletions
+27
View File
@@ -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
+37 -1
View File
@@ -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
View File
@@ -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")
})
})
})