Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download artifact repositories #670

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Controller/RepoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public function packages(Request $request, Organization $organization): JsonResp
).'dists/%package%/%version%/%reference%.%type%',
'preferred' => true,
],
[
'dist-url' => $this->generateUrl(
'organization_repo_url',
['organization' => $organization->alias()],
RouterInterface::ABSOLUTE_URL
).'dists/%package%/%version%/%type%',
'preferred' => false,
],
],
]))
->setPrivate()
Expand Down Expand Up @@ -102,6 +110,35 @@ public function distribution(Organization $organization, string $package, string
});
}

/**
* @Route("/dists/{package}/{version}/{type}",
* name="repo_artifact_package_dist",
* host="{organization}{sep1}repo{sep2}{domain}",
* defaults={"domain":"%domain%","sep1"="%organization_separator%","sep2"="%domain_separator%"},
* requirements={"package"="%package_name_pattern%","type"="zip|tar","domain"="%domain%","sep1"="%organization_separator%","sep2"="%domain_separator%"},
* methods={"GET"})
* @Cache(public=false)
*/
public function artifactDistribution(Organization $organization, string $package, string $version, string $type): StreamedResponse
{
$filename = $this->packageManager
->distFilename($organization->alias(), $package, $version, '', $type)
->getOrElseThrow(new NotFoundHttpException('This distribution file can not be found or downloaded from origin url.'));

return new StreamedResponse(function () use ($filename): void {
$outputStream = \fopen('php://output', 'wb');
if (false === $outputStream) {
throw new HttpException(500, 'Could not open output stream to send binary file.'); // @codeCoverageIgnore
}
$fileStream = $this->packageManager->getDistFileReference($filename);
\stream_copy_to_stream(
$fileStream
->getOrElseThrow(new NotFoundHttpException('This distribution file can not be found or downloaded from origin url.')),
$outputStream
);
});
}

/**
* @Route("/downloads",
* name="repo_package_downloads",
Expand Down
16 changes: 11 additions & 5 deletions src/Service/Dist/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ public function remove(Dist $dist): void

public function filename(Dist $dist): string
{
return \sprintf(
'%s/dist/%s/%s_%s.%s',
$filename = \sprintf(
'%s/dist/%s/%s',
$dist->repo(),
$dist->package(),
$dist->version(),
$dist->ref(),
$dist->format()
$dist->version()
);

if ($dist->ref() !== '') {
$filename .= '_'.$dist->ref();
}

$filename .= '.'.$dist->format();

return $filename;
}

public function size(Dist $dist): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function synchronize(Package $package): void
'packageName' => $p->getPrettyName(),
'prettyVersion' => $p->getPrettyVersion(),
'version' => $p->getVersion(),
'ref' => $p->getDistReference() ?? $p->getDistSha1Checksum(),
'ref' => $p->getDistReference() ?? '',
'distType' => $p->getDistType(),
'distUrl' => $p->getDistUrl(),
'authHeaders' => $this->getAuthHeaders($package),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function findDistribution(Package $package): string
??
$this->versionParser->normalize($packageData['version']);
$packageDist = $packageData['dist'];
$reference = $packageDist['reference'] ?? $packageDist['shasum'];
$reference = $packageDist['reference'] ?? '';

if ($packageVersion === $normalizedVersion && isset($packageDist['url'], $reference)) {
$archiveType = $packageDist['type'];
Expand Down
35 changes: 35 additions & 0 deletions tests/Functional/Controller/RepoControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public function testOrganizationPackagesAction(): void
{
"dist-url": "http://buddy.repo.repman.wip/dists/%package%/%version%/%reference%.%type%",
"preferred": true
},
{
"dist-url": "http://buddy.repo.repman.wip/dists/%package%/%version%/%type%",
"preferred": false
}
]
}
Expand Down Expand Up @@ -146,6 +150,37 @@ public function testOrganizationPackageDistDownload(): void
self::assertTrue($this->client->getResponse()->isNotFound());
}

public function testOrganizationArtifactPackageDistDownload(): void
{
$this->fixtures->prepareRepoFiles();
$this->fixtures->createToken(
$this->fixtures->createOrganization('buddy', $this->fixtures->createUser()),
'secret-org-token'
);

$this->contentFromStream(function (): void {
$this->client->request('GET', '/dists/buddy-works/artifact/1.0.0.0/zip', [], [], [
'HTTP_HOST' => 'buddy.repo.repman.wip',
'PHP_AUTH_USER' => 'token',
'PHP_AUTH_PW' => 'secret-org-token',
]);
});

$response = $this->client->getResponse();
self::assertTrue($response->isOk(), 'Response code was not 200, it was instead '.$response->getStatusCode());
self::assertInstanceOf(StreamedResponse::class, $response);

$this->contentFromStream(function (): void {
$this->client->request('GET', '/dists/vendor/artifact/1.0.0.0/zip', [], [], [
'HTTP_HOST' => 'buddy.repo.repman.wip',
'PHP_AUTH_USER' => 'token',
'PHP_AUTH_PW' => 'secret-org-token',
]);
});

self::assertTrue($this->client->getResponse()->isNotFound());
}

public function testOrganizationTrackDownloads(): void
{
$this->fixtures->createPackage('c75b535f-5817-41a2-9424-e05476e7958f', 'buddy');
Expand Down
Binary file not shown.
38 changes: 38 additions & 0 deletions tests/Unit/Service/Dist/StorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Buddy\Repman\Tests\Unit\Service\Dist;

use Buddy\Repman\Service\Dist;
use Buddy\Repman\Service\Dist\Storage;
use Buddy\Repman\Tests\Doubles\FakeDownloader;
use League\Flysystem\Filesystem;
use League\Flysystem\Memory\MemoryAdapter;
use PHPUnit\Framework\TestCase;

final class StorageTest extends TestCase
{
private Storage $storage;

public function setUp(): void
{
$this->storage = new Storage(
new FakeDownloader(),
new Filesystem(new MemoryAdapter())
);
}

public function testFilename(): void
{
$dist = new Dist('repo', 'package', '1.1.0', '123456', 'zip');

self::assertEquals('repo/dist/package/1.1.0_123456.zip', $this->storage->filename($dist));
}

public function testArtifactFilename(): void
{
$dist = new Dist('repo', 'package', '1.1.0', '', 'zip');
self::assertEquals('repo/dist/package/1.1.0.zip', $this->storage->filename($dist));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public function testSynchronizePackageFromArtifacts(): void
self::assertContains('replaces-buddy-works/replace-^1.0', $linkStrings);
self::assertContains('conflicts-buddy-works/conflict-^1.0', $linkStrings);
self::assertContains('suggests-buddy-works/suggests-You really should', $linkStrings);

$referencestrings = array_map(function (Version $version): string {
return $version->reference();
}, $package->versions()->toArray());
self::assertEquals(['', '', '', ''], $referencestrings);
}

public function testWithMostRecentUnstable(): void
Expand Down