Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
APi upload for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Heckel committed Mar 2, 2015
1 parent fc4c10e commit 7b37c45
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
103 changes: 103 additions & 0 deletions gradle/upload/upload-functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
SYNCANY_API_ENDPOINT="https://api.syncany.org/v3"
SYNCANY_API_ENDPOINT="http://tests.lan/syncany-website/api.syncany.org/html/v3"

if [ "$SYNCANY_API_KEY" == "" ]; then
echo "ERROR: SYNCANY_API_KEY environment variable not set."
exit 1
fi

urlencode() {
# urlencode <string>

local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9._-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}

upload_file() {
# upload_file <filename> <type> <request> <snapshot> <os> <arch>

filename="$1"
type="$2"
request="$3"
snapshot="$4"
os="$5"
arch="$6"

if [ -z "$filename" -o -z "$type" -o -z "$request" -o -z "$snapshot" -o -z "$os" -o -z "$arch" ]; then
echo "ERROR: Invalid arguments for upload_file. None of the arguments can be zero."
exit 2
fi

if [ ! -f "$filename" ]; then
echo "ERROR: Type '$type': File $filename does not exist. Skipping."
exit 3
fi

basename=$(basename "$filename")
basename_encoded=$(urlencode $basename)
checksum=$(sha256sum "$filename" | awk '{ print $1 }')

method="PUT"
methodArgs="type=$type&filename=$basename_encoded&snapshot=$snapshot&os=$os&arch=$arch&checksum=$checksum"

time=$(date +%s)
rand=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)

protected="$method:$request:$methodArgs:$time:$rand"
signature=$(echo -n "$protected" | openssl dgst -sha256 -hmac $SYNCANY_API_KEY | awk '{ print $2 }')

curl -v -T "$filename" "$SYNCANY_API_ENDPOINT/$request?$methodArgs&time=$time&rand=$rand&signature=$signature"

exit_code=$?

if [ $exit_code -ne 0 ]; then
echo "ERROR: Upload failed. curl exit code was $exit_code."
exit 5
fi
}

get_property() {
# get_property <properties-file> <property-name>

properties_file="$1"
property_name="$2"

cat "$properties_file" | grep "$property_name=" | sed -r 's/.+=//'
}

get_os_from_filename() {
# get_os_from_filename <filename>

filename="$1"

if [ -n "$(echo $filename | grep linux)" ]; then
echo "linux"
elif [ -n "$(echo $filename | grep windows)" ]; then
echo "windows"
elif [ -n "$(echo $filename | grep macosx)" ]; then
echo "macosx"
else
echo "all"
fi
}

get_arch_from_filename() {
# get_arch_from_filename <filename>

filename="$1"

if [ -n "$(echo $filename | grep x86_64)" -o -n "$(echo $filename | grep amd64)" ]; then
echo "x86_64"
elif [ -n "$(echo $filename | grep x86)" -o -n "$(echo $filename | grep i386)" ]; then
echo "x86"
else
echo "all"
fi
}

77 changes: 77 additions & 0 deletions gradle/upload/upload-plugin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

SCRIPTDIR="$( cd "$( dirname "$0" )" && pwd )"
REPODIR=$(readlink -f "$SCRIPTDIR/../../../")

if [ -n "$TRAVIS_PULL_REQUEST" -a "$TRAVIS_PULL_REQUEST" != "false" ]; then
echo "NOTE: Skipping upload. This job is a PULL REQUEST."
exit 0
fi

source "$SCRIPTDIR/upload-functions.sh"

properties_file=$(ls -d build/resources/main/org/syncany/plugins/*/plugin.properties)

if [ ! -f "$properties_file" ]; then
echo "ERROR: Cannot find properties file with plugin details."
exit 2
fi

plugin_id=$(get_property $properties_file "pluginId")
release=$(get_property $properties_file "pluginRelease")
snapshot=$([ "$release" == "true" ] && echo "false" || echo "true") # Invert 'release'

echo ""
echo "Files to upload for $plugin_id"
echo "------------------------------"
PWD=`pwd`
cd $REPODIR/build/upload/
sha256sum * 2>/dev/null
cd "$PWD"

echo ""
echo "Uploading plugin $plugin_id"
echo "------------------------------"

files_jar=$(ls $REPODIR/build/upload/*.jar 2> /dev/null)
files_deb=$(ls $REPODIR/build/upload/*.deb 2> /dev/null)
files_exe=$(ls $REPODIR/build/upload/*.exe 2> /dev/null)
files_appzip=$(ls $REPODIR/build/upload/*.app.zip 2> /dev/null)

for file in $files_jar; do
echo "Uploading JAR: $(basename $file) ..."

os=$(get_os_from_filename "$(basename "$file")")
arch=$(get_arch_from_filename "$(basename "$file")")

upload_file "$file" "jar" "plugins/$plugin_id" "$snapshot" "$os" "$arch"
done

for file in $files_deb; do
echo "Uploading DEB: $(basename $file) ..."

os=$(get_os_from_filename "$(basename "$file")")
arch=$(get_arch_from_filename "$(basename "$file")")

upload_file "$file" "deb" "plugins/$plugin_id" "$snapshot" "$os" "$arch"
done

if [ "$plugin_id" == "gui" ]; then
for file in $files_exe; do
echo "Uploading EXE: $(basename $file) ..."

os=$(get_os_from_filename "$(basename "$file")")
arch=$(get_arch_from_filename "$(basename "$file")")

upload_file "$file" "exe" "plugins/$plugin_id" "$snapshot" "$os" "$arch"
done

for file in $files_appzip; do
echo "Uploading APP.ZIP: $(basename $file) ..."

os=$(get_os_from_filename "$(basename "$file")")
arch=$(get_arch_from_filename "$(basename "$file")")

upload_file "$file" "app.zip" "plugins/$plugin_id" "$snapshot" "$os" "$arch"
done
fi

0 comments on commit 7b37c45

Please sign in to comment.