<commit_before>/*
 * tus-jquery-client
 * https://github.com/tus/tus-jquery-client
 *
 * Copyright (c) 2013 Transloadit Ltd and Contributors
 * http://tus.io/
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

(function ($) {
  'use strict';

  // The Public API
  var tus = window.tus = {
    upload: function(file, options) {
      var upload = new ResumableUpload(file, options);
      if (file) {
        upload._start();
      }
      return upload;
    },
    fingerprint: function(file) {
      return 'tus-' + file.name + '-' + file.type + '-' + file.size;
    }
  };

  function ResumableUpload(file, options) {
    // The file to upload
    this.file = file;
    // Options for resumable file uploads
    this.options = {
      // The tus upload endpoint url
      endpoint: options.endpoint,

      // The fingerprint for the file.
      // Uses our own fingerprinting if undefined.
      fingerprint: options.fingerprint,

      // @TODO: second option: resumable: true/false
      // false -> removes resume functionality
      resumable: options.resumable !== undefined ? options.resetBefore : true,
      resetBefore: options.resetBefore,
      resetAfter: options.resetAfter
    };

    // The url of the uploaded file, assigned by the tus upload endpoint
    this.fileUrl = null;

    // Bytes sent to the server so far
    this.bytesWritten = null;

    // @TODO Add this.bytesTotal again

    // the jqXHR object
    this._jqXHR = null;

    // Create a deferred and make our upload a promise object
    this._deferred = $.Deferred();
    this._deferred.promise(this);
  }

  // Creates a file resource at the configured tus endpoint and gets the url for it.
  ResumableUpload.prototype._start = function() {
    var self = this;

    // Optionally resetBefore
    if (!self.options.resumable || self.options.resetBefore === true) {
      self._urlCache(false);
    }

    if (!(self.fileUrl = self._urlCache())) {
      self._post();
    } else {
      self._head();
    }
  };

  ResumableUpload.prototype._post = function() {
    var self    = this;
    var options = {
      type: 'POST',
      url: self.options.endpoint,
      headers: {
        'Final-Length': self.file.size
      }
    };

    $.ajax(options)
      .fail(function(jqXHR, textStatus, errorThrown) {
        // @todo: Implement retry support
        self._emitFail('Could not post to file resource ' +
          self.options.endpoint + '. ' + textStatus);
      })
      .done(function(data, textStatus, jqXHR) {
        var location = jqXHR.getResponseHeader('Location');
        if (!location) {
          return self._emitFail('Could not get url for file resource. ' + textStatus);
        }

        self.fileUrl = location;
        self._uploadFile(0, self.file.size - 1);
      });
  };

  ResumableUpload.prototype._head = function() {
    var self    = this;
    var options = {
      type: 'HEAD',
      url: this.fileUrl,
      cache: false
    };

    console.log('Resuming known url ' + this.fileUrl);
    $.ajax(options)
      .fail(function(jqXHR, textStatus, errorThrown) {
        // @TODO: Implement retry support
        self._emitFail('Could not head at file resource: ' + textStatus);
      })
      .done(function(data, textStatus, jqXHR) {<commit_msg>handle 404 on head check<commit_after>/*
 * tus-jquery-client
 * https://github.com/tus/tus-jquery-client
 *
 * Copyright (c) 2013 Transloadit Ltd and Contributors
 * http://tus.io/
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

(function ($) {
  'use strict';

  // The Public API
  var tus = window.tus = {
    upload: function(file, options) {
      var upload = new ResumableUpload(file, options);
      if (file) {
        upload._start();
      }
      return upload;
    },
    fingerprint: function(file) {
      return 'tus-' + file.name + '-' + file.type + '-' + file.size;
    }
  };

  function ResumableUpload(file, options) {
    // The file to upload
    this.file = file;
    // Options for resumable file uploads
    this.options = {
      // The tus upload endpoint url
      endpoint: options.endpoint,

      // The fingerprint for the file.
      // Uses our own fingerprinting if undefined.
      fingerprint: options.fingerprint,

      // @TODO: second option: resumable: true/false
      // false -> removes resume functionality
      resumable: options.resumable !== undefined ? options.resetBefore : true,
      resetBefore: options.resetBefore,
      resetAfter: options.resetAfter
    };

    // The url of the uploaded file, assigned by the tus upload endpoint
    this.fileUrl = null;

    // Bytes sent to the server so far
    this.bytesWritten = null;

    // @TODO Add this.bytesTotal again

    // the jqXHR object
    this._jqXHR = null;

    // Create a deferred and make our upload a promise object
    this._deferred = $.Deferred();
    this._deferred.promise(this);
  }

  // Creates a file resource at the configured tus endpoint and gets the url for it.
  ResumableUpload.prototype._start = function() {
    var self = this;

    // Optionally resetBefore
    if (!self.options.resumable || self.options.resetBefore === true) {
      self._urlCache(false);
    }

    if (!(self.fileUrl = self._urlCache())) {
      self._post();
    } else {
      self._head();
    }
  };

  ResumableUpload.prototype._post = function() {
    var self    = this;
    var options = {
      type: 'POST',
      url: self.options.endpoint,
      headers: {
        'Final-Length': self.file.size
      }
    };

    $.ajax(options)
      .fail(function(jqXHR, textStatus, errorThrown) {
        // @todo: Implement retry support
        self._emitFail('Could not post to file resource ' +
          self.options.endpoint + '. ' + textStatus);
      })
      .done(function(data, textStatus, jqXHR) {
        var location = jqXHR.getResponseHeader('Location');
        if (!location) {
          return self._emitFail('Could not get url for file resource. ' + textStatus);
        }

        self.fileUrl = location;
        self._uploadFile(0, self.file.size - 1);
      });
  };

  ResumableUpload.prototype._head = function() {
    var self    = this;
    var options = {
      type: 'HEAD',
      url: this.fileUrl,
      cache: false
    };

    console.log('Resuming known url ' + this.fileUrl);
    $.ajax(options)
      .fail(function(jqXHR, textStatus, errorThrown) {
        // @TODO: Implement retry support
        if(jqXHR.status == 404){
          // not valid, not on server, start with post request and restart
          // upload
          self._post();
        }else{
          self._emitFail('Could not head at file resource: ' + textStatus);
        }
      })
      .done(function(data, textStatus, jqXHR) {
{'fraction_non_alphanumeric': 0.12297496318114874, 'fraction_numerical': 0.002650957290132548, 'mean_word_length': 2.260201632261162, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2079798', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2022, 'n_words': 548}