server/api/helper_functions.js
// save drawing to an image file on-disk
save drawing to an image file on-disk
save drawing to an image file on - disk
function saveDrawingToDisk(dataURL, callback){
    callback = _.isFunction(callback) ? callback : function(){};
    if (!dataURL) { callback(false); return; }
    var regex = /^data:.+\/(.+);base64,(.*)$/;
    var matches = dataURL.match(regex);
    if (!_.isArray(matches) || !matches[1] || !matches[2]){
        console.log('Invalid dataURL:', dataURL);
        callback(false); return;
    }
    var ext = 'png';
    var data = matches[2];
    var buffer = new Buffer(data, 'base64');
    var random_string = (Date.now() + Math.random()).toString();
    var filename = crypto.createHash('md5').update(random_string).digest('hex');
    var filepath = '/drawings/' + filename + '.' + ext;
    fs.writeFile('./public' + filepath, buffer, function(err){
        if (err) { console.log('save drawing to disk err', err); callback(false); return; }
        callback(filepath);
    });
}
function saveDrawingToDisk ( dataURL , callback ) { callback = _ . isFunction ( callback ) ? callback : function ( ) { } ; if ( ! dataURL ) { callback ( false ) ; return ; } var regex = / ^data:.+\/(.+);base64,(.*)$ / ; var matches = dataURL . match ( regex ) ; if ( ! _ . isArray ( matches ) || ! matches [ 1 ] || ! matches [ 2 ] ) { console . log ( 'Invalid dataURL:' , dataURL ) ; callback ( false ) ; return ; } var ext = 'png' ; var data = matches [ 2 ] ; var buffer = new Buffer ( data , 'base64' ) ; var random_string = ( Date . now ( ) + Math . random ( ) ) . toString ( ) ; var filename = crypto . createHash ( 'md5' ) . update ( random_string ) . digest ( 'hex' ) ; var filepath = '/drawings/' + filename + '.' + ext ; fs . writeFile ( './public' + filepath , buffer , function ( err ) { if ( err ) { console . log ( 'save drawing to disk err' , err ) ; callback ( false ) ; return ; } callback ( filepath ) ; } ) ; }
save drawing to an image file on-disk
save drawing to an image file on - disk