Rewrote the convoluted return statement of getContentFromGunAndIPFS into a linear code flow.

merge-requests/10/merge
CSDUMMI 2021-11-29 20:53:53 +00:00 zatwierdzone przez Michał "rysiek" Woźniak
rodzic 6112e5ce01
commit 2d55e86d03
1 zmienionych plików z 68 dodań i 61 usunięć

Wyświetl plik

@ -216,34 +216,40 @@ if (typeof window === 'undefined') {
} }
LR.log(pluginName, " +-- guessed contentType : " + contentType); LR.log(pluginName, " +-- guessed contentType : " + contentType);
return getGunData(gunaddr).then(ipfsaddr => { const ipfsaddr = await getGunData(gunaddr)
LR.log(pluginName, `starting IPFS lookup of: '${ipfsaddr}'`); LR.log(pluginName, `starting IPFS lookup of: '${ipfsaddr}'`);
LR.log(pluginName, `ipfs is: '${ipfs}'`); LR.log(pluginName, `ipfs is: '${ipfs}'`);
return ipfs.get(ipfsaddr).next(); const file = await ipfs.get(ipfsaddr).next()
}).then(file => {
// we only need one
if (file.value.content) {
async function getContent(source) {
var content = new Uint8Array()
var data = await source.next()
while (! data.done) {
var newContent = new Uint8Array(content.length + data.value.length);
newContent.set(content)
newContent.set(data.value, content.length)
content = newContent
data = await source.next()
}
return content
}
return getContent(file.value.content).then((content)=>{
LR.log(pluginName, `got a Gun-addressed IPFS-stored file: ${file.value.path}\n+-- content is: ${typeof content}`);
// creating and populating the blob
var blob = new Blob(
[content],
{'type': contentType}
);
return new Response( if(file.value.content) {
let source = file.value.content
let chunks = []
let contentLength = 0;
let data = await source.next()
while(!data.done) {
chunks.push(data.value)
contentLength += data.value.length
data = await source.next()
}
LR.log(pluginName, `Chunks: ${chunks}`)
let content = new Uint8Array(contentLength)
let currentLength = 0;
for(let i = 0; i < chunks.length; i++) {
content.set(chunks[i], currentLength)
currentLength += chunks[i].length;
}
LR.log(pluginName, `got a Gun-addressed IPFS-stored file: ${file.value.path}\n+-- content is: ${typeof content}`);
let blob = new Blob(
[content],
{ type: contentType }
)
return new Response(
blob, blob,
{ {
'status': 200, 'status': 200,
@ -255,10 +261,12 @@ if (typeof window === 'undefined') {
'X-LibResilient-ETag': file.value.path 'X-LibResilient-ETag': file.value.path
} }
} }
); )
})
}; } else {
}); LR.log(pluginName, `Gun-addressed IPFS-stored file could not be resolved on IPFS:\nGun: ${gunaddr}\nIPFS: ${ipfsaddr}`)
return null
}
} }
@ -271,39 +279,38 @@ if (typeof window === 'undefined') {
*/ */
/** /**
* adding stuff to IPFS * adding stuff to IPFS
* accepts an array of URLs * accepts an array of URLs
* *
* returns a Promise that resolves to an object mapping URLs to IPFS hashes * returns a Promise that resolves to an object mapping URLs to IPFS hashes
*/ */
let addToIPFS = (resources) => { let addToIPFS = async resources => {
return new Promise((resolve, reject) => { LR.log(pluginName, "adding to IPFS...")
LR.log(pluginName, "+-- number of resources:", resources.length)
LR.log(pluginName, "adding to IPFS...")
LR.log(pluginName, "+-- number of resources:", resources.length)
var ipfs_addresses = new Map();
resources.forEach(function(res){ let ipfsAddresses = new Map()
LR.log(pluginName, " +-- handling internal resource:", res)
for(let res of resources) {
ipfs.add(Ipfs.urlSource(res)) LR.log(pluginName, " +-- handling internal resource:", res)
.then((result) => {
// add to the list -- this is needed to add stuff to Gun let result = await ipfs.add(Ipfs.urlSource(res))
// result.path is just the filename stored in IPFS, not the actual path!
// res holds the full URL
// what we need in ipfs_addresses is in fact the absolute path (no domain, no scheme) // add to the list -- this is needed to add stuff to Gun
var abs_path = res.replace(window.location.origin, '') // result.path is just the filename stored in IPFS, not the actual path!
ipfs_addresses.set(abs_path, '/ipfs/' + result.cid.string) // res holds the full URL
LR.log(pluginName, "added to IPFS: " + abs_path + ' as ' + ipfs_addresses.get(abs_path)) // what we need in ipfsAddresses is in fact the absolute path (no domain, no scheme)
// if we seem to have all we need, resolve!
if (ipfs_addresses.size === resources.length) resolve(ipfs_addresses); let absPath = res.replace(window.location.origin, '')
}) ipfsAddresses.set(absPath, "/ipfs/" + result.cid.toString())
LR.log(pluginName, "added to IPFS: " + absPath + " as " + ipfsAddresses.get(absPath))
});
})
} }
return ipfsAddresses
}
/** /**
* verification that content pushed to IPFS * verification that content pushed to IPFS
* is, in fact, available in IPFS * is, in fact, available in IPFS