Skip to content

Instantly share code, notes, and snippets.

@elpollodiablo
Last active April 18, 2020 19:10
Show Gist options
  • Save elpollodiablo/b4edfc70b0cd65112d47dfadeb51e951 to your computer and use it in GitHub Desktop.
Save elpollodiablo/b4edfc70b0cd65112d47dfadeb51e951 to your computer and use it in GitHub Desktop.
Share with Seafile Quick Action
#/bin/bash
####################################################################
# Configure a quick action to share a file from finder and
# get a sharable link in clipboard
####################################################################
# On Seafile:
# - create a library (repo) that suits your sharing needs
# On Terminal:
# - get $AUTH_TOKEN:
# AUTH_TOKEN=`curl -s -d "username=YOUR_SEAFILE_EMAIL&password=YOUR_SEAFILE_PASSWORD" https://cloud.linuxteam.at/api2/auth-token/ |awk -F\" '{print $4}'`
# - get $REPO_ID by listing the repos, looking for your repo name and the associated "id" field:
# curl -H 'Authorization: Token $AUTH_TOKEN' -H 'Accept: application/json; indent=4' https://cloud.linuxteam.at/api2/repos/| python -m json.tool |egrep "\{|\"name\"|\"id|\}"
# Open Automator -> (New) Quick Action
# - Choose "Workflow receives current [files or folders] in [Finder]" from dropdown menus
# - Choose an applicable Image as Icon, i.e. "Share" that suits you
# - Choose a color that suits you
# - Search for Action "Run Shell Script"
# - Drag "Run Shell Script" to "Drag Actions here To build your workflow"
# - Choose "Shell: [/bin/bash]" from dropdown
# - Choose "Pass Input: [as arguments]" from dropdown
# - Copy/paste this script into empty textbox
# - Configure API_HOST, AUTH_TOKEN and REPO_ID below to reflect the values you got from the steps above
# - Select "Save..." in the menu bar and choose a good name for the action, i.e. "Share with Seafile"
# - Check in System Preferences > Keyboard > Shortcuts > Services > File and Folders > [X] Share with Seafile is checked
# Done: right click on any file in finder to access "Quick Actions" and select "Share with Seafile" to upload it. After you get a desktop notifiation, the sharable link is in your clipboard.
####################################################################
PATH_TO_SHARE=$1
BASENAME=`/usr/bin/basename "$PATH_TO_SHARE"`
AUTH_TOKEN=""
API_HOST="https://YOUR_SEAFILE_HOST/api2"
CURL="/usr/bin/curl"
DATE="/bin/date"
REPO_ID=""
PBCOPY="/usr/bin/pbcopy"
$DATE >> /tmp/seafile_shared.log
echo $PATH_TO_SHARE >> /tmp/seafile_shared.log
UPLOAD_LINK=`$CURL -s -H "Authorization: Token $AUTH_TOKEN" $API_HOST/repos/$REPO_ID/upload-link/`
UPLOAD_LINK=`echo $UPLOAD_LINK|tr -d \"`
FILE=$($CURL -s -H "Authorization: Token $AUTH_TOKEN" -F file=@\""${PATH_TO_SHARE}"\" -F filename=\""${PATH_TO_SHARE}"\" -F parent_dir=\"/\" $UPLOAD_LINK)
LOCATION=$($CURL -i -X PUT -d p=/"${BASENAME}" -H "Authorization: Token $AUTH_TOKEN" -H 'Accept: application/json; indent=4' $API_HOST/repos/$REPO_ID/file/shared-link/ |grep Location)
LOCATION=`echo -n $LOCATION|cut -d " " -f 2|tr -d '\r'`
echo "$LOCATION"| $($PBCOPY)
# delete/change this for desktop notification behavior
osascript -e "display notification \"$PATH_TO_SHARE\" with title \"Upload Complete\" subtitle \"$LOCATION\" sound name \"Submarine\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment