Created
February 23, 2013 06:25
-
-
Save sadah/5018701 to your computer and use it in GitHub Desktop.
b64e2.rb encode file to base64(data URL Scheme).
b64e2.rb make "base64" directory and write encoded file. usage ruby b64e2.rb [filename]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'base64' | |
| require 'mime/types' | |
| def print_usage | |
| puts <<EOS | |
| b64e2.rb encode file to base64(data URL Scheme). | |
| b64e2.rb make "base64" directory and write encoded file. | |
| usage | |
| ruby b64e2.rb [filename] | |
| EOS | |
| abort | |
| end | |
| filename = ARGV[0] | |
| print_usage if filename.nil? || !File.exists?(filename) | |
| mime_type = MIME::Types.type_for(filename)[0].to_s | |
| encoded_text = Base64.strict_encode64(open(filename).read) | |
| dir_path = File.dirname(filename) + "/base64" | |
| basename = File.basename(filename, ".*") | |
| Dir::mkdir(dir_path) unless File.exists?(dir_path) | |
| File.open("#{dir_path}/#{basename}", "w") do |file| | |
| file.write "data:#{mime_type};base64,#{encoded_text}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment