<commit_before>var CoreObject   = require('core-object');
var Promise      = require('ember-cli/lib/ext/promise');
var SilentError  = require('ember-cli/lib/errors/silent');
var chalk        = require('chalk');

var white = chalk.white;
var green = chalk.green;

var EXPIRE_IN_2030 = new Date('2030');
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;

module.exports = CoreObject.extend({
  init: function() {
    if (!this.config) {
      return Promise.reject(new SilentError('You have to pass a config!'));
    }

    this.s3 = this.s3 || require('s3');
    this.client = this.s3.createClient({
      maxAsyncS3: 1,
      s3Options: this.config.assets
    });
  },

  upload: function() {
    var client = this.client;
    var _this  = this;

    if (!this.ui) {
      var message = 'You have to pass a UI to an adapter.';
      return Promise.reject(new SilentError(message));
    }

    this.ui.pleasantProgress.start(green('Uploading assets'), green('.'));

    return new Promise(function(resolve, reject) {
      var uploader = client.uploadDir(_this.getUploadParams());

      uploader.on('error', _this.logUploadError.bind(_this, reject));

      uploader.on('end', _this.logUploadSuccess.bind(_this, resolve));

      uploader.on('fileUploadStart', _this.logFileUpload.bind(_this));

      uploader.on('fileUploadEnd', _this.logFileUploadEnd.bind(_this, resolve));
    });
  },

  getUploadParams: function() {
    return {
      localDir: 'tmp/assets-sync',
      s3Params: {
        ACL: 'public-read',
        Bucket: this.config.assets.bucket,
        Prefix: this.config.assets.prefix || '',
        CacheControl: 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public',
        Expires: EXPIRE_IN_2030
      },
      getS3Params: this.getS3Params
    }
  },

  getS3Params: function(localFile, stat, callback) {
    var ext = localFile.replace(/.*[\.\/\\]/, '').toLowerCase();
    var isGzippedContent = ['js', 'css'].indexOf(ext) != -1;
    var additionalParams = isGzippedContent ? { ContentEncoding: 'gzip' } : {};

    callback(null, additionalParams);
  },

  logFileUpload: function(fullPath, _) {
    var fileNameMatches = fullPath.match(/\/((\w*[-]\w*||\w*)[.]\w*)/);
    if (fileNameMatches) {
      this.ui.writeLine('Uploading: ' + green(fileNameMatches[1]));
    }
    this.fileUploadPending = true;
  },

  logFileUploadEnd: function(resolve, localFilePath, _s3Key) {<commit_msg>Fix: gzipped SVGs were not marked correctly.<commit_after>var CoreObject   = require('core-object');
var Promise      = require('ember-cli/lib/ext/promise');
var SilentError  = require('ember-cli/lib/errors/silent');
var chalk        = require('chalk');

var white = chalk.white;
var green = chalk.green;

var EXPIRE_IN_2030 = new Date('2030');
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;

module.exports = CoreObject.extend({
  init: function() {
    if (!this.config) {
      return Promise.reject(new SilentError('You have to pass a config!'));
    }

    this.s3 = this.s3 || require('s3');
    this.client = this.s3.createClient({
      maxAsyncS3: 1,
      s3Options: this.config.assets
    });
  },

  upload: function() {
    var client = this.client;
    var _this  = this;

    if (!this.ui) {
      var message = 'You have to pass a UI to an adapter.';
      return Promise.reject(new SilentError(message));
    }

    this.ui.pleasantProgress.start(green('Uploading assets'), green('.'));

    return new Promise(function(resolve, reject) {
      var uploader = client.uploadDir(_this.getUploadParams());

      uploader.on('error', _this.logUploadError.bind(_this, reject));

      uploader.on('end', _this.logUploadSuccess.bind(_this, resolve));

      uploader.on('fileUploadStart', _this.logFileUpload.bind(_this));

      uploader.on('fileUploadEnd', _this.logFileUploadEnd.bind(_this, resolve));
    });
  },

  getUploadParams: function() {
    return {
      localDir: 'tmp/assets-sync',
      s3Params: {
        ACL: 'public-read',
        Bucket: this.config.assets.bucket,
        Prefix: this.config.assets.prefix || '',
        CacheControl: 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public',
        Expires: EXPIRE_IN_2030
      },
      getS3Params: this.getS3Params.bind(this)
    }
  },

  getS3Params: function(localFile, stat, callback) {
    var gzipExtensions = this.config.assets.gzipExtensions ? this.config.assets.gzipExtensions : ['js', 'css', 'svg'];
    var ext = localFile.replace(/.*[\.\/\\]/, '').toLowerCase();
    var isGzippedContent = gzipExtensions.indexOf(ext) != -1;
    var additionalParams = isGzippedContent ? { ContentEncoding: 'gzip' } : {};

    callback(null, additionalParams);
  },

  logFileUpload: function(fullPath, _) {
    var fileNameMatches = fullPath.match(/\/((\w*[-]\w*||\w*)[.]\w*)/);
    if (fileNameMatches) {
      this.ui.writeLine('Uploading: ' + green(fileNameMatches[1]));
    }
    this.fileUploadPending = true;
  },

  logFileUploadEnd: function(resolve, localFilePath, _s3Key) {
{'fraction_non_alphanumeric': 0.15791583166332665, 'fraction_numerical': 0.014428857715430862, 'mean_word_length': 3.3100172711571676, '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': '3155398', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1645, 'n_words': 314}