cswimr/home
Archived
Template
1
0
Fork 0

automatically detect file mimetype

This commit is contained in:
Seaswimmer 2024-10-16 14:24:33 -04:00
parent a4569e1d8f
commit 5f9d13f2bf
Signed by: cswimr
GPG key ID: A9C162E867C851FA

View file

@ -33,10 +33,9 @@ function clip {
function zipline { function zipline {
# see spectacle-screenshot and flameshot-screenshot for example usage # 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 name=${2}
readonly desktopentry=${3} readonly desktop_entry=${3}
readonly contenttype=${4}
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 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} 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 return 127
fi fi
if [[ ! -f "${file}" ]]; then if [[ ! -f "${file_path}" ]]; then
echo "File does not exist." echo "File at $file_path does not exist."
return 66 return 66
fi fi
if [[ -n "${name}" && -n "${desktopentry}" ]]; then if [[ -n "${name}" && -n "${desktop_entry}" ]]; then
if ! does-desktop-entry-exist "$desktopentry"; then if ! does-desktop-entry-exist "$desktop_entry"; then
echo "Desktop entry does not exist." echo "Desktop entry does not exist."
return 66 return 66
fi fi
@ -72,24 +71,20 @@ function zipline {
readonly use_send_notify=false readonly use_send_notify=false
fi fi
if [[ -z "${contenttype}" ]]; then readonly content_type=$(command file -b --mime-type "${file_path}")
readonly content_type="multipart/form-data"
else
readonly content_type=$contenttype
fi
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') readonly link=$(jq -r '.files[0]' <"$temp_file" | tr -d '\n')
echo -n "$link" | clip echo -n "$link" | clip
echo "Upload successful. Link copied to clipboard: $link" echo "Upload successful. Link copied to clipboard: $link"
if $use_send_notify; then 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 fi
rm -f "$temp_file" rm -f "$temp_file"
else else
echo "Upload failed. Error message: $(<"$temp_file")" echo "Upload failed. Error message: $(<"$temp_file")"
if $use_send_notify; then 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 fi
rm -f "$temp_file" rm -f "$temp_file"
return 1 return 1
@ -117,12 +112,6 @@ function spectacle-screenshot {
return 1 return 1
fi fi
if $save_recording; then
readonly content_type="video/webm"
else
readonly content_type="image/png"
fi
if [[ -z "${2}" ]]; then if [[ -z "${2}" ]]; then
if $save_recording; then if $save_recording; then
readonly file=$(mktemp /tmp/spectacle-recording.XXXXXXXX.webm) readonly file=$(mktemp /tmp/spectacle-recording.XXXXXXXX.webm)
@ -141,7 +130,7 @@ function spectacle-screenshot {
fi fi
[ ! -s "$file" ] && return 1 [ ! -s "$file" ] && return 1
zipline "$file" Spectacle org.kde.spectacle.desktop "$content_type" zipline "$file" Spectacle org.kde.spectacle.desktop
rm -f "$file" rm -f "$file"
} }
@ -169,7 +158,7 @@ function flameshot-screenshot {
echo "Screenshot saved to $file" echo "Screenshot saved to $file"
[ ! -s "$file" ] && return 1 [ ! -s "$file" ] && return 1
zipline "$file" Flameshot org.flameshot.Flameshot.desktop image/png zipline "$file" Flameshot org.flameshot.Flameshot.desktop
rm -f "$file" rm -f "$file"
} }