<commit_before>require 'mini_magick'
require 'fileutils'

# When presented with a gravity argument, one of the following values is
# possible.
# - NorthWest
# - North
# - NorthEast
# - West
# - Center (default)
# - East
# - SouthWest
# - South
# - SouthEast
class AssetImage
  def initialize(asset)
    @asset = asset
  end

  def filename(width = nil, height = nil, options = {})
    options[:action] = :resize_to_limit unless options.key?(:action)

    str = ''
    str << 'rtl' if options[:action].to_sym == :resize_to_limit
    str << "-q#{options[:quality]}" if options[:quality]
    # TODO add gravity if present
    str << "-#{width}x#{height}"
    str << "-#{@asset.read_attribute(:filename)}"
    str
  end

  def url(width = nil, height = nil, options = {})
    options[:action] = :resize_to_limit unless options.key?(:action)

    md5 = Digest::MD5.hexdigest(@asset.id.to_s)
    main_dir = md5[0,2]
    second_dir = md5[2,2]

    # if width/height are nil, you need to fetch the original file!

    if width.nil? && height.nil?
      return "/uploads/assets/#{main_dir}/#{second_dir}/#{@asset.read_attribute(:filename)}"
    end


    name = filename(width, height, options)

    unless File.exists?("#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{name}")
      FileUtils.mkpath("#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}")

      case options[:action].to_sym
        when :resize_to_limit
          resize_to_limit(width, height, options)
        else
          raise 'foo'
      end
    end

    "/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{name}"
  end

  def path(width = nil, height = nil, options = {})
    options[:action] = :resize_to_limit unless options.key?(:action)
    "#{Rails.root}/public#{url(width, height, options)}"
  end

  def resize_to_limit(width, height, options = {})
    options[:action] = :resize_to_limit

    md5 = Digest::MD5.hexdigest(@asset.id.to_s)
    main_dir = md5[0,2]
    second_dir = md5[2,2]

    name = filename(width, height, options)

    img = MiniMagick::Image.open(@asset.filename.path)
    img.combine_options do |c|
      c.quality(options[:quality]) if options[:quality]
      c.resize "#{width}x#{height}>"
    end

    img.write("#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{name}")
  end

  def resize_to_fit(width, height)
    # [width]x[height]

    # resize picture
    #
  end

  def resize_to_fill(width, height, gravity = 'Center')
    # [width]
    # OR
    # x[height]
  end

  def resize_and_pad(width, height, gravity = 'Center', background = :transparant)
    # [width]x[height]
    # :transparant background for png
    # :white background for jpg
  end
end<commit_msg>Add 2 asset resize actions.<commit_after>require 'mini_magick'
require 'fileutils'

# When presented with a gravity argument, one of the following values is
# possible.
# - NorthWest
# - North
# - NorthEast
# - West
# - Center (default)
# - East
# - SouthWest
# - South
# - SouthEast
class AssetImage
  def initialize(asset)
    @asset = asset
  end

  def filename(width = nil, height = nil, options = {})
    options[:action] = :resize_to_limit unless options.key?(:action)

    str = ''
    str << 'rtl' if options[:action].to_sym == :resize_to_limit
    str << "-q#{options[:quality]}" if options[:quality]
    # TODO add gravity if present
    str << "-#{width}x#{height}"
    str << "-#{@asset.actual_filename}"
    str
  end

  def url(width = nil, height = nil, options = {})
    options[:action] = :resize_to_limit unless options.key?(:action)

    if width.nil? && height.nil?
      return "/uploads/assets/#{main_dir}/#{second_dir}/#{@asset.actual_filename}"
    end

    name = filename(width, height, options)

    unless File.exists?("#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{name}")
      FileUtils.mkpath("#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}")

      case options[:action].to_sym
        when :resize_to_limit
          resize_to_limit(width, height, options)
        else
          resize_to_fit(width, height, options)
      end
    end

    "/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{name}"
  end

  def path(width = nil, height = nil, options = {})
    options[:action] = :resize_to_limit unless options.key?(:action)
    "#{Rails.root}/public#{url(width, height, options)}"
  end

  # Resize the image to fit within the specified dimensions while retaining
  # the original aspect ratio. Will only resize the image if it is larger than the
  # specified dimensions. The resulting image may be shorter or narrower than specified
  # in the smaller dimension but will not be larger than the specified values.
  #
  def resize_to_limit(width, height, options = {})
    options[:action] = :resize_to_limit

    name = filename(width, height, options)

    img = MiniMagick::Image.open(@asset.filename.path)
    img.combine_options do |c|
      c.quality(options[:quality]) if options[:quality]
      c.resize "#{width}x#{height}>"
    end

    img.write("#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{name}")
  end

  # Resize the image to fit within the specified dimensions while retaining
  # the original aspect ratio. The image may be shorter or narrower than
  # specified in the smaller dimension but will not be larger than the specified values.
  #
  def resize_to_fit(width, height, options = {})
    options[:action] = :resize_to_fit

    name = filename(width, height, options)

    img = MiniMagick::Image.open(@asset.filename.path)
    img.combine_options do |c|
      c.quality(options[:quality]) if options[:quality]
      c.resize "#{width}x#{height}"
    end

    img.write("#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{name}")
  end

  # Resize the image to fit within the specified dimensions while retaining
  # the aspect ratio of the original image. If necessary, crop the image in the
  # larger dimension.
  #
  def resize_to_fill(width, height, gravity = 'Center')
    # [width]
    # OR
    # x[height]
  end

  # Resize the image to fit within the specified dimensions while retaining
  # the original aspect ratio. If necessary, will pad the remaining area
  # with the given color, which defaults to transparent (for gif and png,
  # white for jpeg).
  #
  def resize_and_pad(width, height, gravity = 'Center', background = :transparant)
    # [width]x[height]
    # :transparant background for png
    # :white background for jpg
  end

  def main_dir
    @main_dir ||= Digest::MD5.hexdigest(@asset.id.to_s)[0,2]
  end

  def second_dir
    @second_dir ||= Digest::MD5.hexdigest(@asset.id.to_s)[2,2]
  end
end
{'fraction_non_alphanumeric': 0.13774597495527727, 'fraction_numerical': 0.003428741800834824, 'mean_word_length': 3.478638184245661, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 5, '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': '5597401', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2157, 'n_words': 551}