Skip to content

Instantly share code, notes, and snippets.

@bcremer
Last active August 29, 2015 13:56
Show Gist options
  • Save bcremer/8922940 to your computer and use it in GitHub Desktop.
Save bcremer/8922940 to your computer and use it in GitHub Desktop.
Dropbox share file script
#!/usr/bin/env bash
##
# Required
# - dropbox-cli
# optional
# - xclip
# - convert (imagemagick)
##
#set -o errexit
set -o nounset
#set -o xtrace
main() {
local -r publicdir=~/Dropbox/public/
local -r inputfile=$1
local -r downscale=true
command -v convert >/dev/null 2>&1
local hasConvert=$?
if [[ $hasConvert != 0 ]]; then
echo "convert not found." >&2
exit 1
fi
if ! command -v dropbox >/dev/null 2>&1; then
echo "dropbox cli command not found." >&2
exit 1
fi
if dropbox running != 1; then
echo "dropbox does not seem to be running." >&2
exit 1
fi
if [[ ! -f $inputfile ]]; then
echo "$inputfile is not valid" >&2
exit 1
fi
local mimeType=$(file -b --mime-type ${inputfile})
local isImage=false
if [[ ${mimeType} = "image/jpeg"
|| ${mimeType} = "image/gif"
|| ${mimeType} = "image/png"
]]; then
isImage=true
fi
if [[ ${isImage} = true && ${downscale} = true ]]; then
echo "Downscaling image..."
local tmpfile=$(mktemp)
convert -resize 1280x1024\> "${inputfile}" "${tmpfile}"
else
local tmpfile=${inputfile}
fi
local base=$(basename ${inputfile})
# Get "unique" filename inside dropbox public dir
local target=$(mktemp -u -p ${publicdir} -t pub_$(date +%Y%m%d%H%M)_XXXXX_${base})
# Copy file to destination inside dropbox public dir
cp "${tmpfile}" "${target}"
puburl=$(dropbox puburl $target)
if command -v xclip >/dev/null 2>&1; then
echo "Copied to clipboard"
xclip -selection clipboard <<< ${puburl}
fi
echo ${puburl}
exit 0
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment