aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCãtãlin Mariş <alrraa@gmail.com>2012-08-07 09:51:29 +0200
committerMathias Bynens <mathias@qiwi.be>2012-08-07 09:54:38 +0200
commit5d76fc286fe921cddf9a73c404ada924c856df24 (patch)
treef56b0f3bc59682d23f89f6771c1353f61f279a99
parent3a7ae53e188f16cd51dbae6056439587ce56174e (diff)
downloaddotfiles-5d76fc286fe921cddf9a73c404ada924c856df24.tar.gz
dotfiles-5d76fc286fe921cddf9a73c404ada924c856df24.tar.bz2
dotfiles-5d76fc286fe921cddf9a73c404ada924c856df24.zip
.functions: Make `dataurl` more generic
-rw-r--r--.functions10
1 files changed, 7 insertions, 3 deletions
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