<commit_before>'use strict';

/**
 * @module providers
 */

var fs = require('fs');
var request = require('request');
var EventEmitter = require('events').EventEmitter;
var mime = require('mime');
var util = require('util');

/**
 * TODO
 *
 * @class ResumableUpload
 * @constructor
 */
function ResumableUpload() {
  Object.defineProperties(this, {

    /**
     * TODO
     *
     * @property byteCount
     * @type Number
     */
    byteCount: {value: 0, writable: true},

    /**
     * TODO
     *
     * @property tokens
     * @type Object
     */
    tokens: {value: {}, writable: true},

    /**
     * TODO
     *
     * @property filepath
     * @type String
     */
    filepath: {value: '', writable: true},

    /**
     * TODO
     *
     * @property metadata
     * @type Object
     */
    metadata: {value: {}, writable: true},

    /**
     * TODO
     *
     * @property retry
     * @type Number
     */
    retry: {value: -1, writable: true},

    /**
     * TODO
     *
     * @property host
     * @type String
     */
    host: {value: 'www.googleapis.com'},

    /**
     * TODO
     *
     * @property api
     * @type String
     */
    api: {value: '/upload/youtube/v3/videos'},

    /**
     * TODO
     *
     * @property stats
     * @type Object
     */
    stats: {value: {}, writable: true}
  });
}

util.inherits(ResumableUpload, EventEmitter);

/**
 * Inits the upload by POSTing google for an upload URL (saved to self.location).
 *
 * @method upload
 */
ResumableUpload.prototype.upload = function() {
  var self = this;
  var options = {
    url: 'https://' + self.host + self.api + '?uploadType=resumable&part=snippet,status,contentDetails',
    headers: {
      Host: self.host,
      Authorization: 'Bearer ' + self.tokens.access_token
    },
    body: JSON.stringify(self.metadata)
  };
  options.headers['Content-Length'] = Buffer.from(JSON.stringify(self.metadata)).length;
  options.headers['Content-Type'] = 'application/json';
  options.headers['X-Upload-Content-Length'] = self.stats.size;
  options.headers['X-Upload-Content-Type'] = mime.lookup(self.filepath);

  // Send request and start upload if success
  request.post(options, function(err, res, body) {
    if (body) {
      body = JSON.parse(body);

      if (body.error) {<commit_msg>server: fix server crash when uploading to youtube<commit_after>'use strict';

/**
 * @module providers
 */

var fs = require('fs');
var request = require('request');
var EventEmitter = require('events').EventEmitter;
var mime = require('mime');
var util = require('util');

/**
 * TODO
 *
 * @class ResumableUpload
 * @constructor
 */
function ResumableUpload() {
  Object.defineProperties(this, {

    /**
     * TODO
     *
     * @property byteCount
     * @type Number
     */
    byteCount: {value: 0, writable: true},

    /**
     * TODO
     *
     * @property tokens
     * @type Object
     */
    tokens: {value: {}, writable: true},

    /**
     * TODO
     *
     * @property filepath
     * @type String
     */
    filepath: {value: '', writable: true},

    /**
     * TODO
     *
     * @property metadata
     * @type Object
     */
    metadata: {value: {}, writable: true},

    /**
     * TODO
     *
     * @property retry
     * @type Number
     */
    retry: {value: -1, writable: true},

    /**
     * TODO
     *
     * @property host
     * @type String
     */
    host: {value: 'www.googleapis.com'},

    /**
     * TODO
     *
     * @property api
     * @type String
     */
    api: {value: '/upload/youtube/v3/videos'},

    /**
     * TODO
     *
     * @property stats
     * @type Object
     */
    stats: {value: {}, writable: true}
  });
}

util.inherits(ResumableUpload, EventEmitter);

/**
 * Inits the upload by POSTing google for an upload URL (saved to self.location).
 *
 * @method upload
 */
ResumableUpload.prototype.upload = function() {
  var self = this;
  var options = {
    url: 'https://' + self.host + self.api + '?uploadType=resumable&part=snippet,status,contentDetails',
    headers: {
      Host: self.host,
      Authorization: 'Bearer ' + self.tokens.access_token
    },
    body: JSON.stringify(self.metadata)
  };
  options.headers['Content-Length'] = Buffer.from(JSON.stringify(self.metadata)).length;
  options.headers['Content-Type'] = 'application/json';
  options.headers['X-Upload-Content-Length'] = self.stats.size;
  options.headers['X-Upload-Content-Type'] = mime.getType(self.filepath);

  // Send request and start upload if success
  request.post(options, function(err, res, body) {
    if (body) {
      body = JSON.parse(body);

      if (body.error) {
{'fraction_non_alphanumeric': 0.15881590319792566, 'fraction_numerical': 0.0012964563526361278, 'mean_word_length': 2.368995633187773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3021151', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1471, 'n_words': 327}