Skip to content

Commit 5d76fc2

Browse files
alrramathiasbynens
authored andcommitted
.functions: Make dataurl more generic
1 parent 3a7ae53 commit 5d76fc2

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

.functions

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ if [ $? -eq 0 ]; then
1111
}
1212
fi
1313

14-
# Create a data URL from an image (works for other file types too, if you tweak the Content-Type afterwards)
15-
dataurl() {
16-
echo "data:image/${1##*.};base64,$(openssl base64 -in "$1")" | tr -d '\n'
14+
# Create a data URL from a file
15+
function dataurl() {
16+
local mimeType=$(file --mime-type "$1" | cut -d ' ' -f2)
17+
if [[ $mimeType == text/* ]]; then
18+
mimeType="${mimeType};charset=utf-8"
19+
fi
20+
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
1721
}
1822

1923
# Start an HTTP server from a directory, optionally specifying the port

0 commit comments

Comments
 (0)