SergiX44/XBackBone

View on GitHub
resources/templates/scripts/xbackbone_kde_uploader.sh.twig

Summary

Maintainability
Test Coverage
#!/bin/bash

create_desktop_entry() {
cat << "EOF" > "$HOME/.local/share/kio/servicemenus/xbackbone-uploader.desktop"
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
Icon=imaga
X-KDE-StartupNotify=false
X-KDE-Priority=TopLevel
MimeType=image/*;
Actions=xbackbone_upload

[Desktop Action xbackbone_upload]
Name=Upload with XBackBone
Exec=sh -c 'RESPONSE="$(curl -s -F "token={{ token }}" -F "upload=@%u" {{ upload_url }})"; if [ "$(echo "${RESPONSE}" | jq -r ".message")" = "OK" ]; then URL="$(echo "${RESPONSE}" | jq -r ".url")"; if [ "${DESKTOP_SESSION}" != "" ]; then echo "${URL}" | xclip -selection c; notify-send "Upload completed!" "${URL}"; else echo "${URL}"; fi; exit 0; else MESSAGE="$(echo "${RESPONSE}" | jq -r ".message")"; if [ $? -ne 0 ]; then echo "Unexpected response:"; echo "${RESPONSE}"; exit 1; fi; if [ "${DESKTOP_SESSION}" != "" ]; then notify-send "Error!" "${MESSAGE}"; else echo "Error! ${MESSAGE}"; fi; exit 1; fi'
Icon=image
EOF

    echo "Service menu created!";
}

check() {
    ERRORS=0;

    if [ ! -x "$(command -v jq)" ]; then
        echo "jq command not found.";
        ERRORS=1;
    fi

    if [ ! -x "$(command -v curl)" ]; then
        echo "curl command not found.";
        ERRORS=1;
    fi

    if [ ! -x "$(command -v xclip)" ] && [ "${DESKTOP_SESSION}" != "" ]; then
        echo "xclip command not found.";
        ERRORS=1;
    fi

    if [ ! -x "$(command -v notify-send)" ] && [ "${DESKTOP_SESSION}" != "" ]; then
        echo "notify-send command not found.";
        ERRORS=1;
    fi

    if [ "${ERRORS}" -eq 1 ]; then
      exit 1;
    fi
}

check
create_desktop_entry