Module: EmbeddedFile

Defined in:
lib/asker/loader/embedded_file.rb

Overview

Methods to load embedded files defined into asker input data file Example:

  • def line with :type = :image_url used to link external file as https://…“

  • def line with :type = :file used to load local file as image.png or file.txt“

Class Method Summary collapse

Class Method Details

.load(value, localdir) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

Parameters:

  • value (String)
  • localdir (String)

    Input file base folder

Returns:

  • Hash



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/asker/loader/embedded_file.rb', line 16

def self.load(value, localdir)
  # When filename is an URL
  if value.start_with?('https://') || value.start_with?('http://')
    return { text: "<img src=\"#{value}\" alt=\"image\" width=\"400\" height=\"300\">", file: :none }
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    # When filename is unkown!
    Logger.verbose Rainbow("[ERROR] Unknown file! #{filepath}").red.bright
    exit 1
  end
  # When filename is PNG, JPG o JPEG
  if ['.png', '.jpg', '.jpeg'].include? File.extname(filepath)
    # converts image into base64 strings
    text = '<img src="@@PLUGINFILE@@/' + File.basename(filepath) \
           + '" alt="imagen" class="img-responsive atto_image_button_text-bottom">'
    data = '<file name="' + File.basename(filepath) + '" path="/" encoding="base64">' \
           + Base64.strict_encode64(File.open(filepath, 'rb').read) + '</file>'
    return { text: text, file: data }
  end
  # Suposse that filename is TXT file
  return { text: "<pre>#{File.read(filepath)}</pre>", file: :none } if File.exist?(filepath)
end