
/**
 * Module dependencies.
 */

var debug = require('debug')('wpcom:media');

/**
 * Media methods
 *
 * @param {String} id
 * @param {String} sid site id
 * @param {WPCOM} wpcom
 * @api public
 */

function Media(id, sid, wpcom){
  if (!(this instanceof Media)) return new Media(id, sid, wpcom);

  this.wpcom = wpcom;
  this._sid = sid;
  this._id = id;

  if (!this._id) {
    debug('WARN: media id is not defined');
  }
}

/**
 * Get media
 *
 * @param {Object} [query]
 * @param {Function} fn
 * @api public
 */

Media.prototype.get = function(query, fn){
  var params = {
    apiVersion: '1.1',
    path: '/sites/' + this._sid + '/media/' + this._id
  };

  this.wpcom.sendRequest(params, query, null, fn);
};

/**
 * Edit media
 *
 * @param {Object} body
 * @param {Function} fn
 * @api public
 */

Media.prototype.update = function(body, fn){
  var params = {
    apiVersion: '1.1',
    path: '/sites/' + this._sid + '/media/' + this._id,
    method: 'post'
  };

  this.wpcom.sendRequest(params, null, body, fn);
};

/**
 * Add media file
 *
 * @param {String|Array} files
 * @param {Function} fn
 */

Media.prototype.addFiles = function(files, fn){
  var params = {
    apiVersion: '1.1',
    path: '/sites/' + this._sid + '/media/new',
    method: 'post',
    formData: []
  };

  // process formData
  files = Array.isArray(files) ? files : [ files ];
  for (var i = 0; i < files.length; i++) {
    params.formData.push(['media[]', files[i]]);
  }

  this.wpcom.sendRequest(params, null, null, fn);
};

/**
 * Add media files from URL
 *
 * @param {String|Array|Object} files
 * @param {Function} fn
 */

Media.prototype.addUrls = function(media, fn){
  var params = {
    apiVersion: '1.1',
    path: '/sites/' + this._sid + '/media/new',
    method: 'post'
  };

  var body = { media_urls: [], attrs: [] };

  // process formData
  media = Array.isArray(media) ? media : [ media ];
  for (var i = 0; i < media.length; i++) {
    var m = media[i];
    var attrs;
    var url;

    if ('string' == typeof m) {
      url = m;
    } else {
      attrs = m;
      url = m.url;
      delete attrs.url;

      // set attributes into [attrs]
      body.attrs[i] = attrs;
    }

    // push url into [media_url]
    body.media_urls.push(url);

  }

  this.wpcom.sendRequest(params, null, body, fn);
};

/**
 * Delete media
 *
 * @param {Function} fn
 * @api public
 */

Media.prototype['delete'] =
Media.prototype.del = function(fn){
  var params = {
    apiVersion: '1.1',
    path: '/sites/' + this._sid + '/media/' + this._id + '/delete',
    method: 'post'
  };

  this.wpcom.sendRequest(params, null, null, fn);
};

/**
 * Expose `Media` module
 */

module.exports = Media;

@@ -27,16 +27,40 @@
 s.%0A */%0A%0A
+var fs = require('fs');%0A
 var debu
@@ -418,18 +418,20 @@
 : media 
+%60
 id
+%60
  is not 
@@ -1086,24 +1086,31 @@
 ram %7BString%7C
+Object%7C
 Array%7D files
@@ -1386,18 +1386,17 @@
  : %5B
- 
 files
- 
 %5D;%0A
+%0A
   fo
@@ -1438,16 +1438,436 @@
 ) %7B%0A    
+var f = files%5Bi%5D;%0A%0A    // f != string%0A    if ('string' != typeof f) %7B%0A      // set attrs%0A      for (var k in f) %7B%0A        if ('file' != k) %7B%0A          var param = 'attrs%5B' + i + '%5D%5B' + k + '%5D';%0A          params.formData.push(%5Bparam, f%5Bk%5D%5D);%0A        %7D%0A      %7D%0A      // set file path%0A      f = f.file;%0A    %7D%0A%0A    // create stream if it's necessary%0A    if (!f._readableState) %7B%0A      f = fs.createReadStream(f);%0A    %7D%0A%0A    
 params.f
@@ -1892,23 +1892,16 @@
 ia%5B%5D', f
-iles%5Bi%5D
 %5D);%0A  %7D%0A
@@ -2405,23 +2405,8 @@
 i%5D;%0A
-    var attrs;%0A
     
@@ -2485,127 +2485,172 @@
     
-attrs = m;%0A      url = m.url;%0A      delete attrs.url;%0A%0A      // set attributes into %5Battrs%5D%0A      body.attrs%5Bi%5D = attrs
+// add attributes%0A      body.attrs%5Bi%5D = %7B%7D;%0A      for (var k in m) %7B%0A        if ('url' != k) %7B%0A          body.attrs%5Bi%5D%5Bk%5D = m%5Bk%5D;%0A        %7D%0A      %7D%0A      url = m%5Bk%5D
 ;%0A  
@@ -2718,17 +2718,16 @@
 h(url);%0A
-%0A
   %7D%0A%0A  t

implement attrs in addFiles() method