Skip to content

Instantly share code, notes, and snippets.

@aaferrari
Last active February 11, 2016 11:39
Show Gist options
  • Save aaferrari/a0fc51b784217ab5bf41 to your computer and use it in GitHub Desktop.
Save aaferrari/a0fc51b784217ab5bf41 to your computer and use it in GitHub Desktop.
Conversor de archivos a "inline" en Bash
#!/bin/bash
file2base64 () {
# Si es una archivo de internet, entonces se descarga temporalmente y luego se borra
if [[ "$1" =~ ^https?:// ]]; then
ubicacion="/tmp/imagen-temporal";
wget -q -O $ubicacion $1;
echo 'data:'$(file --mime-type -b $ubicacion)";base64,"$(base64 -w0 $ubicacion);
rm -f $ubicacion > /dev/null;
else # De lo contrario se asume que es un archivo local
# Se utilizan los comandos file para obtener el tipo de archivo y base64 para codificarlo en ese formato
echo 'data:'$(file --mime-type -b "$1")";base64,"$(base64 -w0 "$1")
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment