ipfs-inactive/js-ipfs-http-client

addFromFs not working with directory with multiple files

MohamedLEGH opened this issue · 2 comments

Hello,
When I push a directory with multiples files (and possibly folders inside the folder) and I try to get the data again with ipfs get hash, I only get one file instead of a list of files.

My code :

const ipfsClient = require("ipfs-http-client");
const ipfs = ipfsClient("localhost", "5001", { protocol: "http" }); 
(async function() {
  const results = await ipfs.addFromFs("folder", { recursive: true });
  const hash = results[0].hash;
  console.log(hash);

  files = await ipfs.get(hash);
  await ipfs.pin.add(hash);
  console.log("all files are");
  console.log(files);

})();

Return

QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve
all files are
[ { path: 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve',
    content: <Buffer 48 65 6c 6c 6f 20 77 6f 72 6c 64> } ]

I only get one file instead of all files inside the folder.

Any ideas how to solve this ?

The containing directory is the last entry in the array returned from addFromFs, not the first.

const ipfsClient = require("ipfs-http-client");
const ipfs = ipfsClient("localhost", "5001", { protocol: "http" });
(async function() {
  const results = await ipfs.addFromFs("folder", { recursive: true });
  const hash = results[results.length - 1].hash;
  console.log(hash);

  let files = await ipfs.get(hash);
  await ipfs.pin.add(hash);
  console.log("all files are");
  console.log(files);

})();

Returns:

QmWpVxmu6VjZHRU2r31JTVnQmQ8UYjTLTsKomTe9HLJJoo
all files are
[ { path: 'QmWpVxmu6VjZHRU2r31JTVnQmQ8UYjTLTsKomTe9HLJJoo' },
  { path:
     'QmWpVxmu6VjZHRU2r31JTVnQmQ8UYjTLTsKomTe9HLJJoo/commands.spec.js',
    content:
     <Buffer  ... > },
  { path:
     'QmWpVxmu6VjZHRU2r31JTVnQmQ8UYjTLTsKomTe9HLJJoo/constructor.spec.js',
    content:
     <Buffer 
  ... lots of stuff here

Hope that answers your question @MohamedLEGH - let us know if not!