Javascript http api, как получить список файлов каталога, определяемого его ha sh, с использованием file / ls api? - PullRequest
0 голосов
/ 11 апреля 2020

Я даю Ха sh некоторого локального каталога IPFS и использую fetch api , чтобы получить список файлов из ответа, как показано в коде ниже:

        .then (resp => {return resp.json()})
        .then (json => json.Objects)

что приводит к следующей иерархии Response :

Objects {…}
  QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV    {…}
     Hash   QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV
     Size   0
     Type   Directory
    Links   […]
       0    {…}
         Name   chose.dat
         Hash   QmUtAten38KKm8b7omXhmiJP1QT49mMWLeHJQK3yPnAmBr
         Size   9
         Type   File 
       1  etc...

Я застрял на уровне Objects и не могу достичь Ссылки уровень иерархии. Я не знаю, как обрабатывать уровень ха sh, какое ключевое слово должно использоваться?

Ответы [ 2 ]

1 голос
/ 12 апреля 2020

нормально работает curl http://127.0.0.1:5001/api/v0/file/ls?arg=/ipfs/QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV | sed -e 's/{/\n{/g' дает:

{"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"}]}}}

, поэтому вы можете получить доступ к списку файлов с помощью следующего фрагмента:

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>

Примечание: я получаю ключ ha sh из хеш-таблицы json .Arguments

1 голос
/ 12 апреля 2020

сначала я думаю, что проблема скорее состоит в том, чтобы понять, что API возвращает вам

Команда: curl http://127.0.0.1:5001/api/v0/ls?arg=QmSYQqCHX9LBbvfY86oBQGjCPpok4EAjPxUy7wrCWn8tuV | sed -e "s/{/\n{/g"

возвращает:

{"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":""}]}]}

, поэтому с микс карт и списков, вы должны быть осторожны, чтобы правильно проанализировать json, вот фрагмент кода html, который может сделать то, что вы хотите:

<!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>
...