module Locomotive::Wagon::AssetsConcern

Constants

REGEX

Public Instance Methods

replace_asset_urls(content) click to toggle source

The content assets on the remote engine follows the format: /sites/<id>/assets/<type>/<file> This method replaces these urls by their local representation. <type>/<file>

@param [ String ] content The text where the assets will be replaced.

# File lib/locomotive/wagon/commands/pull_sub_commands/concerns/assets_concern.rb, line 17
def replace_asset_urls(content)
  return '' if content.blank?

  _content = content.dup

  content.force_encoding('utf-8').scan(REGEX).map do |match|
    url, type, filename = match[0], match[2], match[5]
    folder = case type
    when 'assets', 'pages'  then File.join('samples', "_#{env}", type)
    when 'theme'            then $4
    when /\Acontent_entry/  then File.join('samples', "_#{env}", 'content_entries')
    end

    Thread.new do
      if filepath = write_asset(url, File.join(path, 'public', folder, filename))
        [url, File.join('', folder, File.basename(filepath)).to_s]
      else
        [url, '']
      end
    end
  end.map(&:value).each do |(url, replacement)|
    _content.gsub!(url, replacement)
  end

  _content
end
replace_asset_urls_in_hash(hash) click to toggle source
# File lib/locomotive/wagon/commands/pull_sub_commands/concerns/assets_concern.rb, line 44
def replace_asset_urls_in_hash(hash)
  Locomotive::Wagon::YamlExt.transform(hash) do |value|
    replace_asset_urls(value)
  end
end

Private Instance Methods

find_unique_filepath(filepath, binary_file, index = 1) click to toggle source
# File lib/locomotive/wagon/commands/pull_sub_commands/concerns/assets_concern.rb, line 52
def find_unique_filepath(filepath, binary_file, index = 1)
  if File.exists?(filepath) && File.file?(filepath)
    # required because we need to make sure we use the content of file from its start
    binary_file.rewind

    return filepath if FileUtils.compare_stream(binary_file, File.open(filepath))

    folder, ext = File.dirname(filepath), File.extname(filepath)
    basename = File.basename(filepath, ext)

    find_unique_filepath(File.join(folder, "#{basename}-#{index}#{ext}"), binary_file, index + 1)
  else
    filepath
  end
end
get_asset_binary(url) click to toggle source
# File lib/locomotive/wagon/commands/pull_sub_commands/concerns/assets_concern.rb, line 68
def get_asset_binary(url)
  unless url =~ /\Ahttp:\/\//
    base = api_client.uri.dup.tap { |u| u.path = '' }.to_s
    url = URI.join(base, url).to_s
  end

  binary = Faraday.get(url).body rescue nil
end
write_asset(url, filepath) click to toggle source
# File lib/locomotive/wagon/commands/pull_sub_commands/concerns/assets_concern.rb, line 77
def write_asset(url, filepath)
  if binary = get_asset_binary(url)
    FileUtils.mkdir_p(File.dirname(filepath))

    (binary_file = Tempfile.new(File.basename(filepath)).binmode).write(binary)

    find_unique_filepath(filepath, binary_file).tap do |filepath|
      File.open(filepath, 'wb') { |f| f.write(binary) }
    end
  else
    instrument :missing_asset, url: url
    nil
  end
end