So I have been trying to watch for a successful file upload in NodeJS for a while. Sure, you can use something inotify, but the process is heavy when all your looking to do is emit an event in NodeJS when a file is successfully uploaded to a directory on your server.

For starters, I have vsftpd running on an ubuntu installation. I upload to a directory and NodeJS watches for when the file is complete. Then adds it to a queue of files to process for later. I needed something light and fast. Kinda like tail..wait wuh???!

So here’s the deal you can set up a tail child process in node like this:

var spawn = require('child_process').spawn,
    tail = spawn('tail', ['-f', '/var/log/vsftpd.log']);

This will sit and monitor all the data coming through the tail. it’s a persistent child process so not too expensive either.

You can watch the output coming through like this:

tail.stdout.on('data', function(data) {
    console.log("stdout" + data);

tail.stderr.on('data', function(data) {
    console.log("stderr" + data)

tail.on('close', function(code) {
    console.log('closed: ' + code)

The amazing part is that vsftpd logs an even when the file is successfully uploaded, so no more polling for files and determining whether they are complete, or setting up watches on files that have been created but not finished yet. So efficient. Just parse the log file for a line like “UPLOAD” and get the file name and “WHABAM!!!” you’re done.

Leave a Reply

Your email address will not be published. Required fields are marked *