summaryrefslogtreecommitdiff
path: root/recipes-core/lighttpd/files/lighttpd_custom_images_setup
diff options
context:
space:
mode:
Diffstat (limited to 'recipes-core/lighttpd/files/lighttpd_custom_images_setup')
-rw-r--r--recipes-core/lighttpd/files/lighttpd_custom_images_setup57
1 files changed, 57 insertions, 0 deletions
diff --git a/recipes-core/lighttpd/files/lighttpd_custom_images_setup b/recipes-core/lighttpd/files/lighttpd_custom_images_setup
new file mode 100644
index 0000000..ecd5f46
--- /dev/null
+++ b/recipes-core/lighttpd/files/lighttpd_custom_images_setup
@@ -0,0 +1,57 @@
+#!/bin/bash
+# Detects mime types for UI Customization images and generates according Lighttpd config fragment
+
+CONFIG_PATH="/var/run/config/lighttpd_custom_images.conf"
+IMAGE_PATH="/var/www/static/images/"
+MAGIC_DB_PATH="/usr/share/misc/magic-images.mgc"
+shopt -s nullglob
+
+echoerr() {
+ echo "$@" 1>&2
+}
+
+generate_mime_assign() {
+ local IMAGE="$1"
+ local OUTPUT=$(file -ib "$IMAGE" --magic-file "$MAGIC_DB_PATH")
+ local CONTENT_TYPE
+
+ if [ "$?" -ne "0" ] || [[ "$OUTPUT" == "" ]] || [[ "$OUTPUT" == *"cannot open"* ]]; then
+ echoerr "Failed to run file(1): ${?}; ${OUTPUT}"
+ return 1
+ fi
+
+ CONTENT_TYPE="$OUTPUT"
+ cat <<END
+\$HTTP["url"] =~ "/static/images/$IMAGE" {
+ mimetype.assign = ("" => "$CONTENT_TYPE")
+ }
+END
+}
+
+process_files() {
+ local INDENT=" "
+ local ELSE_STRING=""
+ local FRAGMENT
+
+ for IMAGE in custom_*; do
+ INDENT=" "
+
+ FRAGMENT=$(generate_mime_assign $IMAGE)
+ if [ "$?" -eq "0" ]; then
+ echo "${INDENT}${ELSE_STRING}${FRAGMENT}" >> "$CONFIG_PATH"
+ ELSE_STRING="else "
+ fi
+ done
+}
+
+echo "Generating $CONFIG_PATH"
+
+# truncate and write head
+cat > "$CONFIG_PATH" <<END
+\$HTTP["url"] =~ "/static/images/custom_" {
+END
+
+cd "$IMAGE_PATH" && process_files
+
+# write tail (closing brace)
+echo "}" >> "$CONFIG_PATH"