Die 5222.de Homepage
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.

precompress.sh 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash -e
  2. #
  3. ## nginx precompress script
  4. ## by nico from https://magicbroccoli.de/
  5. #
  6. ## variables
  7. logfile=/var/log/nginx/precompressing.log
  8. # small set of if rules to define the folders
  9. if [ "$1" == "--all" ]; then
  10. Folders=("/var/www/xmpp-community.org")
  11. elif [ -d $1 ]; then
  12. Folders=("$1")
  13. else
  14. echo "Folders is undefined."
  15. exit 22
  16. fi
  17. ext="css|js|html|xml|svg|ico|eot|otf|ttf"
  18. ## functions
  19. function compressResource {
  20. gzip -c9 "$1" >"$1.gz"
  21. chown www-data:www-data "$1.gz"
  22. touch -c --reference="$1" "$1.gz"
  23. log_to_file "$(echo "Compressed: $1 > $1.gz")"
  24. }
  25. function log_to_file {
  26. # ghetto logging
  27. echo "[$(date --rfc-3339=seconds)] - $*" >> "$logfile"
  28. }
  29. ## script
  30. # precompiled list of folders
  31. for appDir in "${Folders[@]}"
  32. do
  33. # fetch all existing gzipped CSS/JavaScript/webfont files and remove files that do not have a base uncompressed file
  34. find "$appDir" -type f -regextype posix-extended -iregex ".*\\.(${ext})\\.gz$" -print0 | while read -d '' compressFile
  35. do
  36. if [[ ! -f ${compressFile%.gz} ]]; then
  37. # remove orphan gzipped file
  38. rm "$compressFile"
  39. log_to_file "Removed: $compressFile"
  40. fi
  41. done
  42. # fetch all source CSS/JS/webfont files - excluding *.src.* variants (pre-minified CSS/JavaScript)
  43. # gzip each file and give timestamp identical to that of the uncompressed source file
  44. find "$appDir" -type f -regextype posix-extended \( -iregex ".*\\.(${ext})$" \) \( ! -name "*.src.css" -and ! -name "*.src.js" \) -print0 | while read -d '' sourceFile
  45. do
  46. if [[ -f "$sourceFile.gz" ]]; then
  47. # only re-gzip if source file is different in timestamp to the existing gzip file
  48. if [[ ($sourceFile -nt "$sourceFile.gz") || ($sourceFile -ot "$sourceFile.gz") ]]; then
  49. compressResource "$sourceFile"
  50. fi
  51. else
  52. compressResource "$sourceFile"
  53. fi
  54. done
  55. done
  56. # reloading shit
  57. nginx -s reload
  58. # clearing variables
  59. unset Folders ext logfile
  60. exit 0