Skip to content

Instantly share code, notes, and snippets.

@emctague
Last active October 25, 2019 20:01
Show Gist options
  • Save emctague/11baeb314b198029ee02da0177ad70ab to your computer and use it in GitHub Desktop.
Save emctague/11baeb314b198029ee02da0177ad70ab to your computer and use it in GitHub Desktop.
command-line data url generator

dataurl

Converts a file into a data URL.

e.g.

dataurl test.png

To open in browser:

dataurl -b test.png

Works for any filetype that file can identify the MIME type for!

Installation

curl -sL https://git.io/JeEt0 | bash

You will need to provide your password for sudo. The script will be installed to /usr/local/bin/dataurl.

#!/bin/bash
DO_BROWSER=false
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "Usage: dataurl [options] file"
echo "Options:"
echo " -h, --help Show help"
echo " -b, --browse Launch browser afterward (needs python)"
exit 0
;;
-b|--browse)
DO_BROWSER=true
shift
;;
*)
break
;;
esac
done
MIME=`file -b --mime-type $1`
DATA=`base64 $1`
URL="data:$MIME;base64,$DATA"
echo $URL
if [ "$DO_BROWSER" = true ] ; then
python -m webbrowser "$URL"
fi
curl https://gist.githubusercontent.com/emctague/11baeb314b198029ee02da0177ad70ab/raw/d313a2e6be5591d8f26903f2e1eade43bb4fd5b3/dataurl --output /tmp/dataurl-script
echo "Installing Data URL Script!"
sudo mv /tmp/dataurl-script /usr/local/bin/dataurl
sudo chmod +x /usr/local/bin/dataurl
echo "By the way, don't run scripts through cURL! It's kinda considered unsafe!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment