I am writing a gallery system which currently shows a list of photos on a page and then gives the user the option to download a zip of the entire gallery. If this zip doesn't already exist, it is generated on-the-fly, but it can take a while to generate in it's current state (sometimes around a minute). This is the code I've got so far:
if (fs.existsSync(zipPath)) {
callback(downloadPath);
} else {
socket.emit('preparingZip');
fs.readdir(galleryPath, function(err, listOfPhotos) {
async.each(listOfPhotos, function(photo, cb) {
if (photo != 'thumbnails') {
var photoPath = galleryPath+'/'+photo;
fs.readFile(photoPath, function(err, data) {
if (err) { console.log(err); }
console.log(photoPath);
zip.file(photo, data);
cb();
});
} else {
cb();
}
}, function(err) {
if (err) { console.log(err); }
var data = zip.generate({base64:false,compression:'DEFLATE'});
fs.writeFile(zipPath, data, 'binary', function(err) {
if (err) { console.log(err); }
callback(downloadPath);
});
});
});
}
I have read that fs.writeFile shouldn't be used in this example, and that I should use fs.writeStream instead to make it a lot more efficient, but I can't find any examples of how to use fs.readFile & fs.writeStream together. Could someone please explain how I would do it?
Thanks in advance for any help.