[add] #453 : Remove image older than

[add] #466 : Another dir organisation
This commit is contained in:
Knah Tsaeb 2013-05-03 11:01:17 +02:00
parent cb76512a08
commit c665a3cdec
3 changed files with 94 additions and 44 deletions

View file

@ -6,10 +6,11 @@
site=$1 # url for thumbshot site=$1 # url for thumbshot
hashUrl=$2 # hash(url) is the name of final image hashUrl=$2 # hash(url) is the name of final image
thumbSize=$3 # size of thumb widthxheight ex : 190x90 thumbSize=$3 # size of thumb widthxheight ex : 190x90
onlyThumb=$4 # make only thumbshot no full size image sizeNameDir=$4 # name of size dir
waitForResult=$5 # if true we try to make soon as possible or add to queue 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 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 timeOut=60 # default time out, after this time the site are declared in error
log=false # log all generation success and error log=false # log all generation success and error
randomSleep=`echo $((RANDOM%20))` randomSleep=`echo $((RANDOM%20))`
optimizeTool='' # external tool form optimize png optimizeTool='' # external tool form optimize png
@ -17,7 +18,8 @@ maxQueue=50 # max process in queue
currentProcess=$(ps -e | grep -v grep | grep thumb_ | wc -l) currentProcess=$(ps -e | grep -v grep | grep thumb_ | wc -l)
firstLevel=${hashUrl:0:2} firstLevel=${hashUrl:0:2}
secondLevel=${hashUrl:2:2} secondLevel=${hashUrl:2:2}
startPath=$firstLevel/$secondLevel/ startPath=$sizeNameDir/$firstLevel/$secondLevel/
mkdir -p "cache/img/$startPath" mkdir -p "cache/img/$startPath"
if [ -f "cache/config/serverOptions.php" ] if [ -f "cache/config/serverOptions.php" ]

View file

@ -6,7 +6,7 @@ html {
background-color: #dedede; background-color: #dedede;
} }
form, #result{ form, #result, #header{
padding: 1em; padding: 1em;
text-align: center; text-align: center;
width: 50%; width: 50%;
@ -43,7 +43,7 @@ form .info {
font-size : 0.8em; font-size : 0.8em;
} }
#result { #result, #header {
text-align: center; text-align: center;
padding: 1em; padding: 1em;
margin: 1em auto; margin: 1em auto;

View file

@ -2,24 +2,32 @@
if (empty($_SESSION)) { if (empty($_SESSION)) {
session_start(); session_start();
} }
date_default_timezone_set('Europe/Paris');
// change if you want no inpact // change if you want no inpact
$GLOBALS['config']['onlyLocalServer'] = false; date_default_timezone_set('Europe/Paris');
// soshot only accept request by 127.0.0.1 // soshot only accept request by 127.0.0.1
$GLOBALS['config']['NoWebPage'] = false; $GLOBALS['config']['onlyLocalServer'] = false;
// No form for post url only acces by GET method // No form for post url only acces by GET method
$GLOBALS['config']['maxErrorBeforeBan'] = 3; $GLOBALS['config']['NoWebPage'] = false;
// After 3 criticla error ban user // After 3 criticla error ban user
$GLOBALS['config']['banTime'] = 60; $GLOBALS['config']['maxErrorBeforeBan'] = 3;
// Ban for 60 minutes // Ban for 60 minutes
$GLOBALS['config']['defaultUrl'] = 'https://google.com'; $GLOBALS['config']['banTime'] = 60;
// default url for form // default url for form
$GLOBALS['config']['defaultThumbSize'] = '120x90'; $GLOBALS['config']['defaultUrl'] = 'https://google.com';
// default size for thumbnail // default size for thumbnail
$GLOBALS['config']['onlyThumb'] = TRUE; $GLOBALS['config']['defaultThumbSize'] = '120x90';
// generate only thumbnail or generate thumbnail + 1280x1024 image // generate only thumbnail or generate thumbnail + 1280x1024 image
$GLOBALS['config']['thumbSize'] = array('s' => '120x90', 'm' => '200x160', 'l' => '300x240', 'xl' => '400x320', 'xxl' => '500x400'); $GLOBALS['config']['onlyThumb'] = TRUE;
// list of available size for thumb // list of available size for thumb
$GLOBALS['config']['thumbSize'] = array(
's' => '120x90',
'm' => '200x160',
'l' => '300x240',
'xl' => '400x320',
'xxl' => '500x400'
);
// Remove image older than 12 hours
$GLOBALS['config']['expireCache'] = 12;
if (!file_exists('cache/config/genConf.php')) { if (!file_exists('cache/config/genConf.php')) {
install(); install();
@ -81,7 +89,7 @@ function testExistImg($file) {
* @param $waiForResult (bool) * @param $waiForResult (bool)
* @return * @return
*/ */
function launchScript($url, $hashUrl, $width, $onlyThumb, $waitForResult = false) { function launchScript($url, $hashUrl, $width, $size, $onlyThumb, $waitForResult = false) {
$hashUrl = escapeshellarg($hashUrl); $hashUrl = escapeshellarg($hashUrl);
$url = escapeshellarg($url); $url = escapeshellarg($url);
$width = escapeshellarg($width); $width = escapeshellarg($width);
@ -92,17 +100,21 @@ function launchScript($url, $hashUrl, $width, $onlyThumb, $waitForResult = false
} }
if ($waitForResult === false) { if ($waitForResult === false) {
exec('bin/thumb_server.sh ' . $url . ' ' . $hashUrl . ' ' . $width . ' ' . $onlyThumb . ' > /dev/null &', $result); exec('bin/thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$onlyThumb.' > /dev/null &', $result);
} else { } else {
exec('bin/thumb_server.sh ' . $url . ' ' . $hashUrl . ' ' . $width . ' ' . $onlyThumb . ' 1', $result); exec('bin/thumb_server.sh '.$url.' '.$hashUrl.' '.$width.' '.$size.' '.$onlyThumb.' 1', $result);
} }
return $result; return $result;
} }
function testValidUrl($url) { function testValidUrl($url) {
$url = trim($url);
if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED)) { if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED)) {
$url = parse_url($url); $url = parse_url($url);
if (!in_array($url['scheme'], array('http', 'https'))) { if (!in_array($url['scheme'], array(
'http',
'https'
))) {
return array('msg' => 'Url must be start by http or https.'); return array('msg' => 'Url must be start by http or https.');
} }
return true; return true;
@ -192,7 +204,13 @@ function install() {
if (!is_file('cache/logs/log.txt')) { if (!is_file('cache/logs/log.txt')) {
touch('cache/logs/log.txt'); touch('cache/logs/log.txt');
} }
echo '<div>This is the key for generate thumbnail whith GET method. Save it, this key is secret, don\'t share it. <p class="alert">', $serverKey, '</p></div>'; echo '
<div>
This is the key for generate thumbnail whith GET method. Save it, this key is secret, don\'t share it.
<p class="alert">
', $serverKey, '
</p>
</div>';
checkInstall(); checkInstall();
} }
@ -211,7 +229,26 @@ function checkInstall() {
} }
} }
function removeOlderThan($dir = 'cache/img/shortLived') {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object !== '.' && $object !== '..' && $object) {
if (filetype($dir.'/'.$object) === 'dir') {
removeOlderThan($dir.'/'.$object);
} else {
if(fileatime($dir.'/'.$object) < time() - 3600*$GLOBALS['config']['expireCache']){
unlink($dir.'/'.$object);
}
}
}
}
reset($objects);
}
}
checkInstall(); checkInstall();
removeOlderThan();
if ($_GET) { if ($_GET) {
$ui = $_GET; $ui = $_GET;
unset($_GET); unset($_GET);
@ -222,6 +259,9 @@ if ($_GET) {
ban(); ban();
die('I take a chips and give it to Godzilla. I print a shoes and .............. KAMOULOX ! Well done Jean Pierre.'); die('I take a chips and give it to Godzilla. I print a shoes and .............. KAMOULOX ! Well done Jean Pierre.');
} }
if (!array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) {
die('Die another day !');
}
$ui['url'] = rawurldecode($ui['url']); $ui['url'] = rawurldecode($ui['url']);
$testUrl = testValidUrl($ui['url']); $testUrl = testValidUrl($ui['url']);
@ -241,13 +281,13 @@ if ($_GET) {
$hashUrl = sha1($GLOBALS['config']['serverKey'].$defUrl); $hashUrl = sha1($GLOBALS['config']['serverKey'].$defUrl);
$startPath = substr($hashUrl, 0, 2).'/'.substr($hashUrl, 2, 2).'/'; $startPath = substr($hashUrl, 0, 2).'/'.substr($hashUrl, 2, 2).'/';
$file = 'cache/img/' . $startPath . $hashUrl; $file = 'cache/img/'.$ui['s'].'/'.$startPath.$hashUrl;
if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) { if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) {
if (isset($ui['iw']) && (int)$ui['iw'] === 1) { if (isset($ui['iw']) && (int)$ui['iw'] === 1) {
launchScript($defUrl, $hashUrl, $width, $GLOBALS['config']['onlyThumb'], true); launchScript($defUrl, $hashUrl, $width, $ui['s'], $GLOBALS['config']['onlyThumb'], true);
} else { } else {
$res = launchScript($defUrl, $hashUrl, $width, $GLOBALS['config']['onlyThumb'], false); $res = launchScript($defUrl, $hashUrl, $width, $ui['s'], $GLOBALS['config']['onlyThumb'], false);
header("Content-type: image/png"); header("Content-type: image/png");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo file_get_contents('bin/loadingGen.png'); echo file_get_contents('bin/loadingGen.png');
@ -280,6 +320,10 @@ if ($_POST) {
exit(); exit();
} }
if (!array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) {
die('Die another day !');
}
$defUrl = $ui['url']; $defUrl = $ui['url'];
if (isset($ui['s']) && array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) { if (isset($ui['s']) && array_key_exists($ui['s'], $GLOBALS['config']['thumbSize'])) {
@ -290,12 +334,15 @@ if ($_POST) {
$hashUrl = sha1($GLOBALS['config']['serverKey'].$defUrl); $hashUrl = sha1($GLOBALS['config']['serverKey'].$defUrl);
$startPath = substr($hashUrl, 0, 2).'/'.substr($hashUrl, 2, 2).'/'; $startPath = substr($hashUrl, 0, 2).'/'.substr($hashUrl, 2, 2).'/';
$file = 'cache/img/' . $startPath . $hashUrl; $file = 'cache/img/shortLived/'.$startPath.$hashUrl;
if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) { if (testExistImg($file) !== true || isset($ui['fr']) && (int)$ui['fr'] === 1) {
launchScript($defUrl, $hashUrl, $width, $GLOBALS['config']['onlyThumb'], true); launchScript($defUrl, $hashUrl, $width, 'shortLived', $GLOBALS['config']['onlyThumb'], true);
} }
$success = array('normal' => $file . '.png', 'thumb' => $file . '_thumb.png'); $success = array(
'normal' => $file.'.png',
'thumb' => $file.'_thumb.png'
);
} }
if (empty($defUrl)) { if (empty($defUrl)) {
@ -347,6 +394,7 @@ if (empty($width)) {
<?php <?php
if (!empty($success)) { if (!empty($success)) {
echo '<div id="result">'; echo '<div id="result">';
echo '<h3>This image will removed in 24h</h3>';
echo '<img src="', $success['thumb'], '?r=', time(), '"/>'; echo '<img src="', $success['thumb'], '?r=', time(), '"/>';
echo '<p><label>', $width, '</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['thumb'], '" onclick="this.select()"/></p>'; echo '<p><label>', $width, '</label><input type="text" value="http://', $_SERVER['SERVER_NAME'], '/', $success['thumb'], '" onclick="this.select()"/></p>';
if ($GLOBALS['config']['onlyThumb'] === false) { if ($GLOBALS['config']['onlyThumb'] === false) {