If you wondered an API that requires polling isn’t a very good thing for scalability. On my current setup I can pretty easily handle about 20 status requests per second on top of my normal traffic, the problem is its not hard for a bad polling implmentation being run by one user to make that many requests.
To solve this problem im adding an addition to the Webthumb API that will allow you to skip polling all together. The basic idea is that your make an API request and when your thumbnail is complete i’ll make a GET request back too your server telling you that the request is complete.
So on the request side its just a matter of including an notify tag in your request block. An example is shown below:
<webthumb>
<apikey>apikeyhere</apikey>
<request>
<url>webthumb.bluga.net</url>
<notify>http://webthumb.bluga.net/sample/notify.php?secret=blahblahblah</notify>
</request>
</webthumb>
I’m including a secret variable in the URL so that if someone found the URL of my notify script they couldn’t DOS me by making me download 100′s of different thumbnails from the server.
I’ve written a basic notify script to get you started. Feel free to use this script as a basis for whatever you need.
Update: Paths to thumbnails have changed so the download code listed here wont’ work. The new directory hash is:
<?php
substr($id,-2).'/'.substr($id,-4,-2).'/'.substr($id,-6,-4)
?>
Which means if your job id is: wt4761c8f914559 then the directory is: http://webthumb.bluga.net/data/59/45/91/
<?php
// this is a really simple notify script
// it downloads the specified thumbs for a job and puts them in the storage dir
// if you want to store files based urls etc you'll need to store the id at request time
// then do a mapping in this script
// download options
// zip - all sizes in a zip
// zipAuto - zip download and auto uncompress
// large - 640x480
// medium2 - 320x240
// medium - 160x120
// small - 80x60
$downloadType = 'zipAuto';
// secret id im using to make sure no one has me download every thumb etc
$mysecret = 'changeme';
// directory to write files too
$storageDir = 'tmp';
// webthumb base url
$url = 'http://webthumb.bluga.net/data/';
// unzip command
$unzipCommand = 'unzip';
// END CONFIG
if (!isset($_GET['id']) || !isset($_GET['secret'])) {
exit;
}
if ($mysecret == 'changeme') {
echo "Configure notify script";
exit;
}
$jobId = $_GET['id'];
$secret = $_GET['secret'];
if ($secret !== $mysecret) {
echo "bad secret";
exit;
}
$jobDir = substr($jobId,-4);
switch($downloadType) {
case 'zip':
case 'zipAuto':
$file = "$jobId.zip";
break;
default:
$file = "$jobId-thumb_$downloadType.jpg";
break;
}
// this is the simplest possible download code, curl, PEAR http_request might be better
// will only work if allow_url_fopen is on
$contents = file_get_contents($url.$jobDir."/$file");
file_put_contents($storageDir."/$file",$contents);
if ($downloadType == 'zipAuto') {
exec("cd $storageDir && $unzipCommand $file");
unlink($storageDir."/$file");
}
?>
Let me know if you find any major bugs in the code. There are always going to be cases where the polling API makes more sense (command line utils etc) but I think this notify API should work great for any application integration.