Ejemplo n.º 1
0
e.upload = function upload(username, auth_token, stream, isVideo, cb) {
    var ts = ''+Date.now();
    isVideo = Number(!!isVideo);

    var mediaId = (username + uuid()).toUpperCase();
    var encrypt = spawn('openssl', ['enc', '-K', '4d3032636e5135314a69393776775434', '-aes-128-ecb']);
    encrypt.stdout.pause();
    stream.pipe(encrypt.stdin);

    var form = new FormStream();
    var req_token = e.hash(auth_token, ts);
    form.addField('req_token', req_token);
    form.addField('timestamp', ts);
    form.addStream('data', 'media', 'application/octet-stream', encrypt.stdout);
    form.addField('username', username);
    form.addField('media_id', mediaId);
    form.addField('type', isVideo);

    return Q.promise(function(resolve,reject) {
        var req = https.request({
            host: hostname,
            method: 'POST',
            path: '/ph/upload',
            headers: {
                'Content-type': 'multipart/form-data; boundary=' + form.getBoundary(),
                'User-Agent': user_agent,
            }
        }, function(res) {
            res.setEncoding('ascii');
            res.pipe(sink().on('data', function(data) {
                if (res.statusCode != 200) return reject(data);
                resolve(mediaId);
            }));
        });
	form.on('data', function(data) {
	    req.write(data);
	}).on('end', function(end) {
	    req.end(end);
	});
    }).nodeify(cb);;
};
Ejemplo n.º 2
0
Transloadit.prototype._multipart = function(options, cb) {
  var stream
    , json_params
    , hmac
    , expires
    ;

  options = options || {};
  options.params = options.params || {};
  options.streams = options.streams || {};
  options.files = options.files || {};
  stream = new MultipartStream();

  // add params
  options.params.auth = {
    key: this._key
  };
  if(options.sign) {
    expires = new Date();
    expires.setDate(expires.getDate() + 1);
    options.params.auth.expires = expires.toISOString();
    json_params = JSON.stringify(options.params);
    hmac = crypto.createHmac('sha1', this._secret);
    hmac.update(json_params);
    stream.addField('signature', hmac.digest('hex'));
  } else {
    json_params = JSON.stringify(options.params);
  }
  stream.addField('params', json_params);
  
  // add streams
  Object.keys(options.streams).forEach(function streamKey(key) {
    var currentStream = options.streams[key];
    stream.addStream(key, currentStream.filename, currentStream.mimeType, currentStream.stream);
  });

  // add files
  Object.keys(options.files).forEach(function fileKey(key) {
    var file = options.files[key];
    stream.addFile(key, file.path, file.filename);
  });

  // make streaming request
  stream.pipe(request.post({
    uri: 'http://' + API_HOST + options.path,
    headers: {
      'Content-Type': 'multipart/form-data; boundary=' + stream.getBoundary()
    }
  }, function handleResponse(err, response) {
    var body;
    
    if (err) return cb(err);

    response = response || {};
    response.body = response.body || '{}';
    
    try {
      body = JSON.parse(response.body);
      return cb(null, body);
    } catch (err) {
      return cb(err);
    }
  }));
};