summaryrefslogtreecommitdiff
path: root/recipes-core/lighttpd/files/lighttpd_custom_images_setup
blob: ecd5f4635529ed8e819d32f084f0de587e98ec70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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"