Commit 686cfde6 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add support for serving static files from S3

... currently not supporting the Admin dashboard
parent bed5115a
Loading
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@ setup_database() {

setup_s3() {
	# https://github.com/humanmade/S3-Uploads
	# https://github.com/dannyvankooten/wp-cdn-loader

	declare -a configs=( "${!S3_ENDPOINT_@}" )
	[[ ${#configs[*]} -gt 0 ]] || return 0
@@ -93,11 +94,16 @@ setup_s3() {
		return

	composer require humanmade/s3-uploads
	composer require dannyvankooten/wp-cdn-loader

	# S3-Uploads looks for vendor/ in the wrong place
	ln -sf /app/vendor wp-content/plugins/s3-uploads/

	wp plugin activate s3-uploads
	wp plugin activate wp-cdn-loader

	S3_STATIC_PREFIX=static/$(wp core version)
	wp config set DVK_CDN_URL ${CDN_URL-${S3_UPLOADS_ENDPOINT}/${S3_STATIC_PREFIX}}

	[[ -v S3_UPLOADS_USE_LOCAL ]] &&
		wp config set S3_UPLOADS_USE_LOCAL true --raw
@@ -166,6 +172,38 @@ collect_static()
		--relative \
		--times \
		. static/

	unset IFS
	if wp config has S3_UPLOADS_BUCKET_URL; then
		# Use associative arrays as the closest to sets available
		declare -A UPLOADED=() NEED_UPLOAD=()
		local name

		# Build a set of uploaded files
		for name in $(wp s3-uploads ls ${S3_STATIC_PREFIX}); do
			UPLOADED+=( [$name]= )
		done

		_check_name() {
			[[ ! -f $1 || -v UPLOADED[${1#static/}] ]] && return
			local dir curdir=${1%/*}
			for dir in "${!NEED_UPLOAD[@]}"; do
				[[ $curdir =~ ^$dir ]] && return
				[[ $dir =~ ^$curdir ]] && unset 'NEED_UPLOAD[$dir]'
			done
			NEED_UPLOAD+=( [$curdir]= )
		}

		# Build a set of directories that need syncing to get all missing files
		for name in static/**; do
			_check_name $name
		done

		# Upload the needed directories
		for name in "${!NEED_UPLOAD[@]}"; do
			wp s3-uploads upload-directory "$name" "${S3_STATIC_PREFIX}/${name#static/}" --verbose
		done
	fi
}

next_cron()