3

I wanted to check that is there any way i can download the IPFS file without running the local demon. I am working on a Dapp where i will be uploading the files over IPFS and sending the link of it to the respective team. I want that team to download the file.

  • You can use Infura's IPFS service – Henk Aug 29 '18 at 9:19
2

You can add the npm library 'ipfs' in your DApp.

https://www.npmjs.com/package/ipfs

lib/ipfs.js

const IPFS = require('ipfs');
const node = new IPFS();

 /**
 * Function that uploads file onto IPFS
 * @param {JSON} fileDetails Json containing file details that needs to be 
   updated on IPFS
 * @param {function} callback callback function
 */
const uploadFileToIPFS = function(fileDetails, callback) {
    let fileContent = fs.readFileSync(fileDetails.path);
    node.files.add({
        path: fileDetails.originalname,
        content: fileContent
    }, (err, filesAdded) => {
        if (err) {
            return callback(err);
        }
        callback(null, {
            hash: filesAdded[0].hash
        });
    });
};


/**
 * Function that gets the document contents from IPFS, based on the document's hash
 * @param {String} ipfsHash IPFS hash of the document that is being uploaded onto IPFS
 * @param {function} callback callback function
 */
const getFileContentsFromIPFS = function(ipfsHash, callback) {
    node.files.cat(ipfsHash, function(err, data) {
        callback(err, data);
    });
};

routes/ipfs.js

// API that gets the documents from IPFS
router.get('/api/to/get/document/from/ipfs', function(req, res){
    ipfs.getFileContentsFromIPFS(req.query.ipfsHash, function(err, result){
        if (err) {
            res.json({
                status: 'error',
                message: 'Something went wrong while fetching document from IPFS'
            });
        } else {
            //Get the file type
            let isImageOrPdf = fileType(result);
            if (isImageOrPdf.ext == 'pdf') {
                res.writeHead(200,{
                    'Content-type': 'application/pdf'
                });
                res.end(result);
            } else if(isImageOrPdf.ext == 'png' || isImageOrPdf.ext == 'jpg' ){
                res.writeHead(200, {'Content-Type' : 'image/png'});
                res.end(result);
            }
        }
    });
});

And instead of sending the link, send the ipfsHash of the document and whenever they want to download the file, get the ipfsHash and hit the aboove API and that file will be downloaded.

  • Hi...what is ipfs.getFileContentsFromIPFS. I am getting error as ipfs is not declared. – SSS Sep 2 '18 at 11:06
  • You have to require lib/ipfs.js file in routes/ipfs.js, then you will not get the error. In routes/ipfs.js const ipfs = require(''/path/to/lib/ipfs.js); – doraemon Sep 3 '18 at 6:23
  • ok...thanks. Now i am wondering how do we get the file back in its original form. I mean "result" is not in the same format which i uploaded. I am getting result but how do i convert it in its original format. Looks like i am missing something.. Thanks a lot for your help. – SSS Sep 5 '18 at 4:15
  • The res object in getFileContentFromIpfs will get the file in its original format depending on the file extension. – doraemon Sep 5 '18 at 4:19
0

Try http gatewayCloudflare IPFS gateway - IPFS news

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.