Soshot/bin/thumb_server.sh
Knah Tsaeb c665a3cdec [add] #453 : Remove image older than
[add] #466 : Another dir organisation
2013-05-03 11:06:58 +02:00

102 lines
3.6 KiB
Bash
Executable file

#!/bin/bash
# inspirated source http://www.cambus.net/creating-thumbnails-using-phantomjs-and-imagemagick/ for global idea
# https://gist.github.com/rsvp/1171304 for httpstatus code
# All info at http://forge.leslibres.org/projects/soshot
site=$1 # url for thumbshot
hashUrl=$2 # hash(url) is the name of final image
thumbSize=$3 # size of thumb widthxheight ex : 190x90
sizeNameDir=$4 # name of size dir
onlyThumb=$5 # make only thumbshot no full size image
waitForResult=$6 # if true we try to make soon as possible or add to queue
maxThread=1 # max parralle work. For me the best is nb core - 1
timeOut=60 # default time out, after this time the site are declared in error
log=false # log all generation success and error
randomSleep=`echo $((RANDOM%20))`
optimizeTool='' # external tool form optimize png
maxQueue=50 # max process in queue
currentProcess=$(ps -e | grep -v grep | grep thumb_ | wc -l)
firstLevel=${hashUrl:0:2}
secondLevel=${hashUrl:2:2}
startPath=$sizeNameDir/$firstLevel/$secondLevel/
mkdir -p "cache/img/$startPath"
if [ -f "cache/config/serverOptions.php" ]
then
source "cache/config/serverOptions.php"
fi
if [ $currentProcess -gt $maxQueue ]
then
exit
fi
if [ ! $waitForResult ]
then
sleep `echo $((RANDOM%20))`
while [[ `ps -e | grep -v grep | grep phantomjs | wc -l` -ge $maxThread ]]
do
sleep `echo $((RANDOM%20))`
done
fi
start_time=`date +%s`
timeout $timeOut ./bin/phantomjs --disk-cache=false --local-storage-path=/bin/ --ignore-ssl-errors=true /bin/rasterize.js "$site" "cache/tmp/$hashUrl.png"
if [ ! -f "cache/tmp/$hashUrl.png" ]
then
echo 0
errorCode=`echo $(curl -k --write-out %{http_code} --silent -S --connect-timeout $timeOut \--no-keepalive --output /dev/null $site)`
case $errorCode in
000) cp "bin/000.png" "cache/img/$startPath""$hashUrl""_thumb.png" && cp "bin/000.png" "cache/img/$startPath""$hashUrl.png" ;;
404) cp "bin/404.png" "cache/img/$startPath""$hashUrl""_thumb.png" && cp "bin/404.png" "cache/img/$startPath""$hashUrl.png" ;;
*) cp "bin/error.png" "cache/img/$startPath""$hashUrl""_thumb.png" && cp "bin/error.png" "cache/img/$startPath""$hashUrl.png" ;;
esac
if $log
then
end_time=`date +%s`
logDate=`date +'[%a %d %b %Y] [%H:%M:%S]'`
echo $logDate `expr $end_time - $start_time`s --- ERROR --- httpstatus $errorCode --- $site >> 'cache/logs/logs.txt'
fi
exit
fi
if [ $onlyThumb == 1 ]
then
convert "cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 -filter Lanczos -thumbnail "$thumbSize" "cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "cache/img/$startPath""$hashUrl""_thumb.png"
fi
else
convert "cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 "cache/img/$startPath""$hashUrl.png"
convert "cache/img/$startPath""$hashUrl.png" -filter Lanczos -thumbnail "$thumbSize" "cache/img/$startPath""$hashUrl""_thumb.png"
if [ $optimizeTool ]
then
eval $optimizeTool "cache/img/$startPath""$hashUrl""_thumb.png" "cache/img/$startPath""$hashUrl.png"
fi
fi
rm "cache/tmp/$hashUrl.png"
if [ $onlyThumb ]
then
if [[ -f "cache/img/$startPath""$hashUrl""_thumb.png" ]]
then
echo 1
else
echo 0
fi
else
if [[ -f "cache/img/$startPath""$hashUrl""_thumb.png" && -f "cache/img/$startPath""$hashUrl.png" ]]
then
echo 1
else
echo 0
fi
fi
if $log
then
end_time=`date +%s`
logDate=`date +'[%a %d %b %Y] [%H:%M:%S]'`
echo $logDate `expr $end_time - $start_time`s --- $site >> 'cache/logs/logs.txt'
fi