Skip to content

Instantly share code, notes, and snippets.

@amadden80
Created October 31, 2013 12:36
Show Gist options
  • Save amadden80/7248971 to your computer and use it in GitHub Desktop.
Save amadden80/7248971 to your computer and use it in GitHub Desktop.
Base64
# Andrew Madden
# Converts files into a base64 text representation.
# An example html document, with those representations, is created.
require "base64"
# Converts an audio wavefile's binary data into base64
# Returns a html node using that data
def gen_uri_audio_tag(filename)
data = Base64.strict_encode64(File.open(filename).read)
tag = "<audio autoplay='true', id='ajm-audio-ajm'>\n<source src='data:audio/wav;base64,%s'>\n</source>\n</audio>" % data
end
# Converts a png file's binary data into base64
# Returns a html node using that data
def gen_uri_image_tag(filename)
data = Base64.strict_encode64(File.open(filename).read)
tag = "<img id='ajm-image-ajm', src='data:image/png;base64,%s'/" % data
end
# Basic HTML to demo page
html_open = "<!DOCTYPE html>\n<html>\n<head>\n<title>URI Build</title>\n</head>\n<body>\n"
html_inner = ''
html_close = "\n</body>\n</html>"
# For each filename...
ARGV.each do|filename|
puts filename
if File.exist?(filename)
# Find the file extension
filename_type = filename.split('.')
filetype = filename_type.last.downcase
# Generate html using the uri data
case filetype
when 'png'
uri_tag = gen_uri_image_tag(filename)
html_inner += "</br>\n\n#{uri_tag}\n\n</br>"
when 'wave', 'wav'
uri_tag = gen_uri_audio_tag(filename)
html_inner += "</br>\n\n#{uri_tag}\n\n</br>"
else
puts "#{filename}\' file extension has not been handled"
end
else
puts "#{filename} not found"
end
end
# Write demo html file, using the tags built above
file = File.new("auto_ajm_uri.html", 'w')
file.puts(html_open + html_inner + html_close)
file.close
puts 'auto_ajm_uri.html written...'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment