require "base64" require_relative "../logger" # 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" module EmbeddedFile ## # @param value (String) # @param localdir (String) Input file base folder # @return Hash 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: "
#{File.read(filepath)}", file: :none, type: :text} end def self.is_audio?(filename) extens = [".mp3", ".ogg", ".wav"] extens.each { |ext| return true if filename.downcase.end_with?(ext) } false end def self.is_image?(filename) extens = [".jpg", ".jpeg", ".png"] extens.each { |ext| return true if filename.downcase.end_with?(ext) } false end def self.is_video?(filename) extens = [".mp4", ".ogv"] extens.each { |ext| return true if filename.downcase.end_with?(ext) } false end def self.is_url?(value) return true if value.start_with?("https://", "http://") false end def self.load_audio(value, localdir) output = {text: :error, file: :none, type: :audio} if is_url? value output[:text] = "" 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] = '' output[:file] = '