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.
Once you upload a file to ipfs
, it is theoretically available to anyone who knows its hash. So, it's not possible to restrict the access to the file.
What is possible, is to encrypt the file in some way and only give the decryption key/password to the group of people you want to be able to access it.
Have a look at this awesome ipfs resources.
The projects that seem to best suit your requirements are:
ipfs-add-from-encrypted: This script takes a file or directroy as input, uses tar if a directory and GPG AES256 symmetric encryption with the password you provide and adds it to IPFS and returns the IPFS hash.
ipfsecret: Encrypt and decrypt IPFS files with a secret passphrase.
For your second question: it would be best to have a locally running ipfs node, but fortunately that's not required. Recently, cloudflare launched a set of hosted ipfs gateways which anybody can use.
Best Answer
IPFS daemon exposes by default three services on three different ports:
The exception
Error: serveHTTPGateway: manet.Listen(/ip4/127.0.0.1/tcp/8080) failed: listen tcp4 127.0.0.1:8080: bind: address already in use
means that IPFS daemon can't start the gateway because you already have something running on this port.To find what program runs on the port 8080 an stop it, you can execute the following command: