From 065953fdc71250de4f50e63629828c81248f9711 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Thu, 26 Sep 2024 01:32:25 +0200 Subject: [PATCH] Add immich-upload to screenshot tool --- .bin/screenshot | 76 ++++++++++++++++++++++++++++++++++----- .config/env.d/30-services | 2 ++ 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/.bin/screenshot b/.bin/screenshot index c182bf6..abb51fe 100755 --- a/.bin/screenshot +++ b/.bin/screenshot @@ -2,10 +2,13 @@ tool="grim" +now=$(date '+%F_%H-%M-%S') +now_iso=$(date -Is) +expire_date=$(date -Is -d "+1hour") rclone_remote="s3-serguzim-me:public/screenshots" screenshot_host="https://s3.serguzim.me/public/screenshots" -screenshot_path="/tmp/screenshot.webp" -file="$(date '+%F_%H-%M-%S').webp" +screenshot_path="/tmp/screenshot_$now.webp" +file="$now.webp" clip_image() { @@ -19,18 +22,73 @@ clip_image() upload_image() { + share_url=$(upload_image_immich) + echo "$share_url" + + if [ -n "$share_url" ] + then + notify-send "Screenshot" "Uploaded screenshot" + else + notify-send "Screenshot" "Failed to upload screenshot" + fi + + xdg-open "$share_url" + # to have link available in clipboard manager if [ -n "$WAYLAND_DISPLAY" ] then - wl-copy "$screenshot_host/$file" - wl-copy -p "$screenshot_host/$file" + wl-copy "$share_url" + wl-copy -p "$share_url" else - printf '%s' "$screenshot_host/$file" | xclip -selection clipboard - printf '%s' "$screenshot_host/$file" | xclip -selection primary + printf '%s' "$share_url" | xclip -selection clipboard + printf '%s' "$share_url" | xclip -selection primary fi +} - rclone copyto "$screenshot_path" "$rclone_remote/$file" - notify-send -a "Screenshot" "Uploaded screenshot to s3" +upload_image_rclone() +{ + set -e + + rclone copyto "$screenshot_path" "$rclone_remote/$file" >/dev/null + echo "$screenshot_host/$file" +} + +# Immich url to get api key for service account (skip oauth) +# https://my.immich.app/auth/login?autoLaunch=0 +upload_image_immich() +{ + set -e + + result_upload=$(curl -sSfL "$IMMICH_INSTANCE_URL/api/assets" \ + -H 'Content-Type: multipart/form-data' \ + -H 'Accept: application/json' \ + -H "x-api-key: $IMMICH_API_KEY_SCREENSHOTS" \ + -F 'deviceId="screenshots"' \ + -F "deviceAssetId=\"screenshots-$screenshot_path\"" \ + -F "fileModifiedAt=\"$now_iso\"" \ + -F "fileCreatedAt=\"$now_iso\"" \ + -F "assetData=@\"$screenshot_path\"") + asset_id=$(echo "$result_upload" | jq -r '.id') + + result_share=$(curl -sSfL "$IMMICH_INSTANCE_URL/api/shared-links" \ + -H 'Content-Type: application/json' \ + -H 'Accept: application/json' \ + -H "x-api-key: $IMMICH_API_KEY_SCREENSHOTS" \ + -d "{ + \"allowDownload\": true, + \"allowUpload\": false, + \"assetIds\": [ + \"$asset_id\" + ], + \"expiresAt\": \"$expire_date\", + \"showMetadata\": true, + \"type\": \"INDIVIDUAL\" + }") + share_key=$(echo "$result_share" | jq -r '.key') + + #share_url="$IMMICH_INSTANCE_URL/share/$share_key/photos/$asset_id" + share_url="$IMMICH_INSTANCE_URL/api/assets/$asset_id/original?key=$share_key" + echo "$share_url" } call_screenshot_tool() { @@ -64,7 +122,7 @@ main() { if [ -n "$1" ] && [ -e "$1" ] then - convert "$1" "$screenshot_path" + magick "$1" "$screenshot_path" upload_image clip_image else diff --git a/.config/env.d/30-services b/.config/env.d/30-services index 2af9a38..1c270ce 100755 --- a/.config/env.d/30-services +++ b/.config/env.d/30-services @@ -4,6 +4,8 @@ export DRONE_SERVER="https://ci.serguzim.me" export HASTE_SERVER="https://haste.snrd.eu" +export IMMICH_INSTANCE_URL="https://gallery.serguzim.me" + export LINKWARDEN_URL="https://bookmarks.serguzim.me" export OPENFAAS_URL="https://faas.serguzim.me"