From 5f9d13f2bf7d909744a167cf52d9e9a0f6673086 Mon Sep 17 00:00:00 2001 From: cswimr Date: Wed, 16 Oct 2024 14:24:33 -0400 Subject: [PATCH] automatically detect file mimetype --- .zshc/func.zsh | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/.zshc/func.zsh b/.zshc/func.zsh index aa87095..d816588 100644 --- a/.zshc/func.zsh +++ b/.zshc/func.zsh @@ -33,10 +33,9 @@ function clip { function zipline { # see spectacle-screenshot and flameshot-screenshot for example usage - readonly file=${1:?Please provide the path to the file you want to upload.} + readonly file_path=${1:?Please provide the path to the file you want to upload.} readonly name=${2} - readonly desktopentry=${3} - readonly contenttype=${4} + readonly desktop_entry=${3} readonly token=${ZIPLINE_TOKEN:?Environment variable ZIPLINE_TOKEN is not set. Please set it to your Zipline API token. I recommend placing it in a file at ~/.config/environment.d/zipline.conf} readonly url=${ZIPLINE_URL:?Environment variable ZIPLINE_URL is not set. Please set it to the URL of your Zipline instance. I recommend placing it in a file at ~/.config/environment.d/zipline.conf} @@ -53,13 +52,13 @@ function zipline { return 127 fi - if [[ ! -f "${file}" ]]; then - echo "File does not exist." + if [[ ! -f "${file_path}" ]]; then + echo "File at $file_path does not exist." return 66 fi - if [[ -n "${name}" && -n "${desktopentry}" ]]; then - if ! does-desktop-entry-exist "$desktopentry"; then + if [[ -n "${name}" && -n "${desktop_entry}" ]]; then + if ! does-desktop-entry-exist "$desktop_entry"; then echo "Desktop entry does not exist." return 66 fi @@ -72,24 +71,20 @@ function zipline { readonly use_send_notify=false fi - if [[ -z "${contenttype}" ]]; then - readonly content_type="multipart/form-data" - else - readonly content_type=$contenttype - fi + readonly content_type=$(command file -b --mime-type "${file_path}") - if curl -sS -fH "authorization: $token" "${url%/}/api/upload" -F"file=@$file;type=$content_type" >"$temp_file" 2>&1; then + if curl -sS -fH "authorization: $token" "${url%/}/api/upload" -F"file=@$file_path;type=$content_type" >"$temp_file" 2>&1; then readonly link=$(jq -r '.files[0]' <"$temp_file" | tr -d '\n') echo -n "$link" | clip echo "Upload successful. Link copied to clipboard: $link" if $use_send_notify; then - notify-send -a "$name" -u low -c "transfer.complete" -i "$file" -h "string:desktop-entry:$desktopentry" "Upload Successful" "Link copied to clipboard:\n$link" + notify-send -a "$name" -u low -c "transfer.complete" -i "$file_path" -h "string:desktop-entry:$desktop_entry" "Upload Successful" "Link copied to clipboard:\n$link" fi rm -f "$temp_file" else echo "Upload failed. Error message: $(<"$temp_file")" if $use_send_notify; then - notify-send -a "$name" -u critical -c "transfer.error" -h "string:desktop-entry:$desktopentry" "Upload Failed" "$(<"$temp_file")" + notify-send -a "$name" -u critical -c "transfer.error" -h "string:desktop-entry:$desktop_entry" "Upload Failed" "$(<"$temp_file")" fi rm -f "$temp_file" return 1 @@ -117,12 +112,6 @@ function spectacle-screenshot { return 1 fi - if $save_recording; then - readonly content_type="video/webm" - else - readonly content_type="image/png" - fi - if [[ -z "${2}" ]]; then if $save_recording; then readonly file=$(mktemp /tmp/spectacle-recording.XXXXXXXX.webm) @@ -141,7 +130,7 @@ function spectacle-screenshot { fi [ ! -s "$file" ] && return 1 - zipline "$file" Spectacle org.kde.spectacle.desktop "$content_type" + zipline "$file" Spectacle org.kde.spectacle.desktop rm -f "$file" } @@ -169,7 +158,7 @@ function flameshot-screenshot { echo "Screenshot saved to $file" [ ! -s "$file" ] && return 1 - zipline "$file" Flameshot org.flameshot.Flameshot.desktop image/png + zipline "$file" Flameshot org.flameshot.Flameshot.desktop rm -f "$file" }