From 5d76fc286fe921cddf9a73c404ada924c856df24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A3t=C3=A3lin=20Mari=C5=9F?= Date: Tue, 7 Aug 2012 09:51:29 +0200 Subject: .functions: Make `dataurl` more generic --- .functions | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to '.functions') diff --git a/.functions b/.functions index 5336e3b..71f95cc 100644 --- a/.functions +++ b/.functions @@ -11,9 +11,13 @@ if [ $? -eq 0 ]; then } fi -# Create a data URL from an image (works for other file types too, if you tweak the Content-Type afterwards) -dataurl() { - echo "data:image/${1##*.};base64,$(openssl base64 -in "$1")" | tr -d '\n' +# Create a data URL from a file +function dataurl() { + local mimeType=$(file --mime-type "$1" | cut -d ' ' -f2) + if [[ $mimeType == text/* ]]; then + mimeType="${mimeType};charset=utf-8" + fi + echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')" } # Start an HTTP server from a directory, optionally specifying the port -- cgit v1.2.3