Fixed redirect URL, added more mvn repos

This commit is contained in:
steckbrief 2017-01-02 15:32:12 +01:00
parent 0549800afe
commit 3ac6059824
3 changed files with 14 additions and 36 deletions

View file

@ -4,7 +4,15 @@
*/
return [
'mavenBaseUrls' => ['http://repo1.maven.org/maven2', ],
'mavenBaseUrls' => [
'https://repo1.maven.org/maven2',
'https://jcenter.bintray.com',
'https://jitpack.io/',
'https://repo.maven.apache.org/maven2/',
'https://oss.sonatype.org/content/repositories/snapshots/',
'https://oss.sonatype.org/content/repositories/releases/',
'https://plugins.gradle.org/m2' //Gradle plugin repo
],
'logfile' => 'logs/mvncache.log',
];
?>

View file

@ -1,34 +0,0 @@
<?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');
}
?>

View file

@ -12,6 +12,10 @@ $requestedArtifact = str_replace('/mvn', '', $requestedArtifact);
$baseLocalFolder = __DIR__.'/..';
$localFolder = $baseLocalFolder.substr($requestedArtifact, 0, strripos($requestedArtifact, '/'));
if ('' == $requestedArtifact) { // no artifact is requested - return
sendHttpReturnCodeAndMessage(403, 'Not allowed');
}
$tempFile = tmpfile();
foreach ($config['mavenBaseUrls'] as $mvnBaseUrl) {
$srcUrl = $mvnBaseUrl.$requestedArtifact;
@ -46,7 +50,7 @@ if (!$found) {
stream_copy_to_stream($tempFile, $dst);
if (is_file($dstPath)) {
chmod($dstPath, 0660);
header('Location: http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
header('Location: '.getServerProtocol().'://'.getRequestHostname().$_SERVER["REQUEST_URI"]);
} else {
header('HTTP/1.0 404 Not Found');
}