forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremote.php
35 lines (29 loc) · 874 Bytes
/
remote.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
}
}