kopia lustrzana https://gitlab.com/rysiekpl/libresilient
Rewrote the convoluted return statement of getContentFromGunAndIPFS into a linear code flow.
rodzic
6112e5ce01
commit
2d55e86d03
|
@ -216,32 +216,38 @@ if (typeof window === 'undefined') {
|
|||
}
|
||||
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, `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
|
||||
const file = await ipfs.get(ipfsaddr).next()
|
||||
|
||||
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()
|
||||
}
|
||||
return content
|
||||
|
||||
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;
|
||||
}
|
||||
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(
|
||||
|
||||
let blob = new Blob(
|
||||
[content],
|
||||
{'type': contentType}
|
||||
);
|
||||
{ type: contentType }
|
||||
)
|
||||
|
||||
return new Response(
|
||||
blob,
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -277,31 +285,30 @@ if (typeof window === 'undefined') {
|
|||
*
|
||||
* returns a Promise that resolves to an object mapping URLs to IPFS hashes
|
||||
*/
|
||||
let addToIPFS = (resources) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let addToIPFS = async resources => {
|
||||
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()
|
||||
|
||||
for(let res of resources) {
|
||||
LR.log(pluginName, " +-- handling internal resource:", res)
|
||||
|
||||
ipfs.add(Ipfs.urlSource(res))
|
||||
.then((result) => {
|
||||
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 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);
|
||||
})
|
||||
// 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
|
||||
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Ładowanie…
Reference in New Issue