aboutsummaryrefslogtreecommitdiffstats
path: root/src/loadArtifact.php
blob: 6205164289328104f35ddd0f89b809373d79cac8 (plain)
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
<?php
$mvnBaseUrl = 'http://repo1.maven.org/maven2';

$requestedArtifact = $_SERVER["REQUEST_URI"];
$requestedArtifact = str_replace('/mvn', '', $requestedArtifact);
$baseLocalFolder = __DIR__;
$localFolder = $baseLocalFolder.substr($requestedArtifact, 0, strripos($requestedArtifact, '/'));
//echo $localFolder."<br>";
if (!is_dir($localFolder)) {
  mkdir($localFolder, 0770, true);
}

$srcUrl = $mvnBaseUrl.$requestedArtifact;
//echo $srcUrl."<br>";
$src = fopen($srcUrl, 'r');

$dstPath = $baseLocalFolder.$requestedArtifact;
//echo $dstPath;
$dst = fopen($dstPath, 'w');

$filesize = stream_copy_to_stream($src, $dst);

if (0 == $filesize) {
  unlink($dstPath);
  unlink($localFolder);
}

if (is_file($dstPath)) {
  chmod($dstPath, 0660);
  header('Location: http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
} else {
  header('HTTP/1.0 404 Not Found');
}
?>