This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/bin/upload

29 lines
968 B
Text
Raw Normal View History

2019-09-06 14:42:53 -05:00
#!/usr/bin/env bash
2019-12-12 11:02:24 -06:00
# TODO: progress bar!
2019-09-06 14:42:53 -05:00
HOST="ld"
f="${1}"
fname="${2:-$(basename "${f}")}"
subdir="${3:-uploads}"
internal_dir="/home/daniel/services/data/files/${subdir}"
url="https://lyte.dev/${subdir}/${fname}"
uuid="$(uuidgen -t)"
2019-09-06 14:42:53 -05:00
[ "${f}" == "" ] && echo "No file provided. Exiting." >&2 && exit 2
[ ! -f "${f}" ] && echo "File '$f' does not exist. Exiting." >&2 && exit 1
ssh ld mkdir -p "${internal_dir}"
rsync_output="$(rsync --stats --ignore-existing "${f}" "${HOST}:${internal_dir}/${fname}")"
num_uploaded="$(<<< "${rsync_output}" grep -oP "Number of created files: \K\d+")"
2019-09-06 14:42:53 -05:00
if [[ $num_uploaded -eq 1 ]]; then
echo "${f} uploaded to ${url}"
elif [[ "$(curl -s -o /dev/null -w "%{http_code}" "${url}")" -eq 200 ]]; then
echo "ERROR: A file already exists at ${url}"
else
echo "${rsync_output}" >> "${HOME}/.upload.log"
echo "ERROR: The file failed to upload - perhaps rsync failed for some reason?\n See \"${HOME}/.upload.log\" for details"
fi