Meteor.js — Download files form the internet using node packages
So, I was playing around with downloading images from the internet. After trying a lot, i came across this awesome approach. Here is the solution:
I am using ‘file-download’: ‘0.1.2’ node package to do this job.
I am using Telescope to work with the downloads.
Add the below line to the Npm dependencies of the package.js
Npm.depends({
‘file-download’: ‘0.1.2’
});
Now, let’s write our download.js file.
var download = Npm.require(‘file-download’);
var data = Npm.require(‘fs’); //If you wish to read the just downloaded file, you can make use of this module.var base = process.env.PWD
var DOWNLOAD_DIR = base+’/uploads’;
This is just the initial setup. Now, let’s add our download function.
//for more details on the npm package, please refer to - https://www.npmjs.com/package/file-downloadfunction downloadImage(url, title) {
// extracting the name of the file from the downloaded file.
var fileName = title + “.” + url.substr((url.lastIndexOf(‘.’) + 1));
var options = {
directory: DOWNLOAD_DIR,
filename: fileName
}download(url, options, function(err){
if (err) {
console.log(err);
throw err
}
});//Returning the downloaded file.return fileName;
}
Now that we have the download Image function, we can use it the way we want to, if needed we can expose it as a meteor method. Something like this.
Meteor.startup(function () {
Meteor.methods({
downloadImage: function(url, title){
return downloadImage(url, title); //this will return the filename
}
});
});
Note: You need to add the above Meteor.methods in a startup block, as all this is happening at the server end. For the client to have access to the Meteor.method, we need to add it to the startup.
You can also have methods to read the downloaded file.
function readDownloadedFile(filename) {
// Load your file
//Note - we have initialized DOWNLOAD_DIR as a global variable in the file.
var file = data.readFileSync(DOWNLOAD_DIR+filename);
}
You can also have a method to delete the downloaded file once you are done with it.
function deleteDownloadedFile(file) {
data.unlink(file, function(err) {
if (err) {
return console.error(err);
}
// console.log(“File deleted successfully!”);
});
}
Yes, it’s that simple. Now, lets also expose these as Meteor.methods, so they can be used anywhere from the client too. So, your final Meteor.methods block will look something like below.
Meteor.startup(function () {
Meteor.methods({
downloadImage: function(url, title){
return downloadImage(url, title);
//this will return the filename
},
readDownloadedFile: function(filename){
return readDownloadedFile(filename);
//this will return the read file
//You can also get the filename by downloading a new file.
//Remember downloadFile returns a filename, which can act as an input to read file. //return readDownloadedFile(downloadImage(url, title));},
deleteDownloadedeFile: function(file){
//Note the input is a complete filepath to locate the file.
deleteDownloadedFile(file); //You can also pass something like
//deleteDownloadedFile(DOWNLOAD_DIR+downloadImage(url, title)); } });
});
I hope downloading files with Meteor has gone a little simple for at least you!