Skip to content

Instantly share code, notes, and snippets.

@isaaclw
Last active November 22, 2019 20:51
Show Gist options
  • Save isaaclw/96c6c87373ad34e433066d1720e7c022 to your computer and use it in GitHub Desktop.
Save isaaclw/96c6c87373ad34e433066d1720e7c022 to your computer and use it in GitHub Desktop.
Wordpress Upload Daemon #bash #wordpress #ftp
#!/bin/bash
SHARE='/opt/shuttle'
FROM_FOLDER="$SHARE/Ready to Transfer"
TRANS_FOLDER="$SHARE/Transferred"
ERROR_FOLDER="$SHARE/Didnt Upload"
mkdir -p "$FROM_FOLDER" "$TRANS_FOLDER" "$ERROR_FOLDER"
# The idea is that files show up in $FROM_FOLDER, and are uploaded via ftp to the wordpress folder.
# If they transfer successfully, they're moved to $TRANS_FOLDER
# If they fail, they're tranferred to $ERROR_FOLDER
# Files are uploaded, and then moved to prevent the file from showing up in wordpress too soon.
#FTP
USERNAME=
HOSTNAME=
EMAIL_TO=(
)
UPLOAD_TO="/home/$USERNAME/tmp/uploads"
FINAL_FOLDER="/home/$USERNAME/public_html/wp-content/uploads"
email() {
file="$1"
for addr in ${EMAIL_TO[@]}; do
echo "The file: $file was uploaded to bigspringmennonite.org
Log in here: http://bigspringmennonite.org/wp-admin/" | mutt -s 'File uploaded' -- "$addr"
done
}
upload() {
FILE="$1"
cd "$FROM_FOLDER"
# Check if the file already exists
#echo "ls '$FINAL_FOLDER/$FILE'" | sftp -b - "$USERNAME@$HOSTNAME"
#[ $? -eq 0 ] && { echo "File exists in $FINAL_FOLDER" && mv -v "$FILE" "$ERROR_FOLDER"; exit 1; }
echo "ls '$UPLOAD_TO/$FILE'" | sftp -b - "$USERNAME@$HOSTNAME"
[ $? -eq 0 ] && { echo "File exists in $UPLOAD_TO" && mv -v "$FILE" "$ERROR_FOLDER"; exit 1; }
echo "cd $UPLOAD_TO
put -af '$FILE'
rename '$UPLOAD_TO/$FILE' '$FINAL_FOLDER/$FILE'
quit" | sftp "$USERNAME@$HOSTNAME"
mv -v "$FILE" "$TRANS_FOLDER/"
email "$FILE"
}
if [ -n "$1" ]; then
[ ! -e "$FROM_FOLDER/$1" ] && { echo "$1 needs to be a file"; exit 1; }
upload "$1"
else
echo "Running Monitor"
while true; do
if [ -n "$(ls "$FROM_FOLDER")" ]; then
item="$(ls "$FROM_FOLDER" | sort | head -1)"
"$0" "$item"
else
sleep 10
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment