From 3b6ea22add46b4264c00b503fcc269ee58a9be32 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 29 Dec 2023 03:44:32 +0100 Subject: [PATCH] add data_url fn --- zsh/std.zsh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/zsh/std.zsh b/zsh/std.zsh index e1fb9e5..aa968bf 100644 --- a/zsh/std.zsh +++ b/zsh/std.zsh @@ -4,4 +4,23 @@ function skip() { local n=$1 shift tail -n +"$((n+1))" "$@" +} + +# usage: data_url +# 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 } \ No newline at end of file