Search code examples
javascriptapihttpipfs

Javascript http api how to get the list of files of a directory defined by its hash using file/ls api?


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 ?


Solution

  • 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