<commit_before>var Card = require('core/models/card'),
    COLORS = ['#fc9a1b', '#f30059', '#12799c', '#f75213', '#8ac5a8', '#1aab26', '#15a58f'],
    PATTERNS = ['1', '2', '3', '4', '5', '6', '7', '8'],
    usedColors = [],
    usedPatterns = [];

module.exports = Card.extend({
  defaults: function() {
    return _.extend({
      type: 'file',
      is_uploading: false,
      upload_progress: 0,
      thumbnail_sm_path: null,
      thumbnail_md_path: null,
      thumbnail_lg_path: null
    }, Card.prototype.defaults);
  },

  localAttributes: function() {
    return _.union(
      ['is_uploading', 'upload_progress'], Card.prototype.localAttributes
    );
  },

  parse: function(response) {
    if (!response.thumbnail_xs_path ||
    !response.thumbnail_sm_path ||
    !response.thumbnail_md_path ||
    !response.thumbnail_lg_path && this.hasPreview()) {
      return _.omit(response, ['thumbnail_xs_path', 'thumbnail_sm_path',
      'thumbnail_md_path', 'thumbnail_lg_path']);
    } else {
      return response;
    }
  },

  getExtension: function() {
    return _.last(this.get('name').split('.')) ||
    _.last(this.get('mime_type').split('/'));
  },

  hasPreview: function() {
    return this.get('thumbnail_sm_path') !== null &&
    this.get('thumbnail_md_path') !== null &&
    this.get('thumbnail_lg_path') !== null;
  },

  getSmallPreview: function() {
    return this.get('thumbnail_sm_path');
  },

  getMediumPreview: function() {
    return this.get('thumbnail_md_path');
  },

  getLargePreview: function() {
    return this.get('thumbnail_lg_path');
  },

  getPreview: function(isDetail) {
    var color, image, pattern, unusedColors, unusedPatterns;

    isDetail = isDetail || false;

    if (this.hasPreview()) {
      if (isDetail) {
        image = this.getLargePreview();
      } else {
        image = this.get('featured')
          ? this.getMediumPreview()
          : this.getSmallPreview();
      }

      return {
        image: image
      };
    } else {
      if (!this.has('metadata') || !this.get('metadata').pattern) {
        this.generatePattern();
      }

      return {
        color: this.get('metadata').pattern.color,
        pattern: this.get('metadata').pattern.shape,
        extension: this.getExtension()
      };
    }
  },

  generatePattern: function() {
    unusedColors = _.difference(COLORS, usedColors);
    unusedPatterns = _.difference(PATTERNS, usedPatterns);

    if (unusedColors.length) {
      color = unusedColors[_.random(unusedColors.length - 1)]
      usedColors.push(color);
    } else {
      color = COLORS[_.random(COLORS.length - 1)]
      usedColors = [color];
    }

    if (unusedPatterns.length) {
      pattern = unusedPatterns[_.random(unusedPatterns.length - 1)]
      usedPatterns.push(pattern);
    } else {
      pattern = PATTERNS[_.random(PATTERNS.length - 1)]
      usedPatterns = [pattern];
    }

    this.save({
      metadata: {
        pattern: {
          color: color,
          shape: pattern
        }
      }
    });

    return this;
  },

  previewIsDataUrl: function(isDetail) {
    return this.hasPreview() && _.isDataUrl(this.getPreview(isDetail).image);
  }
});<commit_msg>Don’t save files without content.<commit_after>var Card = require('core/models/card'),
    COLORS = ['#fc9a1b', '#f30059', '#12799c', '#f75213', '#8ac5a8', '#1aab26', '#15a58f'],
    PATTERNS = ['1', '2', '3', '4', '5', '6', '7', '8'],
    usedColors = [],
    usedPatterns = [];

module.exports = Card.extend({
  defaults: function() {
    return _.extend({
      type: 'file',
      is_uploading: false,
      upload_progress: 0,
      thumbnail_sm_path: null,
      thumbnail_md_path: null,
      thumbnail_lg_path: null
    }, Card.prototype.defaults);
  },

  localAttributes: function() {
    return _.union(
      ['is_uploading', 'upload_progress'], Card.prototype.localAttributes
    );
  },

  parse: function(response) {
    if (!response.thumbnail_xs_path ||
    !response.thumbnail_sm_path ||
    !response.thumbnail_md_path ||
    !response.thumbnail_lg_path && this.hasPreview()) {
      return _.omit(response, ['thumbnail_xs_path', 'thumbnail_sm_path',
      'thumbnail_md_path', 'thumbnail_lg_path']);
    } else {
      return response;
    }
  },

  getExtension: function() {
    return _.last(this.get('name').split('.')) ||
    _.last(this.get('mime_type').split('/'));
  },

  hasPreview: function() {
    return this.get('thumbnail_sm_path') !== null &&
    this.get('thumbnail_md_path') !== null &&
    this.get('thumbnail_lg_path') !== null;
  },

  getSmallPreview: function() {
    return this.get('thumbnail_sm_path');
  },

  getMediumPreview: function() {
    return this.get('thumbnail_md_path');
  },

  getLargePreview: function() {
    return this.get('thumbnail_lg_path');
  },

  getPreview: function(isDetail) {
    isDetail = isDetail || false;

    if (this.hasPreview()) {
      if (isDetail) {
        image = this.getLargePreview();
      } else {
        image = this.get('featured')
          ? this.getMediumPreview()
          : this.getSmallPreview();
      }

      return {
        image: image
      };
    } else {
      if (!this.has('metadata') || !this.get('metadata').pattern) {
        this.generatePattern();
      }

      return {
        color: this.get('metadata').pattern.color,
        pattern: this.get('metadata').pattern.shape,
        extension: this.getExtension()
      };
    }
  },

  generatePattern: function() {
    var data, color, image, pattern, unusedColors, unusedPatterns;

    unusedColors = _.difference(COLORS, usedColors);
    unusedPatterns = _.difference(PATTERNS, usedPatterns);

    if (unusedColors.length) {
      color = unusedColors[_.random(unusedColors.length - 1)]
      usedColors.push(color);
    } else {
      color = COLORS[_.random(COLORS.length - 1)]
      usedColors = [color];
    }

    if (unusedPatterns.length) {
      pattern = unusedPatterns[_.random(unusedPatterns.length - 1)]
      usedPatterns.push(pattern);
    } else {
      pattern = PATTERNS[_.random(PATTERNS.length - 1)]
      usedPatterns = [pattern];
    }

    data = {
      metadata: {
        pattern: {
          color: color,
          shape: pattern
        }
      }
    };

    if (this.isNew()) {
      this.set(data);
    } else {
      this.save(data);
    }

    return this;
  },

  previewIsDataUrl: function(isDetail) {
    return this.hasPreview() && _.isDataUrl(this.getPreview(isDetail).image);
  }
});
{'fraction_non_alphanumeric': 0.1592346860052461, 'fraction_numerical': 0.012343774108933806, 'mean_word_length': 2.753329473074696, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6308058', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2099, 'n_words': 358}