androidcodeexamples.com
Offcode ltd logo
ANDROID
APACHE
THUMBNAILS
SCRIPTS
GFX
World-class examples brought to you by Offcode ltd
THUMBNAILS
Thumbnail setup Thumbnail API Front page
androidcodeexamples.com

A sample thumbnail API

We'll show a simple thumbnail API that was used for years at immediatenet.com.


Sample thumbnail API

This is how it looks like:

#!/bin/sh

# immediatenet.com

# Let the bots wait..
if [ "$QUERY_STRING" = "" ]; then
        /bin/sleep 1 2>/dev/null > /dev/null
        exit
fi

cd /home/thumbapiuser/thumb_api_fast 2>/dev/null > /dev/null
if [ $? -ne 0 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/unexpected.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/unexpected.jpg
        exit
fi

SIZE=`/bin/echo "$QUERY_STRING" | /bin/sed "s/%60//g" | /usr/bin/tr --delete ';\`"<>,!@#$%^(){}[]' | /usr/bin/cut -d"&" -f 1 | /bin/sed "s/Size=//"`

SIZE_W=`echo "$SIZE" | /usr/bin/cut -d'x' -f 1`
SIZE_H=`echo "$SIZE" | /usr/bin/cut -d'x' -f 2`

SIZEOK="0"
if [ "$SIZE_W" = "1280" ]; then
        SIZEOK="1"
fi
if [ "$SIZE_W" = "1024" ]; then
        SIZEOK="1"
fi
if [ "$SIZE_W" = "800" ]; then
        SIZEOK="1"
fi
if [ "$SIZEOK" = "0" ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_size.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_size.jpg
        exit
fi

SIZEOK="0"
if [ "$SIZE_H" = "1024" ]; then
        SIZEOK="1"
fi
if [ "$SIZE_H" = "768" ]; then
        SIZEOK="1"
fi
if [ "$SIZE_H" = "600" ]; then
        SIZEOK="1"
fi

if [ "$SIZEOK" = "0" ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_size.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_size.jpg
        exit
fi

MYNAME=`/bin/echo "$QUERY_STRING" "$SIZE_W" "$SIZE_H" "15" | /usr/bin/cut -d'&' -f2- | /usr/bin/cut -b 5- | /bin/sed "s/^http:\/\///I" | /bin/sed "s/^www.//I" | /usr/bin/xxd -ps -c 100 | /usr/bin/head -1`
DIRA=`/bin/echo "$MYNAME" | /usr/bin/cut -b 1,2`
DIRB=`/bin/echo "$MYNAME" | /usr/bin/cut -b 3,4`

if [ "$DIRA" = "" ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/unexpected.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/unexpected.jpg
        exit
fi
if [ "$DIRB" = "" ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/unexpected.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/unexpected.jpg
        exit
fi

if [ -f $DIRA/$DIRB/$MYNAME.jpg ]; then
        DT=`stat -t -c %y $DIRA/$DIRB/$MYNAME.jpg`
        MODI=`date -R -u -d "$DT" | cut -d'+' -f1`
        LEN=`/usr/bin/stat -c %s $DIRA/$DIRB/$MYNAME.jpg`
        /bin/echo "Last-Modified: \"$MODI\"GMT"
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat $DIRA/$DIRB/$MYNAME.jpg
        exit
fi

/bin/echo "$HTTP_USER_AGENT" | /bin/grep "Wget" 2>/dev/null >/dev/null
if [ $? -eq 0 ]; then
        /bin/echo "wget" "$REMOTE_ADDR" "$HTTP_REFERER" >> /var/www/monitorp.html
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/unexpected.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/unexpected.jpg
        exit
fi

INFO=`/bin/echo "$QUERY_STRING" | /usr/bin/cut -d'&' -f2- | /usr/bin/cut -b 5- | /bin/sed "s/%60//g"`
if [ "$INFO" = "" ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_url.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_url.jpg
        exit
fi

cnt=`echo "$INFO" | /usr/bin/wc -c`

if [ $cnt -le 4 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_url.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_url.jpg
        exit
fi

/bin/echo "$INFO" | /usr/bin/tr '[:upper:]' '[:lower:]' | /bin/grep "file://" > /dev/null
if [ $? -eq 0 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_url.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_url.jpg
        exit
fi


/bin/echo "$INFO" | /usr/bin/tr '[:upper:]' '[:lower:]' | /bin/grep "http://" > /dev/null
if [ $? -eq 0 ]; then
        STEP=`/bin/echo "$INFO" | /usr/bin/tr '[:upper:]' '[:lower:]' | /bin/grep "http://" | /bin/sed "s/http:\/\///g" | /usr/bin/cut -d"/" -f -1 | /usr/bin/cut -d"&" -f -1`
else
        /bin/echo "$INFO" | /usr/bin/tr '[:upper:]' '[:lower:]' | /bin/grep ":/" > /dev/null
        if [ $? -eq 0 ]; then
                LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_http.jpg`
                /bin/echo "Content-Length: $LEN"
                /bin/echo "Content-type: image/jpeg"
                /bin/echo ""
                /bin/cat /home/thumbapiuser/thumb_api_errors/error_http.jpg
                exit
        fi
        STEP=`/bin/echo "$INFO" | /usr/bin/tr '[:upper:]' '[:lower:]' | /usr/bin/cut -d"/" -f -1 | /usr/bin/cut -d"&" -f -1`
fi

if [ "$STEP" = "" ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_unknown_host.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_unknown_host.jpg
        exit
fi

cnts=`echo "$STEP" | /usr/bin/wc -c`

if [ $cnts -le 4 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_url.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_url.jpg
        exit
fi

/usr/bin/nslookup -fail "$STEP" 2>/dev/null >/dev/null
if [ $? -gt 0 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_unknown_host.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_unknown_host.jpg
        exit
fi
STEPRESULTS=`/usr/bin/nslookup "$STEP"`

/bin/echo "$STEPRESULTS" | /bin/grep -i "can't" > /dev/null
if [ $? -eq 0 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_unknown_host.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_unknown_host.jpg
        exit
fi

if [ "$REMOTE_ADDR" = "88.208.244.8" ]; then
        /bin/echo "Recursion!" "$REMOTE_ADDR" "$HTTP_REFERER" >> /var/www/monitorp.html
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_recursion.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_recursion.jpg
        exit
fi

export USERNAME=thumbapiuser
export HOME=/home/thumbapiuser
export GDM_LANG=en_US.utf8
export DISPLAY=:0.0

# We don't want to run out of disk space!
hdamount=`/bin/df -x fuse.gvfs-fuse-daemon | /bin/grep "dev/sda6" | /usr/bin/awk '{print $5}' | /usr/bin/tr --delete '%'`
if [ $hdamount -ge 60 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_disk_full.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/disk_full.jpg
        exit
fi

GNMS=`/bin/ps -A | /bin/grep "gnome-web-photo" | /usr/bin/wc -l`
if [ $GNMS -gt 10 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_server_busy.jpg`
        /bin/echo "Serverbusy!" "$REMOTE_ADDR" "$HTTP_REFERER" >> /var/www/monitorp.html
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_server_busy.jpg
        exit
fi

XCHECKS=`/bin/ps -A | /bin/grep "xhostcheck" | /usr/bin/wc -l`
if [ $XCHECKS -gt 1 ]; then
        LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_server_busy.jpg`
        /bin/echo "Serverbusy!" "$REMOTE_ADDR" "$HTTP_REFERER" >> /var/www/monitorp.html
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat /home/thumbapiuser/thumb_api_errors/error_server_busy.jpg
        exit
fi

FILEN=`/bin/mktemp -u XXXXXXXX`
MYLOCA=`/bin/echo "$INFO" | /bin/sed "s/^http:\/\///I"`
/usr/bin/timeout -k 0m 50s /home/thumbapiuser/ff/gnome-web-photo-10/src/gnome-web-photo --mode=photo -w $SIZE_W -h $SIZE_H --delay=0 --force --timeout=35 http://$MYLOCA $FILEN.png 2>/dev/null > /dev/null


if [ -f $FILEN.png ]; then
        /usr/bin/convert $FILEN.png -resize 15% $FILEN.jpg > /dev/null >/dev/null
        /bin/rm $FILEN.png 2>/dev/null >/dev/null
        if [ -d $DIRA ]; then
                if [ -d $DIRA/$DIRB ]; then
                        cp $FILEN.jpg $DIRA/$DIRB/$MYNAME.jpg 2>/dev/null >/dev/null
                else
                        /bin/mkdir $DIRA/$DIRB 2>/dev/null >/dev/null
                        cp $FILEN.jpg $DIRA/$DIRB/$MYNAME.jpg 2>/dev/null >/dev/null
                fi
        else
                /bin/mkdir $DIRA 2>/dev/null >/dev/null
                /bin/mkdir $DIRA/$DIRB 2>/dev/null >/dev/null
                cp $FILEN.jpg $DIRA/$DIRB/$MYNAME.jpg 2>/dev/null >/dev/null
        fi
        LEN=`/usr/bin/stat -c %s $FILEN.jpg`
        /bin/echo "Content-Length: $LEN"
        /bin/echo "Content-type: image/jpeg"
        /bin/echo ""
        /bin/cat $FILEN.jpg
        /bin/rm $FILEN.jpg 2>/dev/null >/dev/null
        /bin/echo "$INFO" "$HTTP_REFERER" >> /var/www/monitorp.html
        /bin/cp -P /var/www/artwork/random_images/image3.jpg /var/www/artwork/random_images/image4.jpg 2>/dev/null >/dev/null
        /bin/cp -P /var/www/artwork/random_images/image2.jpg /var/www/artwork/random_images/image3.jpg 2>/dev/null >/dev/null
        /bin/cp -P /var/www/artwork/random_images/image1.jpg /var/www/artwork/random_images/image2.jpg 2>/dev/null >/dev/null
        /bin/cp -P /var/www/artwork/random_images/image0.jpg /var/www/artwork/random_images/image1.jpg 2>/dev/null >/dev/null
        /bin/ln -sf /home/thumbapiuser/thumb_api_fast/$DIRA/$DIRB/$MYNAME.jpg /var/www/artwork/random_images/image0.jpg 2>/dev/null >/dev/null
        exit
fi

LEN=`/usr/bin/stat -c %s /home/thumbapiuser/thumb_api_errors/error_timeout.jpg`
/bin/echo "Content-Length: $LEN"
/bin/echo "Content-type: image/jpeg"
/bin/echo ""
/bin/cat /home/thumbapiuser/thumb_api_errors/error_timeout.jpg
/bin/echo "FAIL:" "$INFO" "mediumsize" >> /var/www/monitorp.html
/bin/cp /home/thumbapiuser/thumb_api_errors/m154_115.jpg $DIRA/$DIRB/$MYNAME.jpg 2>/dev/null >/dev/null
exit