From f5bf25dcf721272ebe84201f1539dd924d1b13f5 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Sat, 7 Mar 2020 01:58:01 +0100 Subject: [PATCH] Cool utility to base64 a file --- bash/bash.d/network.bash | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 bash/bash.d/network.bash diff --git a/bash/bash.d/network.bash b/bash/bash.d/network.bash new file mode 100644 index 0000000..dd9a243 --- /dev/null +++ b/bash/bash.d/network.bash @@ -0,0 +1,18 @@ +#!/bin/bash + +function data_url() { + if [ -z "$1" ]; then + cat << EndOfMessage +Create a data URL that can be directly used in HTML. +Usage: data_url +EndOfMessage + return + fi + local mime_type base64 + mime_type=$(file -b --mime-type "$1") + if [[ "$mime_type" == 'image/svg' ]]; then + mime_type='image/svg+xml' + fi + base64=$(base64 "$1") + echo "data:$mime_type;base64,$base64"; +}