index.js
function addFileResource(cmd, file, params, callback) {
    if( !file ) return callback({error:true,message:"no file provided"});
    if( !fs.existsSync(file) ) return callback({error:true,message:"no file found: "+file});
    if( !fs.statSync(file).isFile() ) return callback({error:true,message:"not a file: "+file});
    if( !params ) return callback({error:true,message:"no resource parameters provided"});
    if( !params.package_id ) return callback({error:true,message:"no package_id provided"});

    // clean up filename
    var name = getCkanFilename(file);
    var folder = new Date().toISOString()+'/'+name;
    
    // set default / known parameters
    params.url = server+"/storage/f/"+encodeURIComponent(folder);
    if( !params.name ) params.name = file.replace(/.*\//,'');
    params.size = fs.statSync(file).size;
    params.resource_type = "file.upload";
    
    exports.exec(cmd, params, function(err, pkg){
        if( err ) return callback(err);
        
        rest.post(server + '/storage/upload_handle', {
            multipart: true,
            headers : {
                Authorization : key
            },
            data: {
              key: folder,
              file: rest.file(file, null, fs.statSync(file).size, null, params.mimetype)
            }
        }).on('complete', function(data) {
            // HACK: redirects seem to fire complete twice.  this is bad
            if( !callback ) return;
            
            if (data instanceof Error) {
                callback(data);
            } else {
                callback(null, data);
            }
            callback = null;
        });
    });

}
function addFileResource(cmd, file, params, callback) {
    if( !file ) return callback({error:true,message:"no file provided"});
    if( !fs.existsSync(file) ) return callback({error:true,message:"no file found: "+file});
    if( !fs.statSync(file).isFile() ) return callback({error:true,message:"not a file: "+file});
    if( !params ) return callback({error:true,message:"no resource parameters provided"});
    if( !params.package_id ) return callback({error:true,message:"no package_id provided"});

    // clean up filename
    var name = getCkanFilename(file);
    var folder = new Date().toISOString()+'/'+name;
    
    // set default / known parameters
    params.url = server+"/storage/f/"+encodeURIComponent(folder);
    if( !params.name ) params.name = file.replace(/.*\//,'');
    params.size = fs.statSync(file).size;
    params.resource_type = "file.upload";
    
    exports.exec(cmd, params, function(err, pkg){
        if( err ) return callback(err);
        
        rest.post(server + '/storage/upload_handle', {
            multipart: true,
            headers : {
                Authorization : key
            },
            data: {
              key: folder,
              file: rest.file(file, null, fs.statSync(file).size, null, params.mimetype)
            }
        }).on('complete', function(data) {
            // HACK: redirects seem to fire complete twice.  this is bad
            if( !callback ) return;
            
            if (data instanceof Error) {
                callback(data);
            } else {
                callback(null, data);
            }
            callback = null;
        });
    });

}
function addFileResource ( cmd , file , params , callback ) { if ( ! file ) return callback ( { error : true , message : "no file provided" } ) ; if ( ! fs . existsSync ( file ) ) return callback ( { error : true , message : "no file found: " + file } ) ; if ( ! fs . statSync ( file ) . isFile ( ) ) return callback ( { error : true , message : "not a file: " + file } ) ; if ( ! params ) return callback ( { error : true , message : "no resource parameters provided" } ) ; if ( ! params . package_id ) return callback ( { error : true , message : "no package_id provided" } ) ; // clean up filename var name = getCkanFilename ( file ) ; var folder = new Date ( ) . toISOString ( ) + '/' + name ; // set default / known parameters params . url = server + "/storage/f/" + encodeURIComponent ( folder ) ; if ( ! params . name ) params . name = file . replace ( / .*\/ / , '' ) ; params . size = fs . statSync ( file ) . size ; params . resource_type = "file.upload" ; exports . exec ( cmd , params , function ( err , pkg ) { if ( err ) return callback ( err ) ; rest . post ( server + '/storage/upload_handle' , { multipart : true , headers : { Authorization : key } , data : { key : folder , file : rest . file ( file , null , fs . statSync ( file ) . size , null , params . mimetype ) } } ) . on ( 'complete' , function ( data ) { // HACK: redirects seem to fire complete twice.  this is bad if ( ! callback ) return ; if ( data instanceof Error ) { callback ( data ) ; } else { callback ( null , data ) ; } callback = null ; } ) ; } ) ; }
todo, check for read access
todo check for read access