feedhenry / fh-fhc

FeedHenry CLI, the Command Line Interface to FeedHenry
Other
26 stars 66 forks source link

'Request' based binary upload not working on all FH clusters #40

Closed dberesford closed 7 years ago

dberesford commented 11 years ago

See commit https://github.com/feedhenry/fh-fhc/commit/90bd8e17cefe0323a4cbd448d3f76aed3e863b2b.

To reproduce, see the two files below, newupload.js and oldupload.js. Tweak storeitems guid and target urls as appropriate when testing these (and run from the root fh-art directory).

The oldupload.js works on all clusters. newupload.js fails when run against CSG and LON3, but pass locally and in staging.

newupload.js:

var requestjs = require('request');
var fs = require('fs');
var util = require('util');
var path = require('path');

var headers =  { accept: 'application/json',
   cookie: 'feedhenry=9iSWBCOqW5dxpDWsDrIgPZGI;',
   'User-Agent': 'FHC/0.7.12-BUILD-NUMBER linux/2.6.32-38-server' };

var opts = {
   url: 'https://testing-csg1.feedhenry.com/box/srv/1.1/admin/storeitem/uploadbinary',
   headers: headers
};

var r = requestjs.post(opts, function(err, response, body){
  console.log("err: " + util.inspect(err))
  console.log("body: " + util.inspect(body))
});

// Get a handle on a form for Multi Part Form Upload
var form = r.form();

form.append('type', 'android');
form.append('guid', 'vxBCC7Q8UYDhOnXZcHFtdjgq');
form.append('file', fs.createReadStream('test/fixtures/android.apk.zip'));

oldupload.js

var requestjs = require('request');
var fs = require('fs');
var util = require('util');
var path = require('path');

  var headers =  { accept: 'application/json',
     cookie: 'feedhenry=9iSWBCOqW5dxpDWsDrIgPZGI;',
     'User-Agent': 'FHC/0.7.12-BUILD-NUMBER linux/2.6.32-38-server' };

  var opts = {
     //url: 'https://testing-csg1.feedhenry.com/box/srv/1.1/admin/storeitem/uploadbinary',
     host: 'testing-csg1.feedhenry.com',
     path: '/box/srv/1.1/admin/storeitem/uploadbinary',
     headers: headers
  };

var log = function() {
  function info(data){
    console.log(data); 
  }
  return { 
    info : info,
    silly : info
  };
}()

uploadFile(opts.url, 'test/fixtures/android.apk.zip', {'type': 'android', 'guid' : 'vxBCC7Q8UYDhOnXZcHFtdjgq'},'"application/octet-stream"', function(err, data){
  console.log("err: " + util.inspect(err))
  console.log("data: " + util.inspect(data))

});

function uploadFile(url, filepath, fields, contentType, cb){
  var boundary = Math.random();
  var post_data = [];
  var path = require('path');
  var filename = path.basename(filepath);
  for(var key in fields){
    post_data.push(new Buffer(EncodeFieldPart(boundary, key, fields[key]), 'ascii'));
  }

  post_data.push(new Buffer(EncodeFilePart(boundary, contentType, 'file', filename), 'ascii'));

  var file_reader = fs.createReadStream(filepath, {encoding: 'binary'});
  var file_contents = '';
  file_reader.on('data', function(data){
    file_contents += data;
  });
  file_reader.on('end', function(){
    post_data.push(new Buffer(file_contents, 'binary'));
    post_data.push(new Buffer("\r\n--" + boundary + "--"), 'ascii');
    doUploadFile(url, post_data, boundary, cb);
  });
}

// Field encoding
function EncodeFieldPart(boundary,name,value) {
    var return_part = "--" + boundary + "\r\n";
    return_part += "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
    return_part += value + "\r\n";
    return return_part;
}

// File encoding
function EncodeFilePart(boundary,type,name,filename) {
    var return_part = "--" + boundary + "\r\n";
    return_part += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n";
    return_part += "Content-Type: " + type + "\r\n\r\n";
    return return_part;
}

// do the actual upload
function doUploadFile(urlpath, post_data, boundary, cb) {

  var length = 0;

  for(var i = 0; i < post_data.length; i++) {
    length += post_data[i].length;
  }
  var url = require('url');
  var https = https || require('https');
  var post_options = {
    //host: url.parse(getFeedHenryUrl()).hostname,
    host: 'testing-csg1.feedhenry.com',
    port: '443',
    path: opts.path, //'/box/srv/1.1/admin/storeitem/uploadbinary',
    method: 'POST',
    headers : {
        'Content-Type' : 'multipart/form-data; boundary=' + boundary,
        'Content-Length' : length,
        //'Cookie' : "feedhenry=" + fhc.config.get("cookie") + ";"
        'Cookie' : opts.headers.cookie
    }
  };

  var post_request = https.request(post_options, function(response){
    response.setEncoding('utf8');
    var data = '';
    response.on('data', function(chunk){
      data = data + chunk;
    });
    response.on('end', function(){
      log.silly(data, 'app import');

      data = JSON.parse(data);
      return cb(undefined, data); 
    });
    response.on('error', function(err) {
      return cb(err);
    });
  });

  for (var i = 0; i < post_data.length; i++) {
    post_request.write(post_data[i]);
  }
  post_request.end();
}
lholmquist commented 9 years ago

This issue is 3 years old, is it still valid?

wtrocki commented 7 years ago

Closing as outdated.