class MaxMindReleaseUploader < CarrierWave::Uploader::Base
  # include CarrierWaveDirect::Uploader
  include CarrierWave::MimeTypes
  process :set_content_type

  after :store, :update_extras

  def initialize(*args)
    super
    JamRuby::UploaderConfiguration.set_aws_private_configuration(self)
  end

  # Add a white list of extensions which are allowed to be uploaded.
  def extension_white_list
    %w(zip csv)
  end

  def store_dir
    nil
  end

  # important; this code assumes that the mounted_as ends in _url, and the corresponding _md5 field has the same prefix
  # this is true for max_mind_release, but not necessarily other models; so careful copy/pasting
  def update_extras(file)
    mounted = mounted_as.to_s
    md5_field = mounted[0, mounted.rindex('_url')] + '_md5'
    size_field = mounted[0, mounted.rindex('_url')] + '_size'
    model[size_field.to_sym] = file.size
    model[md5_field.to_sym] = ::Digest::MD5.file(file).hexdigest

  end

  def filename
    if mounted_as.to_s == 'region_codes_url' || mounted_as.to_s == 'iso3166_url'
      ext = '.csv'
    else
      ext = '.zip'
    end

    File.join(model.store_dir, mounted_as.to_s + ext)
  end
end
