Skip to content

Commit

Permalink
Added an alternative fetching script written in PHP for those who can…
Browse files Browse the repository at this point in the history
…'t run perl.

Can be improved, should actually use a stream, but for now this works for smaller files.
  • Loading branch information
ErikDeBruijn committed Sep 24, 2014
1 parent b3de697 commit 40e0410
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions remote.php
@@ -0,0 +1,35 @@
<?php
// error_reporting(E_ALL);

define(CACHE_PATH, 'cache');
$maxSize = 10000000; // [bytes], max size of file
$maxTime = 60; // [s], max download time
$cacheExpireSeconds = 60;

// echo "Fetching ".$_REQUEST['url'];

retrieveFile($_REQUEST['url']);
cleanUpExpired();

function retrieveFile($url) {
$contents = file_get_contents($url);
$f = CACHE_PATH . "/tmp-".time()."-".rand(0,1024*1024)."-".basename($url);
file_put_contents("./".$f, $contents);
$data = array(
'filename' => basename($url),
'file' => CACHE_PATH."/".basename($f),
'url' => $url
);

echo json_encode ( $data );
}

function cleanUpExpired() {
// Remove cached files that expired
$files = glob("".CACHE_PATH."/*");
foreach($files as $file)
{
if(time() - filemtime($file) > $cacheExpireSeconds)
@unlink($file);
}
}

0 comments on commit 40e0410

Please sign in to comment.