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

.is_audio?(filename) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/asker/loader/embedded_file.rb', line 33

def self.is_audio?(filename)
  extens = [".mp3", ".ogg", ".wav"]
  extens.each { |ext| return true if filename.downcase.end_with?(ext) }
  false
end

.is_image?(filename) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/asker/loader/embedded_file.rb', line 39

def self.is_image?(filename)
  extens = [".jpg", ".jpeg", ".png"]
  extens.each { |ext| return true if filename.downcase.end_with?(ext) }
  false
end

.is_url?(value) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/asker/loader/embedded_file.rb', line 51

def self.is_url?(value)
  return true if value.start_with?("https://", "http://")

  false
end

.is_video?(filename) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/asker/loader/embedded_file.rb', line 45

def self.is_video?(filename)
  extens = [".mp4", ".ogv"]
  extens.each { |ext| return true if filename.downcase.end_with?(ext) }
  false
end

.load(value, localdir) ⇒ Object

Returns Hash.

Parameters:

  • value (String)
  • localdir (String)

    Input file base folder

Returns:

  • Hash



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/asker/loader/embedded_file.rb', line 13

def self.load(value, localdir)
  return load_image(value, localdir) if is_image? value
  return load_audio(value, localdir) if is_audio? value
  return load_video(value, localdir) if is_video? value

  if is_url? value
    Logger.error "EmbebbedFile: Unknown URL type (#{value})"
    exit 1
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.error "EmbeddedFile: File does not exist (#{filepath})"
    exit 1
  end

  # Suposse that filename is TEXT file
  {text: "<pre>#{File.read(filepath)}</pre>", file: :none, type: :text}
end

.load_audio(value, localdir) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/asker/loader/embedded_file.rb', line 57

def self.load_audio(value, localdir)
  output = {text: :error, file: :none, type: :audio}

  if is_url? value
    output[:text] = "<audio src=\"#{value}\" controls></audio>"
    output[:file] = :none
    output[:type] = :url
    return output
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.error "EmbebbedFile: Audio file no exists (#{filepath})"
    exit 1
  end
  output[:text] = '<audio controls><source src="@@PLUGINFILE@@/' + File.basename(filepath) \
                  + '">Your browser does not support the audio tag.</audio>'
  output[:file] = '<file name="' + File.basename(filepath) \
                  + '" path="/" encoding="base64">' \
                  + Base64.strict_encode64(File.open(filepath, "rb").read) \
                  + "</file>"
  output[:type] = :audio
  output
end

.load_image(value, localdir) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/asker/loader/embedded_file.rb', line 82

def self.load_image(value, localdir)
  output = {text: :error, file: :none, type: :image}

  if is_url? value
    output[:text] = "<img src=\"#{value}\" alt=\"image\" width=\"400\" height=\"300\">"
    output[:file] = :none
    output[:type] = :url
    return output
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.error "EmbeddedFile: Unknown file (#{filepath})"
    exit 1
  end
  output[:text] = '<img src="@@PLUGINFILE@@/' + File.basename(filepath) \
                  + '" alt="imagen" class="img-responsive atto_image_button_text-bottom">'
  output[:file] = '<file name="' + File.basename(filepath) \
                  + '" path="/" encoding="base64">' \
                  + Base64.strict_encode64(File.open(filepath, "rb").read) \
                  + "</file>"
  output[:type] = :image
  output
end

.load_video(value, localdir) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/asker/loader/embedded_file.rb', line 107

def self.load_video(value, localdir)
  output = {text: :error, file: :none, type: :video}
  if is_url? value
    output[:text] = "<video controls width=\"400\" height=\"300\">" \
                    + "<source src=\"#{value}\"/></video>"
    output[:file] = :none
    output[:type] = :url
    return output
  end

  filepath = File.join(localdir, value)
  unless File.exist?(filepath)
    Logger.error "Unknown file (#{filepath})"
    exit 1
  end
  output[:text] = '<video controls><source src="@@PLUGINFILE@@/' \
                  + File.basename(filepath) \
                  + '"/>Your browser does not support the video tag.</video>'
  output[:file] = '<file name="' \
                  + File.basename(filepath) \
                  + '" path="/" encoding="base64">' \
                  + Base64.strict_encode64(File.open(filepath, "rb").read) \
                  + "</file>"
  output[:type] = :video
  output
end