JavaScript addAll folder CID

Hi,

I am using:

const ipfs = await IpfsCore.create();
await ipfs.addAll(IPFSArray,{ pin: true, wrapWithDirectory: true});

How can I get the CID of the folder?

Thanks,

Ken

1 Like

Hey Ken,

The add or addAll method will return information about each file we’ve added, as well as details about any directory it had to create in order to add the files with the correct paths. In your example, the addAll method would return an array of n+1 elements where n is the number of files in IPFSArray, one for each file added and another for the wrapping directory created.

Check out the ProtoSchool guide for more information.

Hi Daniel,

I do get that but the path is empty. for all of them. if I remove wrapWithDirectory I then get the path but obviously do not then get the extra one.

Thanks,

Ken

What are you trying to do?

var ipfs;
async function main() {
	ipfs = await window.IpfsCore.create();
	const files = [{
		path: '/tmp/myfile.txt',
		content: 'ABC'
	},{
		path: 'tmp2',
		content: 'DEF'
	
	}];

	for await (const result of ipfs.addAll(files, {wrapWithDirectory: true})) {
		console.log(result);
	}
}
main()

This returns 4 objects, one with a blank path:

Object { path: "tmp/myfile.txt", cid: {…}, size: 11, mode: 420, mtime: undefined }
Object { path: "tmp2", cid: {…}, size: 11, mode: 420, mtime: undefined }
Object { path: "tmp", cid: {…}, size: 67, mode: 493, mtime: undefined }
Object { path: "", cid: {…}, size: 173, mode: 493, mtime: undefined }

tmp/myfile.txt contains “ABC”, tmp2 contains “DEF”, tmp contains myfile.txt (which contains “ABC”), and the empty path contains tmp and tmp2. The corresponding CIDs for these are how you access the data inside of them. The path is empty because it’s the root directory wrapping all the contents specified, it’d be the same as the path being /.

Docs: https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#ipfsaddallsource-options

Thanks. It looks like there is something wrong with the array I am creating. I’ll come back to you if I cannot resolve it.

The problem I am having is that the content of the imagesI in the array I am trying to upload are base64 png’s. How can I upload these?

You could convert the images from base64 to binary before you store them. Alternatively you could convert the images back from base64 after retrieval if base64 is a requirement for some reason.

If you need help with encoding/decoding, check out this article: How To Encode and Decode Strings with Base64 in JavaScript | DigitalOcean

Thanks. I managed to resolve this.

2 Likes

How were you able to resolve this?

It’d be great if you can share your solution for others who may stumble across this issue.

1 Like

I had to use this function amd pass it the base64 string for each image I wanted to send

  function ConvData(Data) {
        var byteString = atob(Data.split(',')[1]);
        var ab = new ArrayBuffer(byteString.length);
        var ia = new Uint8Array(ab);
        for (var i = 0; i < byteString.length; i++) {
          ia[i] = byteString.charCodeAt(i);
        }
        return new Blob([ab], { type: "image/png" });
  }
1 Like