Skip to content

Update using-fswatch.md to use CHIBISAFE_WATCH_DIR #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

John-Paul-R
Copy link

No description provided.

@John-Paul-R
Copy link
Author

I actually also have this set of modifications to the script to make it work on my linux machine, if wanted I can add to the changeset:

#!/usr/bin/env sh

setup_environment() {
	script_dir="$(dirname "$0")"
	script_dir="$(realpath "${script_dir}")"
	env_file="${script_dir}/chibisafe_watcher.env"

	if [ -f "${env_file}" ]; then
		source "${env_file}"
	else
		echo "No environment file found."
		exit 1
	fi
}

upload() {
	filename=$1
	dump_file="$(mktemp --suffix=-chibisafe.dump)"

	echo "${filename}" >"${dump_file}"
	stat --format="%m" "${filename}" >>"${dump_file}"
	curl -D "${dump_file}" \
		-H "x-api-key: ${CHIBISAFE_API_KEY}" \
		-H "albumuuid: ${CHIBISAFE_ALBUM_UUID}" \
		-F "file[]=@${filename}" \
		-s "${CHIBISAFE_REQUEST_URL}"
	rm "$dump_file"
	printf '\n'
}

start_watch() {
	echo "Watching directory: ${CHIBISAFE_WATCH_DIR}"
	fswatch -0 -x "${CHIBISAFE_WATCH_DIR}" | while read -d "" event_line; do
		path=$(echo "${event_line}" | cut -d' ' -f1)
		event_types=$(echo "${event_line}" | cut -d' ' -f2-)

		#echo "general event: ${path} ${event_types}"
		if [[ "${event_types}" == *"MovedTo"* || "${event_types}" == *"Created"* ]]; then
			# If event is on the directory itself (for moves/renames)
			if [ "${path}" = "${CHIBISAFE_WATCH_DIR}" ]; then
				find "${CHIBISAFE_WATCH_DIR}" -type f -cmin -0.1 | while read -r new_file; do
					echo "Potential File Move: ${new_file}"
					upload "${new_file}"
				done
			# If event is on a specific file
			elif [ -f "${path}" ]; then
				echo "Simple File Create: ${path} (${event_types})"
				upload "${path}"
			fi
		fi
	done
}

setup_environment
start_watch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant