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
- .is_audio?(filename) ⇒ Boolean
- .is_image?(filename) ⇒ Boolean
- .is_url?(value) ⇒ Boolean
- .is_video?(filename) ⇒ Boolean
-
.load(value, localdir) ⇒ Object
Hash.
- .load_audio(value, localdir) ⇒ Object
- .load_image(value, localdir) ⇒ Object
- .load_video(value, localdir) ⇒ Object
Class Method Details
.is_audio?(filename) ⇒ 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
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
51 52 53 54 |
# File 'lib/asker/loader/embedded_file.rb', line 51 def self.is_url?(value) return true if value.start_with?('https://') || value.start_with?('http://') false end |
.is_video?(filename) ⇒ 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.
12 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 12 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.verbose Rainbow("[ERROR] EmbebbedFile. Unkown URL: #{value}").red.bright exit 1 end filepath = File.join(localdir, value) unless File.exist?(filepath) Logger.verbose Rainbow("[ERROR] EmbeddedFile. File does not exist!: #{filepath}").red.bright # return { text: "URI error", file: :none, type: :unkown } exit 1 end # Suposse that filename is TEXT file return { text: "<pre>#{File.read(filepath)}</pre>", file: :none, type: :text } end |
.load_audio(value, localdir) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/asker/loader/embedded_file.rb', line 56 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.verbose Rainbow("[ERROR] Audio file no exists!: #{filepath}").red.bright 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/asker/loader/embedded_file.rb', line 80 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.verbose Rainbow("[ERROR] Unknown file! #{filepath}").red.bright 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/asker/loader/embedded_file.rb', line 104 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.verbose Rainbow("[ERROR] Unknown file! #{filepath}").red.bright 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 |