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
906 B
Plaintext
Raw Normal View History

2020-01-22 08:34:12 -06:00
#!/usr/bin/env sh
2019-09-06 14:42:53 -05:00
2019-12-12 11:02:24 -06:00
# TODO: progress bar!
HOST="faceless"
2019-09-06 14:42:53 -05:00
f="${1}"
fname="${2:-$(basename "${f}")}"
subdir="${3:-uploads}"
2020-11-05 23:32:22 -06:00
internal_dir="/home/daniel/public-static-files/${subdir}"
url="https://files.lyte.dev/${subdir}/${fname}"
2019-09-06 14:42:53 -05:00
2020-01-22 08:34:12 -06:00
[ "${f}" = "" ] && echo "No file provided. Exiting." >&2 && exit 2
2019-09-06 14:42:53 -05:00
[ ! -f "${f}" ] && echo "File '$f' does not exist. Exiting." >&2 && exit 1
2020-01-22 08:34:12 -06:00
if [ "$(curl -s -o /dev/null -w "%{http_code}" "${url}")" -eq 200 ]; then
echo "ERROR: A file already exists at ${url}"
exit 3
fi
2020-12-14 11:35:35 -06:00
ssh "$HOST" mkdir -p "$(dirname "${internal_dir}/${fname}")"
rsync --progress --no-owner --no-group --chmod=644 --ignore-existing "${f}" "${HOST}:${internal_dir}/${fname}" | tee "${HOME}/.upload.log"
2020-01-22 08:34:12 -06:00
code="$?"
2020-01-22 17:25:47 -06:00
echo "Uploaded to: ${url}"
2019-09-06 14:42:53 -05:00
2020-01-22 08:34:12 -06:00
if [ "$code" -ne 0 ]; then
printf "ERROR: The file failed to upload - perhaps rsync failed for some reason?\n See \"%s/.upload.log\" for details\n" "$HOME"
fi