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);
return getGunData(gunaddr).then(ipfsaddr => {
LR.log(pluginName, `starting IPFS lookup of: '${ipfsaddr}'`);
LR.log(pluginName, `ipfs is: '${ipfs}'`);
return 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}
);
const ipfsaddr = await getGunData(gunaddr)
LR.log(pluginName, `starting IPFS lookup of: '${ipfsaddr}'`);
LR.log(pluginName, `ipfs is: '${ipfs}'`);
const file = await ipfs.get(ipfsaddr).next()
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,
{
'status': 200,
@ -255,10 +261,12 @@ if (typeof window === 'undefined') {
'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
* accepts an array of URLs
*
* returns a Promise that resolves to an object mapping URLs to IPFS hashes
*/
let addToIPFS = (resources) => {
return new Promise((resolve, reject) => {
LR.log(pluginName, "adding to IPFS...")
LR.log(pluginName, "+-- number of resources:", resources.length)
var ipfs_addresses = new Map();
/**
* adding stuff to IPFS
* accepts an array of URLs
*
* returns a Promise that resolves to an object mapping URLs to IPFS hashes
*/
let addToIPFS = async resources => {
LR.log(pluginName, "adding to IPFS...")
LR.log(pluginName, "+-- number of resources:", resources.length)
resources.forEach(function(res){
LR.log(pluginName, " +-- handling internal resource:", res)
ipfs.add(Ipfs.urlSource(res))
.then((result) => {
// add to the list -- this is needed to add stuff to Gun
// 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)
var abs_path = res.replace(window.location.origin, '')
ipfs_addresses.set(abs_path, '/ipfs/' + result.cid.string)
LR.log(pluginName, "added to IPFS: " + abs_path + ' as ' + ipfs_addresses.get(abs_path))
// if we seem to have all we need, resolve!
if (ipfs_addresses.size === resources.length) resolve(ipfs_addresses);
})
});
})
let ipfsAddresses = new Map()
for(let res of resources) {
LR.log(pluginName, " +-- handling internal resource:", res)
let result = await ipfs.add(Ipfs.urlSource(res))
// add to the list -- this is needed to add stuff to Gun
// result.path is just the filename stored in IPFS, not the actual path!
// res holds the full URL
// what we need in ipfsAddresses is in fact the absolute path (no domain, no scheme)
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
* is, in fact, available in IPFS