diff --git a/scripts/neocities.sh b/scripts/neocities.sh new file mode 100644 index 0000000..963e061 --- /dev/null +++ b/scripts/neocities.sh @@ -0,0 +1,53 @@ +#!/bin/bash + + +# This script takes care of uploading all artifacts in the public/ directory +# To neocities via its API, This script is meant to be used in CI only +# however it may occasionally be tested elsewhere + + +upload_file() { + file_path="$1" + uri_path="${file_path:6}" + + if [ -z "$file_path" ];then + echo ERROR: upload_file Received an empty parameter exiting now >&2 + return 1 + fi + + # Sub-routine uploads a single file for + base='https://neocities.org/api/upload' + # Curl gives us the exit code for this sub-routine's exit code + curl \ + -H "Authorization: Bearer ${API_KEY}" \ + -F "${uri_path}=@${file_path}" +} + +feed_files() { + # 2 seconds between API hits to ease up on Neocities + SLEEP_TIME=2 + + if [ ! -z "$CUSTOM_SLEEP_TIME" ]; then + SLEEP_TIME=$CUSTOM_SLEEP_TIME + fi + + # Main uploading section + while read -r line; do + if upload_file "$line"; then + echo Uploaded $line + else + echo Failed $line >&2 + fi + + sleep $SLEEP_TIME + done < /dev/stdin +} + +if [ -d public/ ];then + echo Found public directory + # Upload all files that we wish to upload + find public/ -type f | grep '\.[a-z]*' | feed_files +else + echo No public directory found + exit 1 +fi diff --git a/upload.sh b/upload.sh deleted file mode 100755 index 462deb2..0000000 --- a/upload.sh +++ /dev/null @@ -1,98 +0,0 @@ -#!/bin/bash - -if [ ! -z .env ];then - # no env means we have to use the neocities var - export KEY=$NEOCITIES_KEY -else - source .env -fi - -url="https://neocities.org" -site=.mirror -auth="Authorization: Bearer $KEY" - - -_upload_html() { - # fix this comparison cuz its busted - if [ ${1:0:1} = "" ];then - echo Path must start with ./ - exit 1 - fi - for f in $@;do - remote_path=${f:1} - echo curl -s -o /dev/null -w '%{http_code}' -H "auth" -F "$remote_path=@$f" $url/api/upload - curl -s -o /dev/null -w '%{http_code}' -H "$auth" -F "$remote_path=@$f" $url/api/upload - done -} - -_upload_media() { - [[ "${1:0:1}" == "./" ]] && \ - echo 'Path must start with ./' && \ - exit 1 - - for f in $@;do - remote_path=${f:1} - echo curl -s -o /dev/null -w '%{http_code}' -H "auth" -F "$remote_path=@$f" $url/api/upload - curl -s -o /dev/null -w '%{http_code}' -H "$auth" -F "$remote_path=@$f" $url/api/upload - done -} - -_upload_all_media() { - for file in $(find media -type f);do - echo Uploading media: $file - curl -H "$auth" -F "$file=@$file" $url/api/upload -s > /dev/null - done -} - -html() { - # Total reupload of the site's html - _upload_html $(find -name '*.html') -} - -roots() { - _upload_html ./*.html -} - -folder() { - if [ -z $1];then - echo No folder specified > /dev/stderr - else - pushd $1 > /dev/null - _upload_html ./*.html - popd > /dev/null - fi -} - -_help() { -cat < Reuploads all the html content - r => Uploads all root html pages - f [FOLDER] => Upload all html in [FOLDER] - s [PATH] => Upload single html page with mirrord path - m [PATH] => Upload any amount of media files - M => Upload all available media -EOF -} - -if [ -z $1 ];then - _help -else - pushd $site > /dev/null - while getopts 'a::Hhrf:s:m:M' opt;do - case $opt in - H) _help;; - h) _upload_html $(find -name '*.html');; # Upload all html content - r) _upload_html ./*.html;; # Upload all root html pages - f) _upload_html $2/*.html;;# Upload html in the spec'd folder - s) _upload_html $2;;# upload single html page - m) _upload_media $2;;# upload raw file - M) _upload_all_media;; - esac - done - popd > /dev/null -fi -