fix(cid2json): Works with CID files and directories

This commit is contained in:
Chris Troutner
2025-02-09 06:12:47 -07:00
parent 9ae5e890ae
commit 8a70d6a169
2 changed files with 25 additions and 11 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
{
"password": "YksrA6beZTqSioEpHxZd",
"password": "spksihHDsyk3JUUkH3xt",
"email": "system@system.com",
"id": "67a8a4578de3cb65f3bbdbc0",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3YThhNDU3OGRlM2NiNjVmM2JiZGJjMCIsImlhdCI6MTczOTEwNTM2N30.YA9hkc0dUyNvKCLjWnW-L1J1UvCBvGKCVC8qkrKe4Xw"
"id": "67a8a8e25a3e45751f654a13",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3YThhOGUyNWEzZTQ1NzUxZjY1NGExMyIsImlhdCI6MTczOTEwNjUzMH0.ViWmWvfJF2UnM46pRXvl0sXThfP2dNlzrmzSxpWChew"
}
+22 -8
View File
@@ -177,27 +177,41 @@ class IpfsUseCases {
if (!fileMetadata.dataPinned) {
throw new Error(`CID ${cid} has not been pinned by ipfs-file-pin-service instance ${ipfsFileProvider}`)
}
console.log('fileMetadata: ', fileMetadata)
// console.log('fileMetadata: ', fileMetadata)
// Throw an error if this is not a JSON file
const filename = fileMetadata.filename
if (!filename.endsWith('.json')) {
throw new Error(`CID ${cid} does not resolve to a JSON file.`)
}
console.log('ping01')
// Retrieve the CID content and store it in a Buffer.
const helia = this.adapters.ipfs.ipfs
const fileChunks = []
console.log('ping02')
for await (const chunk of helia.fs.cat(cid)) {
fileChunks.push(chunk)
// list cid content. This is used to determine if the CID is a file or a directory.
const contentArray = []
for await (const file of helia.fs.ls(cid)) {
contentArray.push(file)
}
console.log('ping03')
// console.log('contentArray: ', contentArray)
try {
// Handle CIDs that are files.
for await (const chunk of helia.fs.cat(cid)) {
fileChunks.push(chunk)
}
} catch (err) {
// Handle CIDs that are directorys containing a JSON file. (TokenTiger.com tokens)
for await (const chunk of helia.fs.cat(`${cid}/${contentArray[0].name}`)) {
fileChunks.push(chunk)
}
}
const fileBuf = Buffer.concat(fileChunks)
console.log('ping04')
// Convert the Buffer into a string.
const jsonStr = fileBuf.toString()
console.log('ping05')
// Parse the string into a JSON object.
let json = null
try {