<commit_before>
/**
 * 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;
<commit_msg>media: implement attrs in addFiles() method<commit_after>
/**
 * Module dependencies.
 */

var fs = require('fs');
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|Object|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++) {
    var f = files[i];

    // f != string
    if ('string' != typeof f) {
      // set attrs
      for (var k in f) {
        if ('file' != k) {
          var param = 'attrs[' + i + '][' + k + ']';
          params.formData.push([param, f[k]]);
        }
      }
      // set file path
      f = f.file;
    }

    // create stream if it's necessary
    if (!f._readableState) {
      f = fs.createReadStream(f);
    }

    params.formData.push(['media[]', f]);
  }

  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 url;

    if ('string' == typeof m) {
      url = m;
    } else {
      // add attributes
      body.attrs[i] = {};
      for (var k in m) {
        if ('url' != k) {
          body.attrs[i][k] = m[k];
        }
      }
      url = m[k];
    }

    // 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;

{'fraction_non_alphanumeric': 0.18887015177065766, 'fraction_numerical': 0.004047217537942664, 'mean_word_length': 2.4888235294117647, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5233166', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2125, 'n_words': 551}