]> git.plutz.net Git - busy/blobdiff - actions/image_upload.cgi
porting busy to confetti framework
[busy] / actions / image_upload.cgi
diff --git a/actions/image_upload.cgi b/actions/image_upload.cgi
new file mode 100755 (executable)
index 0000000..bc66728
--- /dev/null
@@ -0,0 +1,105 @@
+#!/bin/zsh
+# Copyright 2011 Paul Haensch
+# This file is part of Kinoserver
+#
+# Kinoserver is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Kinoserver is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Kinoserver.  If not, see <http://www.gnu.org/licenses/>.
+
+page="$(egrep -o '(^|&)p=[a-zA-Z0-9]+(&|$)' <<<"${QUERY_STRING}" |sed 's:.*p=::;s:&::g')"
+info="$(egrep -o '(^|&)i=[0-9]{10}_[0-9]{3}(&|$)' <<<"${QUERY_STRING}" |tr -d 'i=&')"
+[ -z "$page" ] && page="Locations"
+[ -z "$info" ] && info="$(date +%s)_$(apg -M N -a 1 -n 1 -m 3 -x 3)"
+
+if [ -z "${CONTENT_LENGTH}" -o "${CONTENT_LENGTH}" -le 0 ]; then
+  echo -n "Location: http://${HTTP_HOST}/?p=Error&r=tooSmall\n\n"
+  exit 0
+elif [ "${CONTENT_LENGTH}" -gt 33554432 ]; then
+  echo -n "Location: http://${HTTP_HOST}/?p=Error&r=tooBig\n\n"
+  exit 0
+fi
+
+case "$page" in
+  Photographs) imgpath="../Photographs/${info}";;
+  Locations) imgpath="../Locations";;
+esac
+basename="$(date +%s)_$(apg -M N -a 1 -n 1 -m 3 -x 3)"
+filename="$imgpath/${basename}_meta.upload"
+mkdir -p "$imgpath"
+
+n=1
+(head -c "${CONTENT_LENGTH}") |while line="$(line)"; do
+  if [ -z "${delim}" ]; then
+    delim="${line}"
+    end="$(echo "${delim}" |sed 's;\r;--\r;')"
+  fi
+  if [ "${line}" = "${delim}" ]; then
+    field=$(egrep -o 'name="[a-zA-Z0-9]+"' <<<$(line) |sed 's,name=",,;s,"$,,')
+    line="$(line)"                             #consume a newline
+    [ "$field" = "picture" ] && line="$(line)" #consume additional newline for file upload
+    if [ -n "$filename" ]; then                        #finish last uploaded file by removing trailing CR|LF
+      size=$(stat -c %s "$filename")
+      truncate -s $(($size - 2)) "$filename"
+    fi
+    if [ "$field" = "picture" ]; then
+      if [ "$page" = "Locations" -a "$filename" = "${imgpath}/${basename}_meta.upload" ]; then
+       echo -e "${lat}\t${lon}\t${title}" > "$filename"
+       echo -e "${tags}" |tr '\n' '|' >> "$filename"
+       echo -e "\n${desc}" >> "$filename"
+      elif [ "$page" = "Photographs" -a "$filename" = "${imgpath}/${basename}_meta.upload" ]; then
+       echo -e "name=$name" > "$filename"
+      fi
+      filename="${imgpath}/${basename}_$n.upload"
+      touch "$filename"
+    fi
+    n=$((n+1))
+  elif [ "${line}" = "${end}" ]; then
+    size=$(stat -c %s $filename)
+    truncate -s $(($size - 2)) $filename
+  else
+    case "$field" in
+      picture) echo -E "$line" >>$filename
+       ;;
+      title)   title="$(sed 's/\t/ /g;s/\r|\n//g;s/\\/\\\\/g' <<<"$line")"
+       ;;
+      name)    name="$(sed 's/\t/ /g;s/\r|\n//g;s/\\/\\\\/g' <<<"$line")"
+       ;;
+      lat)     lat="$(egrep '[.0-9]{1,8}' <<<"$line" |tr -d '\r')"
+       ;;
+      lon)     lon="$(egrep '[.0-9]{1,8}' <<<"$line" |tr -d '\r')"
+       ;;
+      tags)    tags="${tags}$(sed 's/\t/ /g;s/\r//g;s/\\/\\\\/g' <<<"$line")\n"
+       ;;
+      desc)    desc="${desc}$(sed 's/\t/ /g;s/\r//g;s/\\/\\\\/g' <<<"$line")\n"
+       ;;
+    esac
+  fi
+done
+
+n=1
+for each in ${imgpath}/${basename}_<0-9999>.upload; do
+  convert "$each" "${imgpath}/${basename}_$n.jpg"
+  rm "$each"
+  if [ "$page" = "Locations" -a -f "${imgpath}/${basename}_$n.jpg" ]; then
+    info="${basename}_$n"
+    cp "${imgpath}/${basename}_meta.upload" "${imgpath}/${basename}_${n}.meta"
+  fi
+  n=$((n+1))
+done
+[ "$page" = "Photographs" -a ! -f "${imgpath}.meta" ] && cp "${imgpath}/${basename}_meta.upload" "${imgpath}.meta"
+rm "${imgpath}/${basename}_meta.upload"
+
+if [ -z "$info" ]; then
+  echo -n  "Location: http://${HTTP_HOST}/?p=Error&r=noPic\n\n"
+else
+  echo -n "Location: http://${HTTP_HOST}/?p=${page}&i=$info#$info\n\n"
+fi