add data_url fn

This commit is contained in:
JMARyA 2023-12-29 03:44:32 +01:00
parent 5ebe8b934b
commit 3b6ea22add
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -4,4 +4,23 @@ function skip() {
local n=$1 local n=$1
shift shift
tail -n +"$((n+1))" "$@" tail -n +"$((n+1))" "$@"
}
# usage: data_url <file>
# turn file into a data url
data_url() {
if [ $# -eq 1 ]; then
file_path=$1
if [ -f "$file_path" ]; then
base64_data=$(base64 < "$file_path")
mime_type=$(file --mime-type -b "$file_path")
data_url="data:$mime_type;base64,$base64_data"
echo "$data_url"
else
echo "File not found: $file_path"
fi
else
echo "\$ data_url [FILE_PATH]"
echo "Turn data into a data URL"
fi
} }