Die 5222.de Homepage https://www.5222.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
2.0 KiB

#!/bin/bash -e
#
## nginx precompress script
## by nico from https://magicbroccoli.de/
#
## variables
logfile=/var/log/nginx/precompressing.log
# small set of if rules to define the folders
if [ "$1" == "--all" ]; then
Folders=("/var/www/xmpp-community.org")
elif [ -d $1 ]; then
Folders=("$1")
else
echo "Folders is undefined."
exit 22
fi
ext="css|js|html|xml|svg|ico|eot|otf|ttf"
## functions
function compressResource {
gzip -c9 "$1" >"$1.gz"
chown www-data:www-data "$1.gz"
touch -c --reference="$1" "$1.gz"
log_to_file "$(echo "Compressed: $1 > $1.gz")"
}
function log_to_file {
# ghetto logging
echo "[$(date --rfc-3339=seconds)] - $*" >> "$logfile"
}
## script
# precompiled list of folders
for appDir in "${Folders[@]}"
do
# fetch all existing gzipped CSS/JavaScript/webfont files and remove files that do not have a base uncompressed file
find "$appDir" -type f -regextype posix-extended -iregex ".*\\.(${ext})\\.gz$" -print0 | while read -d '' compressFile
do
if [[ ! -f ${compressFile%.gz} ]]; then
# remove orphan gzipped file
rm "$compressFile"
log_to_file "Removed: $compressFile"
fi
done
# fetch all source CSS/JS/webfont files - excluding *.src.* variants (pre-minified CSS/JavaScript)
# gzip each file and give timestamp identical to that of the uncompressed source file
find "$appDir" -type f -regextype posix-extended \( -iregex ".*\\.(${ext})$" \) \( ! -name "*.src.css" -and ! -name "*.src.js" \) -print0 | while read -d '' sourceFile
do
if [[ -f "$sourceFile.gz" ]]; then
# only re-gzip if source file is different in timestamp to the existing gzip file
if [[ ($sourceFile -nt "$sourceFile.gz") || ($sourceFile -ot "$sourceFile.gz") ]]; then
compressResource "$sourceFile"
fi
else
compressResource "$sourceFile"
fi
done
done
# reloading shit
nginx -s reload
# clearing variables
unset Folders ext logfile
exit 0