0

I am a newbie to both Javascript and ipfs and I am trying an experiment to fetch an image buffer from the ipfs hash "QmdD8FL7N3kFnWDcPSVeD9zcq6zCJSUD9rRSdFp9tyxg1n" using ipfs-mini node module.
Below is my code


const IPFS = require('ipfs-mini'); 
const FileReader = require('filereader');
var multer = require('multer');
const ipfs = initialize();

     app.post('/upload',function(req,res){
     upload(req,res, function(err){
        console.log(req.file.originalname);
        ipfs.cat('QmdD8FL7N3kFnWDcPSVeD9zcq6zCJSUD9rRSdFp9tyxg1n', function(err, data){
          if(err) console.log("could not get the image from the ipfs for hash " + ghash);
          else {
              var wrt = data.toString('base64');
              console.log('size ; ' + wrt.length);
              fs.writeFile('tryipfsimage.gif',wrt, (err) =>{
              if(err)console.log('can not write file');
              else {
                //console.log(data);
                ipfs.stat('QmdD8FL7N3kFnWDcPSVeD9zcq6zCJSUD9rRSdFp9tyxg1n', (err, data)=>{
                  //  console.log(hexdump(wrt));
                });
                console.log("files written successfully");
              }
          });
        }
      });
    });
    });
     function initialize() {
       console.log('Initializing the ipfs object');
       return new IPFS({
         host: 'ipfs.infura.io',
         protocol: 'https'
       });
     }

I could view the image properly in the browser using the link below "https://ipfs.io/ipfs/QmdD8FL7N3kFnWDcPSVeD9zcq6zCJSUD9rRSdFp9tyxg1n", but if I open the file 'tryipfsimage.gif' in which I dump the return buffer of the cat API in above program, the content of the image seems corrupted. I am not sure what the mistake I am doing in the code. it would be great If someone points me the mistake.

Srinivasan
  • 77
  • 1
  • 1
  • 5

1 Answers1

0

From ipfs docs https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#javascript---ipfsfilescatipfspath-callback

file in the callback is actually a Buffer so by toString('base64')'ing it you are writing actual base64 into the .gif file - no need to do this. you can pass the Buffer directly to the fs.writeFile api with

fs.writeFile('tryipsimage.gif', file, ...

For larger files I would recommend using the ipfs catReadableStream, where you can do something more like:

const stream = ipfs.catReadableStream('QmdD8FL7N3kFnWDcPSVeD9zcq6zCJSUD9rRSdFp9tyxg1n')

// don't forget to add error handlers to stream and whatnot
const fileStream = fs.createWriteStream('tryipsimage.gif')

stream.pipe(fileStream);
Catalyst
  • 3,143
  • 16
  • 20
  • Hello Sir, Thank you for the response, Firstly I tried writing the plain out buffer from the cat API response without any encoding format, but there is no luck in retrieving back the proper ".gif" file as I could see from the browser. while I tried the hexdump of the written file I could see that the initial 10 bytes are same as the original source file's hex content, but later on the hex value "bfef efbd bdbf " keep repeats between few actual values.Secondly, I could not see the "catReadableStream" API support in ipfs-mini. The reason I am strict with mini version is I have a space constraint – Srinivasan Mar 26 '18 at 14:13
  • Also please let me understand, whether Ipfs file stream API are the ultimate solution for dealing with the image buffers, if so could you please point me any useable sample code that could run on Node program instead of a browser. – Srinivasan Mar 26 '18 at 14:23
  • not sure I understand the the question for samples? This is node.js code. The streams do not matter for small files. I'm not sure what your problem is if writing the buffer directly does not give you the right output. Maybe ipfs is changing the encoding? – Catalyst Mar 26 '18 at 20:06