Reputation: 1815
I give the Hash of some local IPFS directory and use the fetch api to get the list of files from the response as shown in the code below :
.then (resp => {return resp.json()})
.then (json => json.Objects)
which results in the following Response hierarchy :
Objects {…}
QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV {…}
Hash QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV
Size 0
Type Directory
Links […]
0 {…}
Name chose.dat
Hash QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr
Size 9
Type File
1 etc...
I got stuck at the Objects level and I am unable to reach the Links level of the hierarchy. I do not know how to handle the hash level, what keyword must be used ?
Upvotes: 0
Views: 1008
Reputation: 325
ok running curl http://127.0.0.1:5001/api/v0/file/ls?arg=/ipfs/QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV | sed -e 's/{/\n{/g'
gives :
{"Arguments":
{"/ipfs/QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV":"QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV"},"Objects":
{"QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV":
{"Hash":"QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV","Size":0,"Type":"Directory","Links":[
{"Name":"chose.dat","Hash":"QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr","Size":9,"Type":"File"},
{"Name":"chose.txt","Hash":"QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr","Size":9,"Type":"File"},
{"Name":"machi.dat","Hash":"QmPG68xD8CcrjcN5Efo3TM5PY77AMQiyZfAgR1snqauG3g","Size":499,"Type":"File"},
{"Name":"machin.chose","Hash":"QmUVmCWPJjaoxZ8d4XRVckyPaphzA1aKauNepZ5uR5rKkT","Size":111,"Type":"File"},
{"Name":"machine","Hash":"QmNTFx9vRLvMS6tC5m1MLqtt42j5LJKm4KQ8igdxWbRoqr","Size":1156,"Type":"File"},
{"Name":"s.txt","Hash":"QmeKMRaYxbP6r8wqeswtroXdQCAw72t9HxS4gAK6UvrnGF","Size":15,"Type":"File"},
{"Name":"sentence.txt","Hash":"QmPHrjJSuMw6TRFq9vWD6WkbzN6MwDFQeKLh9owTPstaDq","Size":17,"Type":"File"},
{"Name":"set.dat","Hash":"QmbkS4z9LH2LkBrooEQDMhZdUzBzqZ7waAVAAAhQRWXVwv","Size":2847,"Type":"File"},
{"Name":"si.txt","Hash":"QmRGebxmjHxxhAXjBgczU5QEWsNmBaKapV1TGsa4AY1mMt","Size":15,"Type":"File"},
{"Name":"simple.txt","Hash":"QmRGebxmjHxxhAXjBgczU5QEWsNmBaKapV1TGsa4AY1mMt","Size":15,"Type":"File"},
{"Name":"spot.dat","Hash":"QmZTerejEeCfijBv4y8CZqu6P8s2BUwyi7VpDBCPFE9sDd","Size":111,"Type":"File"},
{"Name":"spot.yml","Hash":"QmU1JhyC7Qegt29sg1o2u2pdb1XY43MZp6srJMciQLgNQk","Size":111,"Type":"File"},
{"Name":"string","Hash":"QmcUwH9vFa6mV1KaGuFjQEttdiKGRsUUE9CP5Aha8F37R6","Size":9,"Type":"File"},
{"Name":"temp","Hash":"QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr","Size":9,"Type":"File"},
{"Name":"toto","Hash":"QmRGebxmjHxxhAXjBgczU5QEWsNmBaKapV1TGsa4AY1mMt","Size":15,"Type":"File"},
{"Name":"truc","Hash":"QmZTerejEeCfijBv4y8CZqu6P8s2BUwyi7VpDBCPFE9sDd","Size":111,"Type":"File"},
{"Name":"truc.dat","Hash":"QmSkG1t12biXNHWzckFwDAXsdjtvybVH3UBfDJQquCnJ9y","Size":2847,"Type":"File"},
{"Name":"trucZ","Hash":"QmXYsmrQPmJc56Pk7SBQREYWp5kB8QLjtQWr8mEXRhNUaE","Size":111,"Type":"File"}]}}}
therefore you can access the file list with the following snippet :
let ipfs_path = '/ipfs/QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV';
let url = 'http://127.0.0.1:5001/api/v0/file/ls?arg='+ipfs_path;
console.log('url:'+url);
fetch(url, { method: "POST", mode: 'cors'})
.then (resp => {return resp.json()})
.then (readObj)
function readObj(json) {
console.dir(json);
let hash = json.Arguments[ipfs_path]
console.log('hash: ',hash)
let obj = json.Objects
let links = obj[hash].Links
let names = links.map ( e => e.Name )
console.log(names)
let buf = ''
for (let i=0; i<links.length; i++) {
file = links[i]
buf += '// file '+i+' :<br>name: '+file.Name
+'<br>hash: '+file.Hash
+ '<br>size: '+file.Size
+'<br>.'
}
document.getElementById('result').innerHTML = buf;
}
<div id=result></div>
Note: I get the hash key from the json.Arguments' hashtable
Upvotes: 1
Reputation: 325
first I think the problem is rather to understand what the API is returning you
The command: curl http://127.0.0.1:5001/api/v0/ls?arg=QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV | sed -e "s/{/\n{/g"
returns:
{"Objects":[
{"Hash":"QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV","Links":[
{"Name":"chose.dat","Hash":"QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr","Size":9,"Type":2,"Target":""},
{"Name":"chose.txt","Hash":"QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr","Size":9,"Type":2,"Target":""},
{"Name":"machi.dat","Hash":"QmPG68xD8CcrjcN5Efo3TM5PY77AMQiyZfAgR1snqauG3g","Size":499,"Type":2,"Target":""},
{"Name":"machin.chose","Hash":"QmUVmCWPJjaoxZ8d4XRVckyPaphzA1aKauNepZ5uR5rKkT","Size":111,"Type":2,"Target":""},
{"Name":"machine","Hash":"QmNTFx9vRLvMS6tC5m1MLqtt42j5LJKm4KQ8igdxWbRoqr","Size":1156,"Type":2,"Target":""},
{"Name":"s.txt","Hash":"QmeKMRaYxbP6r8wqeswtroXdQCAw72t9HxS4gAK6UvrnGF","Size":15,"Type":2,"Target":""},
{"Name":"sentence.txt","Hash":"QmPHrjJSuMw6TRFq9vWD6WkbzN6MwDFQeKLh9owTPstaDq","Size":17,"Type":2,"Target":""},
{"Name":"set.dat","Hash":"QmbkS4z9LH2LkBrooEQDMhZdUzBzqZ7waAVAAAhQRWXVwv","Size":2847,"Type":2,"Target":""},
{"Name":"si.txt","Hash":"QmRGebxmjHxxhAXjBgczU5QEWsNmBaKapV1TGsa4AY1mMt","Size":15,"Type":2,"Target":""},
{"Name":"simple.txt","Hash":"QmRGebxmjHxxhAXjBgczU5QEWsNmBaKapV1TGsa4AY1mMt","Size":15,"Type":2,"Target":""},
{"Name":"spot.dat","Hash":"QmZTerejEeCfijBv4y8CZqu6P8s2BUwyi7VpDBCPFE9sDd","Size":111,"Type":2,"Target":""},
{"Name":"spot.yml","Hash":"QmU1JhyC7Qegt29sg1o2u2pdb1XY43MZp6srJMciQLgNQk","Size":111,"Type":2,"Target":""},
{"Name":"string","Hash":"QmcUwH9vFa6mV1KaGuFjQEttdiKGRsUUE9CP5Aha8F37R6","Size":9,"Type":2,"Target":""},
{"Name":"temp","Hash":"QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr","Size":9,"Type":2,"Target":""},
{"Name":"toto","Hash":"QmRGebxmjHxxhAXjBgczU5QEWsNmBaKapV1TGsa4AY1mMt","Size":15,"Type":2,"Target":""},
{"Name":"truc","Hash":"QmZTerejEeCfijBv4y8CZqu6P8s2BUwyi7VpDBCPFE9sDd","Size":111,"Type":2,"Target":""},
{"Name":"truc.dat","Hash":"QmSkG1t12biXNHWzckFwDAXsdjtvybVH3UBfDJQquCnJ9y","Size":2847,"Type":2,"Target":""},
{"Name":"trucZ","Hash":"QmXYsmrQPmJc56Pk7SBQREYWp5kB8QLjtQWr8mEXRhNUaE","Size":111,"Type":2,"Target":""}]}]}
so with a mix of maps and lists you have to be careful to parse the json properly, here is a html code snippet that might do what you want:
<!DOCTYPE html>
<div id=result></div>
<script>
let ipfs_path = '/ipfs/QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV';
let url = 'http://127.0.0.1:5001/api/v0/ls?arg='+ipfs_path;
console.log('url:'+url);
fetch(url, { method: "POST", mode: 'cors'})
.then (resp => {return resp.json()})
.then (json => json.Objects)
.then (readObj)
function readObj(obj) {
console.dir(obj);
let buf = ''
for (let i=0; i<obj[0].Links.length; i++) {
let file=obj[0].Links[i];
console.log('// file '+i+': '+file.Hash)
buf += '// file '+i+':<br>name: '+file.Name+'<br>hash: '+file.Hash+'<br>size: '+file.Size+'<br>.';
}
document.getElementById('result').innerHTML = buf;
}
</script>
Upvotes: 2