2013-03-14 11:56:21 +01:00
|
|
|
#!/bin/bash
|
2013-03-26 15:22:13 +01:00
|
|
|
# inspirated source http://www.cambus.net/creating-thumbnails-using-phantomjs-and-imagemagick/ for global idea
|
2013-03-19 17:07:13 +01:00
|
|
|
# https://gist.github.com/rsvp/1171304 for httpstatus code
|
|
|
|
# All info at http://forge.leslibres.org/projects/soshot
|
2013-03-14 11:56:21 +01:00
|
|
|
|
2013-03-26 15:22:13 +01:00
|
|
|
site=$1 # url for thumbshot
|
2015-07-09 17:21:32 +02:00
|
|
|
hashUrl=$2 # hash(url) is the name of final image
|
2013-03-19 17:07:13 +01:00
|
|
|
thumbSize=$3 # size of thumb widthxheight ex : 190x90
|
2013-05-03 11:01:17 +02:00
|
|
|
sizeNameDir=$4 # name of size dir
|
2015-07-09 17:21:32 +02:00
|
|
|
renderType=$5 # make only thumbshot / thumbshot + realSize / thumbshot + complete / thumbshot + complete + realSize
|
2013-05-03 11:01:17 +02:00
|
|
|
waitForResult=$6 # if true we try to make soon as possible or add to queue
|
2015-07-13 12:34:33 +02:00
|
|
|
noErrorLog=$7
|
2013-08-16 11:56:15 +02:00
|
|
|
|
2013-05-31 12:28:30 +02:00
|
|
|
##########################################
|
|
|
|
##### DON'T EDIT THIS PARAM USE #####
|
|
|
|
##### cache/config/serverOptions.php #####
|
2013-08-16 11:56:15 +02:00
|
|
|
##### for overwrite options #####
|
2013-05-31 12:28:30 +02:00
|
|
|
##########################################
|
2013-04-09 16:47:04 +02:00
|
|
|
maxThread=1 # max parralle work. For me the best is nb core - 1
|
2016-12-12 15:27:29 +01:00
|
|
|
timeOut=120 # default time out, after this time the site are declared in error
|
2013-03-29 14:27:43 +01:00
|
|
|
log=false # log all generation success and error
|
2016-12-12 15:27:29 +01:00
|
|
|
randomSleep=`echo $((RANDOM%40))`
|
2013-03-26 15:22:13 +01:00
|
|
|
optimizeTool='' # external tool form optimize png
|
|
|
|
maxQueue=50 # max process in queue
|
2016-12-12 15:55:31 +01:00
|
|
|
currentProcess=$(ps ax | grep -v grep | grep thumb_server.sh | wc -l)
|
2013-04-02 15:42:06 +02:00
|
|
|
firstLevel=${hashUrl:0:2}
|
|
|
|
secondLevel=${hashUrl:2:2}
|
2013-05-03 11:01:17 +02:00
|
|
|
startPath=$sizeNameDir/$firstLevel/$secondLevel/
|
2015-01-29 14:32:45 +01:00
|
|
|
getArch=$(getconf LONG_BIT)
|
2015-07-09 17:21:32 +02:00
|
|
|
#echo $getArch
|
2016-12-12 15:27:29 +01:00
|
|
|
|
2016-12-12 15:55:31 +01:00
|
|
|
if [ -f "../cache/config/serverOptions.php" ]
|
|
|
|
then
|
|
|
|
source "../cache/config/serverOptions.php"
|
|
|
|
fi
|
|
|
|
|
2016-12-12 15:27:29 +01:00
|
|
|
if [ $currentProcess -gt $maxQueue ]
|
|
|
|
then
|
|
|
|
rm "../cache/tmp/$hashUrl.lock"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2015-07-13 12:34:33 +02:00
|
|
|
if [ "$site" = "manual" ]
|
2013-05-31 12:28:30 +02:00
|
|
|
then
|
2013-08-16 11:56:15 +02:00
|
|
|
site=$(cat "../cache/tmp/manual.txt")
|
|
|
|
echo '' > "../cache/tmp/manual.txt"
|
2013-05-31 12:28:30 +02:00
|
|
|
fi
|
|
|
|
|
2013-08-16 11:56:15 +02:00
|
|
|
mkdir -p "../cache/img/$startPath"
|
2013-03-26 15:22:13 +01:00
|
|
|
|
2013-03-19 17:07:13 +01:00
|
|
|
if [ ! $waitForResult ]
|
|
|
|
then
|
2016-12-12 15:27:29 +01:00
|
|
|
sleep $randomSleep
|
2016-12-12 16:03:56 +01:00
|
|
|
while [[ `ps -ax | grep -v grep | grep phantomjs | wc -l` -ge $maxThread ]]
|
2013-03-19 17:07:13 +01:00
|
|
|
do
|
2016-12-12 15:27:29 +01:00
|
|
|
sleep $randomSleep
|
2013-03-19 17:07:13 +01:00
|
|
|
done
|
|
|
|
fi
|
2013-03-26 15:22:13 +01:00
|
|
|
|
2013-03-19 17:07:13 +01:00
|
|
|
start_time=`date +%s`
|
2015-01-29 14:32:45 +01:00
|
|
|
if [ $getArch -eq 64 ]
|
2015-07-09 17:21:32 +02:00
|
|
|
then
|
2016-12-12 15:27:29 +01:00
|
|
|
timeout $timeOut nice -n 5 ./phantomjs_x64 --disk-cache=false --local-storage-path=../cache/tmp --ignore-ssl-errors=true --web-security=false rasterize.js "$site" "../cache/tmp/$hashUrl.png"
|
|
|
|
exitStatus=$?
|
2015-01-09 17:06:23 +01:00
|
|
|
else
|
2016-12-12 15:27:29 +01:00
|
|
|
timeout $timeOut nice -n 5 ./phantomjs_x86 --disk-cache=false --local-storage-path=../cache/tmp --ignore-ssl-errors=true --web-security=false rasterize.js "$site" "../cache/tmp/$hashUrl.png"
|
|
|
|
exitStatus=$?
|
|
|
|
fi
|
|
|
|
|
|
|
|
#echo $exitStatus
|
|
|
|
|
|
|
|
if [ $exitStatus -eq 124 ]
|
|
|
|
then
|
|
|
|
echo 0
|
|
|
|
cp "000.png" "../cache/img/$startPath""$hashUrl""_thumb.png" && cp "000.png" "../cache/img/$startPath""$hashUrl.png" && cp "000.png" "../cache/img/$startPath""$hashUrl""_complete.png"
|
|
|
|
rm "../cache/tmp/$hashUrl.lock"
|
|
|
|
exit
|
2015-01-09 17:06:23 +01:00
|
|
|
fi
|
|
|
|
|
2015-07-13 14:27:22 +02:00
|
|
|
if [[ ! -f "../cache/tmp/$hashUrl.png" ]]
|
2013-03-19 17:07:13 +01:00
|
|
|
then
|
|
|
|
echo 0
|
2015-07-10 17:19:53 +02:00
|
|
|
errorCode=`echo $(curl -k -L --write-out %{http_code} --silent -S --connect-timeout $timeOut \--no-keepalive --output /dev/null $site)`
|
2013-05-31 12:28:30 +02:00
|
|
|
if [ $site == 'manual' ]
|
|
|
|
then
|
|
|
|
errorCode='manual'
|
|
|
|
fi
|
2015-07-13 14:27:22 +02:00
|
|
|
#echo $errorCode
|
2013-03-19 17:07:13 +01:00
|
|
|
case $errorCode in
|
2015-07-10 17:19:53 +02:00
|
|
|
000) cp "000.png" "../cache/img/$startPath""$hashUrl""_thumb.png" && cp "000.png" "../cache/img/$startPath""$hashUrl.png" && cp "000.png" "../cache/img/$startPath""$hashUrl""_complete.png";;
|
|
|
|
200) cp "error.png" "../cache/img/$startPath""$hashUrl""_thumb.png" && cp "error.png" "../cache/img/$startPath""$hashUrl.png" && cp "error.png" "../cache/img/$startPath""$hashUrl""_complete.png";;
|
|
|
|
404) cp "404.png" "../cache/img/$startPath""$hashUrl""_thumb.png" && cp "404.png" "../cache/img/$startPath""$hashUrl.png" && cp "404.png" "../cache/img/$startPath""$hashUrl""_complete.png";;
|
2015-07-13 12:34:33 +02:00
|
|
|
manual) rm "../cache/tmp/$hashUrl.lock" && exit;;
|
2015-07-10 17:19:53 +02:00
|
|
|
*) cp "error.png" "../cache/img/$startPath""$hashUrl""_thumb.png" && cp "error.png" "../cache/img/$startPath""$hashUrl.png" && cp "error.png" "../cache/img/$startPath""$hashUrl""_complete.png";;
|
2013-03-19 17:07:13 +01:00
|
|
|
esac
|
2015-07-10 13:09:15 +02:00
|
|
|
rm "../cache/tmp/$hashUrl.lock"
|
2015-07-13 12:34:33 +02:00
|
|
|
if $log == true
|
2013-03-19 17:07:13 +01:00
|
|
|
then
|
|
|
|
end_time=`date +%s`
|
|
|
|
logDate=`date +'[%a %d %b %Y] [%H:%M:%S]'`
|
2015-07-13 14:27:22 +02:00
|
|
|
if [ $noErrorLog == "noErrorLog" ]
|
2015-07-13 12:34:33 +02:00
|
|
|
then
|
|
|
|
exit
|
|
|
|
fi
|
2013-05-31 12:28:30 +02:00
|
|
|
if [ $errorCode == 200 ]
|
|
|
|
then
|
2013-08-16 11:56:15 +02:00
|
|
|
echo "none --- "$site" --- "$hashUrl" --- "$thumbSize" --- "$sizeNameDir" --- "$onlyThumb" --- true --- "$errorCode > "../cache/logs/retry/"$hashUrl".log"
|
2013-05-31 12:28:30 +02:00
|
|
|
else
|
2013-08-16 11:56:15 +02:00
|
|
|
echo "none --- "$site" --- "$hashUrl" --- "$thumbSize" --- "$sizeNameDir" --- "$onlyThumb" --- true --- "$errorCode > "../cache/logs/other/"$hashUrl".log"
|
2013-05-31 12:28:30 +02:00
|
|
|
fi
|
2013-03-19 17:07:13 +01:00
|
|
|
fi
|
|
|
|
exit
|
|
|
|
fi
|
2015-07-09 17:21:32 +02:00
|
|
|
|
|
|
|
case $renderType in
|
|
|
|
# thumbnail only
|
|
|
|
t)
|
2016-12-12 15:27:29 +01:00
|
|
|
nice -n 5 convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 -filter Lanczos -thumbnail "$thumbSize" "../cache/tmp/$hashUrl""_thumb.png"
|
2013-03-26 15:22:13 +01:00
|
|
|
if [ $optimizeTool ]
|
|
|
|
then
|
2016-12-12 15:27:29 +01:00
|
|
|
eval nice -n 5 $optimizeTool "../cache/tmp/$hashUrl""_thumb.png" &>/dev/null
|
2013-03-26 15:22:13 +01:00
|
|
|
fi
|
2015-07-10 11:30:04 +02:00
|
|
|
mv "../cache/tmp/$hashUrl""_thumb.png" "../cache/img/$startPath""$hashUrl""_thumb.png"
|
2013-08-16 11:56:15 +02:00
|
|
|
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" ]]
|
2013-03-14 16:17:04 +01:00
|
|
|
then
|
|
|
|
echo 1
|
|
|
|
else
|
|
|
|
echo 0
|
|
|
|
fi
|
2015-07-10 11:30:04 +02:00
|
|
|
rm "../cache/tmp/$hashUrl.png"
|
2015-07-10 12:09:20 +02:00
|
|
|
rm "../cache/tmp/$hashUrl.lock"
|
2015-07-09 17:21:32 +02:00
|
|
|
;;
|
|
|
|
# thumbnail + complete
|
|
|
|
tc)
|
2016-12-12 15:27:29 +01:00
|
|
|
nice -n 5 convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 -filter Lanczos -thumbnail "$thumbSize" "../cache/tmp/$hashUrl""_thumb.png"
|
2015-07-09 17:21:32 +02:00
|
|
|
if [ $optimizeTool ]
|
|
|
|
then
|
2016-12-12 15:27:29 +01:00
|
|
|
nice -n 5 eval $optimizeTool "../cache/tmp/$hashUrl""_thumb.png" "../cache/tmp/$hashUrl.png" &>/dev/null
|
2015-07-09 17:21:32 +02:00
|
|
|
fi
|
|
|
|
mv "../cache/tmp/$hashUrl.png" "../cache/img/$startPath""$hashUrl""_complete.png"
|
2015-07-10 11:30:04 +02:00
|
|
|
mv "../cache/tmp/$hashUrl""_thumb.png" "../cache/img/$startPath""$hashUrl""_thumb.png"
|
2015-07-09 17:21:32 +02:00
|
|
|
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" && -f "../cache/img/$startPath""$hashUrl""_complete.png" ]]
|
2013-03-14 16:17:04 +01:00
|
|
|
then
|
|
|
|
echo 1
|
|
|
|
else
|
|
|
|
echo 0
|
|
|
|
fi
|
2015-07-10 12:09:20 +02:00
|
|
|
rm "../cache/tmp/$hashUrl.lock"
|
2015-07-09 17:21:32 +02:00
|
|
|
;;
|
|
|
|
# thumbnail + realsize
|
|
|
|
tr)
|
2016-12-12 15:27:29 +01:00
|
|
|
nice -n 5 convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 "../cache/tmp/$hashUrl.png"
|
|
|
|
nice -n 5 convert "../cache/tmp/$hashUrl.png" -filter Lanczos -thumbnail "$thumbSize" "../cache/tmp/$hashUrl""_thumb.png"
|
2015-07-09 17:21:32 +02:00
|
|
|
if [ $optimizeTool ]
|
|
|
|
then
|
2016-12-12 15:27:29 +01:00
|
|
|
eval nice -n 5 $optimizeTool "../cache/tmp/$hashUrl""_thumb.png" "../cache/tmp/$hashUrl.png" &>/dev/null
|
2015-07-09 17:21:32 +02:00
|
|
|
fi
|
2015-07-10 11:30:04 +02:00
|
|
|
mv "../cache/tmp/$hashUrl.png" "../cache/img/$startPath""$hashUrl.png"
|
|
|
|
mv "../cache/tmp/$hashUrl""_thumb.png" "../cache/img/$startPath""$hashUrl""_thumb.png"
|
2015-07-09 17:21:32 +02:00
|
|
|
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" && -f "../cache/img/$startPath""$hashUrl.png" ]]
|
|
|
|
then
|
|
|
|
echo 1
|
|
|
|
else
|
|
|
|
echo 0
|
|
|
|
fi
|
2015-07-10 12:09:20 +02:00
|
|
|
rm "../cache/tmp/$hashUrl.lock"
|
2015-07-09 17:21:32 +02:00
|
|
|
;;
|
|
|
|
# thumbnail + realsize + complete
|
|
|
|
trc)
|
2016-12-12 15:27:29 +01:00
|
|
|
cp "../cache/tmp/$hashUrl.png" "../cache/tmp/$hashUrl""_complete.png"
|
|
|
|
nice -n 5 convert "../cache/tmp/$hashUrl.png" -crop 1280x1024+0+0 "../cache/tmp/$hashUrl.png"
|
|
|
|
nice -n 5 convert "../cache/tmp/$hashUrl.png" -filter Lanczos -thumbnail "$thumbSize" "../cache/tmp/$hashUrl""_thumb.png"
|
2015-07-09 17:21:32 +02:00
|
|
|
if [ $optimizeTool ]
|
|
|
|
then
|
2016-12-12 15:27:29 +01:00
|
|
|
eval nice -n 5 $optimizeTool "../cache/tmp/$hashUrl""_thumb.png" "../cache/tmp/$hashUrl.png" "../cache/tmp/$hashUrl""_complete.png" &>/dev/null
|
2015-07-09 17:21:32 +02:00
|
|
|
fi
|
2015-07-10 11:30:04 +02:00
|
|
|
mv "../cache/tmp/$hashUrl.png" "../cache/img/$startPath""$hashUrl.png"
|
|
|
|
mv "../cache/tmp/$hashUrl""_thumb.png" "../cache/img/$startPath""$hashUrl""_thumb.png"
|
|
|
|
mv "../cache/tmp/$hashUrl""_complete.png" "../cache/img/$startPath""$hashUrl""_complete.png"
|
2015-07-09 17:21:32 +02:00
|
|
|
if [[ -f "../cache/img/$startPath""$hashUrl""_thumb.png" && -f "../cache/img/$startPath""$hashUrl.png" && -f "../cache/img/$startPath""$hashUrl""_complete.png" ]]
|
|
|
|
then
|
|
|
|
echo 1
|
|
|
|
else
|
|
|
|
echo 0
|
|
|
|
fi
|
2015-07-10 12:09:20 +02:00
|
|
|
rm "../cache/tmp/$hashUrl.lock"
|
2015-07-09 17:21:32 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2013-03-19 17:07:13 +01:00
|
|
|
if $log
|
|
|
|
then
|
|
|
|
end_time=`date +%s`
|
|
|
|
logDate=`date +'[%a %d %b %Y] [%H:%M:%S]'`
|
2013-08-16 11:56:15 +02:00
|
|
|
echo $logDate `expr $end_time - $start_time`s >> '../cache/logs/success.txt'
|
2013-03-19 17:07:13 +01:00
|
|
|
fi
|