Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an alternative fetching script written in PHP for those who can…
…'t run perl. Can be improved, should actually use a stream, but for now this works for smaller files.
- Loading branch information
1 parent
b3de697
commit 40e0410
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |