jquery-file-upload-middleware with express.js: how to move files & add watermarks? jquery-file-upload-middleware with express.js: how to move files & add watermarks? express express

jquery-file-upload-middleware with express.js: how to move files & add watermarks?


How to move the original file :

On jquery-file-upload-middleware website, it explains how to move a file, and if you read their documentation, how to move a file with a custom suffix (User ID, Session ID, etc.) :

app.use('/api', function (req, res, next) {            req.filemanager = upload.fileManager();            next();        });        app.use('/api/endpoint', function (req, res, next) {            // your real /api handler that will actually move the file            ...            // req.filemanager.move(filename, path, function (err, result))            req.filemanager.move('SomeFile.jpg', 'project1', function (err, result) {                // SomeFile.jpg gets moved from uploadDir/SomeFile.jpg to                // uploadDir/project1/SomeFile.jpg                // if path is relative (no leading slash), uploadUrl will                // be used to generate relevant urls,                // for absolute paths urls are not generated                if (!err) {                    // result structure                    // {                    //     filename: 'SomeFile.jpg',                    //     url: '/uploads/project1/SomeFile.jpg',

If you don't want to do this, (This post) explains how you can move a file from one location to another with node. I changed the unlinkSync() to unlink()

var fs = require('fs');//var util = require('util');var is = fs.createReadStream('source_file');var os = fs.createWriteStream('destination_file');is.pipe(os);is.on('end',function() {    fs.unlink('source_file', function(err){         // Continue execution         });});/* node.js 0.6 and earlier you can use util.pump:util.pump(is, os, function() {    fs.unlink('source_file', function(err){         // Continue execution    });});*/

Add a watermark to a file

This post explains how, with node, you can spawn a child process and use ImageMagick to add a watermark to an image :

    // Require our module dependenciesvar exec = require('child_process').exec;// Create command array to invoke ImageMagick composite where// -dissolve is the amount of transparency for the watermark// -gravity tells how to align images of varying size// -quality is the image quality of the JPEG (not required if producing PNG)var command = [    'composite',    '-dissolve', '50%',    '-gravity', 'center',     '-quality', 100,    pathToWatermarkJpg,    pathToImageJpg,    pathToResultJpg;];// Join command array by a space character and then execute commandexec(command.join(' '), function(err, stdout, stderr) {    // Do stuff with result here});