XBackBone/resources/templates/scripts/xbackbone_uploader.sh.twig

105 lines
2.5 KiB
Twig
Raw Normal View History

2018-11-29 09:10:59 +00:00
#!/bin/bash
am_i_root() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root or with sudo";
exit;
fi
}
2018-11-30 09:51:03 +00:00
create_desktop_entry() {
2018-11-29 09:10:59 +00:00
cat << "EOF" > "/usr/share/applications/xbackbone-uploader-usr_{{ username }}.desktop"
[Desktop Entry]
Encoding=UTF-8
Exec=&EXEC& %U
Type=Application
Terminal=false;
Comment=Uploader script for XBackBone ({{ username }})
Name=XBackBone Uploader ({{ username }})
2019-07-09 17:09:46 +00:00
Icon=image
2018-11-29 09:10:59 +00:00
GenericName=File Uploader
StartupNotify=false
Categories=Graphics;
MimeType=image/bmp;image/jpeg;image/gif;image/png;image/tiff;image/x-bmp;image/x-ico;image/x-png;image/x-pcx;image/x-tga;image/xpm;image/svg+xml;
NoDisplay=false
EOF
2018-11-30 09:51:03 +00:00
sed -i "s:&EXEC&:${1}:g" "/usr/share/applications/xbackbone-uploader-usr_{{ username }}.desktop"
echo "Desktop entry created!";
2018-11-29 09:10:59 +00:00
}
2018-11-30 09:51:03 +00:00
upload() {
2019-07-09 17:17:36 +00:00
RESPONSE="$(curl -s -F "token={{ token }}" -F "upload=@${1}" {{ upload_url }})";
2018-11-30 09:51:03 +00:00
2020-04-16 19:49:35 +00:00
if [[ "$(echo "${RESPONSE}" | jq -r '.message')" == "OK" ]]; then
2018-11-30 09:51:03 +00:00
URL="$(echo "${RESPONSE}" | jq -r '.url')";
if [ "${DESKTOP_SESSION}" != "" ]; then
echo "${URL}" | xclip -selection c;
notify-send "Upload completed!" "${URL}";
else
echo "${URL}";
fi
2020-10-20 17:28:22 +00:00
exit 0;
2018-11-30 09:51:03 +00:00
else
2020-10-20 17:28:22 +00:00
MESSAGE="$(echo "${RESPONSE}" | jq -r '.message')";
if [ $? -ne 0 ]; then
echo "Unexpected response:";
echo "${RESPONSE}";
exit 1;
fi
if [ "${DESKTOP_SESSION}" != "" ]; then
2020-10-20 17:28:22 +00:00
notify-send "Error!" "${MESSAGE}";
else
2020-10-20 17:28:22 +00:00
echo "Error! ${MESSAGE}";
fi
2020-10-20 17:28:22 +00:00
exit 1;
2018-11-30 09:51:03 +00:00
fi
2018-11-29 09:10:59 +00:00
}
2018-11-30 09:51:03 +00:00
check() {
ERRORS=0;
if [ ! -x "$(command -v jq)" ]; then
echo "jq command not found.";
ERRORS=1;
2019-07-09 17:10:42 +00:00
fi
2018-11-29 09:10:59 +00:00
if [ ! -x "$(command -v curl)" ]; then
echo "curl command not found.";
ERRORS=1;
2018-11-29 09:10:59 +00:00
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;
2018-11-29 09:10:59 +00:00
fi
}
check
2018-11-30 09:51:03 +00:00
if [ "${1}" == "-desktop-entry" ]; then
am_i_root
2018-11-29 09:10:59 +00:00
create_desktop_entry "$(realpath "${0}")";
2018-11-30 09:51:03 +00:00
exit;
2018-11-29 09:10:59 +00:00
fi
2018-11-30 09:51:03 +00:00
if [ -f "${1}" ]; then
upload "${1}";
else
if [ "${DESKTOP_SESSION}" != "" ]; then
notify-send "Error!" "File specified not exists.";
2020-04-16 19:49:35 +00:00
exit 1;
else
echo "Error! File specified not exists.";
2020-04-16 19:49:35 +00:00
exit 1;
fi
2019-07-09 17:09:46 +00:00
fi