Skip to content

Instantly share code, notes, and snippets.

@DerEnderKeks
Last active October 20, 2018 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DerEnderKeks/30cc7b3aebee4eee444337f452e19565 to your computer and use it in GitHub Desktop.
Save DerEnderKeks/30cc7b3aebee4eee444337f452e19565 to your computer and use it in GitHub Desktop.
Linux file upload script for Yanius (https://github.com/DerEnderKeks/Yanius)
#!/bin/bash
# LICENSE
# The MIT License (MIT)
# Copyright (c) 2017 DerEnderKeks
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# You have to insert your data in the following variables
API_KEY="API-Key"
COPYRIGHT="Your Name"
URL="https://example.com/api/upload"
IMAGE_DIR="$HOME/Pictures/"
IMAGE_PREFIX="upload-linux_"
# ---- Don't edit anything below this line unless you know what you're doing ----
ICON="$( cd "$( dirname "$0" )" && pwd )/icon.png"
# Tools
curl="$(which curl)"
zenity="$(which zenity)"
notify_send="$(which notify-send)"
exiftool="$(which exiftool)"
gnome_screenshot="$(which gnome-screenshot)"
deepin_screenshot="$(which deepin-screenshot)"
node="$(which node)"
# Dependencies:
# - gnome-screenshot
# - xclip
# - zenity
# - curl
# - notify-send
# - exiftool
# - deepin_screenshot
# - node
#
#
# command description
# ---------------------------
# upload -c upload window
# upload -a upload desktop
# upload -b area upload
# upload -d file upload
#
# Usage: uploadFile [fileName]
function uploadFile () {
if [ -z "$1" ]; then
echo "No file specified"
exit 1
elif [ ! -f "$1" ]; then
echo "Upload cancelled"
exit 1
fi
response=`"$curl" "$URL" -# -F "apikey=$API_KEY" -F "file=@$1"`
message=`echo "$response" | "$node" -e "process.stdin.resume();process.stdin.setEncoding('utf8');process.stdin.on('data', function(data) {var json = JSON.parse(data);if(json.message) process.stdout.write(json.message)});"` # this works. don't touch it
fileURL=`echo "$response" | "$node" -e "process.stdin.resume();process.stdin.setEncoding('utf8');process.stdin.on('data', function(data) {var json = JSON.parse(data);if(json.url) process.stdout.write(json.url)});"` # same here
# Copy link to clipboard and show notification
printf "$fileURL" | xclip -selection "clipboard"
"$notify_send" -i "$ICON" -t 2000 "$message" "$fileURL"
echo "$message $fileURL"
}
function help () {
echo "___________ upload by DerEnderKeks ___________"
echo ""
echo "Usage:"
echo " upload [OPTIONS] [PATH]"
echo ""
echo "OPTIONS:"
echo " -d upload entire desktop"
echo " -a select area to upload"
echo " -w upload current window"
echo " -f upload specific file (opens file dialog)"
echo ""
echo " --help,-h show this text"
echo ""
echo "PATH:"
echo " PATH optional: path of file to upload"
}
function generateFileName () {
echo "$IMAGE_DIR""$IMAGE_PREFIX""$(date +"%Y-%m-%d_%H.%M.%S").png"
}
function modifyExif () {
if [ -z "$1" ]; then
return
fi
"$exiftool" -quiet -overwrite_original -all= -credit="$COPYRIGHT" -copyright="Copyright $COPYRIGHT" "$1"
}
if [ -z "$API_KEY" ]; then
echo "Change the variable API_KEY in $0"
"$notify_send" -i "$ICON" "Change the variable API_KEY in $0"
exit 1
elif [ -z "$1" ]; then
echo "No file entered."
help
exit 1
fi
# Argument Handling
case "$1" in
-d)
echo "Desktop"
fileName=$(generateFileName)
"$gnome_screenshot" -f "$fileName"
modifyExif "$fileName"
uploadFile "$fileName"
;;
-a)
echo "Area"
fileName=$(generateFileName)
"$deepin_screenshot" -n -s "$fileName"
modifyExif "$fileName"
uploadFile "$fileName"
;;
-w)
echo "Window"
fileName=$(generateFileName)
"$gnome_screenshot" -w -f "$fileName"
modifyExif "$fileName"
uploadFile "$fileName"
;;
-f)
echo "File Upload"
fileName=`"$zenity" --file-selection`
uploadFile "$fileName"
;;
-h|--help)
help
exit 0
;;
*)
echo "Upload $1"
uploadFile "$1"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment