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

Add architecture endpoints #20

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.2, 7.3, 7.4, 8.0]
php: [7.4, 8.0]
experimental: [false]
include:
- php: 7.4
Expand Down
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@
]
},
"require": {
"php": ">=7.2",
"josegonzalez/dotenv": "^3.2",
"slim/slim": "^3.0",
"php": ">=7.4",
"josegonzalez/dotenv": "~4.0",
"slim/slim": "~3.0",
"validator/livr": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"mikey179/vfsstream": "^1.6",
"helmich/phpunit-psr7-assert": "^4.1",
"syberisle/mock-php-stream": "^1.1"
"phpunit/phpunit": "~9.6",
"mikey179/vfsstream": "~1.6",
"helmich/phpunit-psr7-assert": "~4.1",
"syberisle/mock-php-stream": "~1.1",
"phpstan/phpstan": "^1.12"
},
"scripts" : {
"test": "vendor/bin/phpunit",
"stan": "vendor/bin/phpstan analyze src",
"test-coverage": "vendor/bin/phpunit --coverage-clover build/logs/clover.xml"
}
}
35 changes: 16 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Phagrancy">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
failOnWarning="true">
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="8191"/>
</php>
<testsuites>
<testsuite name="Phagrancy">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
88 changes: 45 additions & 43 deletions src/Action/Api/Scope/Box/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Phagrancy\Action\Api\Scope\Box;

use Phagrancy\Concern\FindsBox;
use Phagrancy\Http\Response;
use Phagrancy\Model\Input;
use Phagrancy\Model\Repository;
Expand All @@ -19,54 +20,55 @@
*/
class Delete
{
/**
* @var Repository\Box
*/
private $boxes;
use FindsBox;

/**
* @var string
*/
private $storagePath;
/**
* @var Repository\Box
*/
private $boxes;

/**
* @var Input\BoxDelete
*/
private $input;
/**
* @var string
*/
private $storagePath;

public function __construct(Repository\Box $boxes, Input\BoxDelete $input, $storagePath)
{
$this->boxes = $boxes;
$this->input = $input;
$this->storagePath = $storagePath;
}
/**
* @var Input\BoxDelete
*/
private $input;

public function __invoke(ServerRequestInterface $request)
{
/**
* The route controls these params, and they are validated so safe
*
* @var string $name
* @var string $scope
* @var string $version
* @var string $provider
*/
$params = $this->input->validate($request->getAttribute('route')->getArguments());
if (!$params) {
return new Response\NotFound();
}
public function __construct(Repository\Box $boxes, Input\BoxDelete $input, $storagePath)
{
$this->boxes = $boxes;
$this->input = $input;
$this->storagePath = $storagePath;
}

extract($params);
$box = $this->boxes->ofNameInScope($name, $scope);
public function __invoke(ServerRequestInterface $request)
{
/**
* The route controls these params, and they are validated so safe
*
* @var string $name
* @var string $scope
* @var string $version
* @var string $provider
*/
$params = $this->input->validate($request->getAttribute('route')->getArguments());
if (!$params) {
return new Response\NotFound();
}

if ($box) {
$path = "{$this->storagePath}/{$box->path()}/{$version}/{$provider}.box";
$boxPath = $this->findBox($params, $this->storagePath);
if ($boxPath) {
if (is_writable($boxPath) && unlink($boxPath)) {
return new Response\AllClear();
}
else {
return new Response\Json(['errors' => 'unable to delete'], 409);
}
}

if (file_exists($path) && unlink($path)) {
return new Response\Json([]);
}
}

return new Response\NotFound();
}
return new Response\NotFound();
}
}
3 changes: 3 additions & 0 deletions src/Action/Api/Scope/Box/ReturnsUrlForBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ protected function createUrlFromRouteParams($params)
isset($params['version']) && $url[] = "version/{$params['version']}";
isset($params['provider']) && $url[] = "provider/{$params['provider']}";

// should we validate the architecture is approved? i386,amd64,aarch64
isset($params['architecture']) && $url[] = $params['architecture'];

return '/api/v1/box/' . join('/', $url);
}
}
83 changes: 22 additions & 61 deletions src/Action/Api/Scope/Box/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace Phagrancy\Action\Api\Scope\Box;

use Phagrancy\Http\Response;
use Phagrancy\Model\Input;
use Phagrancy\Model\Repository;
use Phagrancy\Model\Entity\Box;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
Expand All @@ -18,75 +18,36 @@
* @package Phagrancy\Action\Api\Scope\Box
*/
class Upload
extends UploadAction
{
/**
* @var Repository\Box
*/
private $boxes;

/**
* @var string
*/
private $uploadPath;

/**
* @var Input\BoxUpload
*/
private $input;

public function __construct(Repository\Box $boxes, Input\BoxUpload $input, $uploadPath)
{
$this->boxes = $boxes;
$this->input = $input;
$this->uploadPath = $uploadPath;
}

public function __invoke(ServerRequestInterface $request)
public function perform(ServerRequestInterface $request, Box $box, $params): ResponseInterface
{
/**
* The route controls these params, and they are validated so safe
*
* @var string $name
* @var string $scope
* @var string $version
* @var string $provider
*/
$params = $this->input->validate($request->getAttribute('route')->getArguments());
if (!$params) {
return new Response\NotFound();
}

extract($params);
$box = $this->boxes->ofNameInScope($name, $scope);
$path = "{$this->uploadPath}/{$box->path()}/{$version}/";

// If box with same version and provider already exists prevent overwriting
if (file_exists("$path/{$provider}.box")) {
return new Response\NotFound();
if (!file_exists("{$this->uploadPath}/tmp")) {
mkdir("{$this->uploadPath}/tmp", 0755, true);
}
$tmp = tempnam("{$this->uploadPath}/tmp", 'phagrancy');

if ($box) {
if (!file_exists("{$this->uploadPath}/tmp")) {
mkdir("{$this->uploadPath}/tmp", 0755, true);
}
$tmp = tempnam("{$this->uploadPath}/tmp", 'phagrancy');
$request->getBody()->detach();
$from = fopen("php://input", 'r');
$to = fopen($tmp, 'w');

$request->getBody()->detach();
$from = fopen("php://input", 'r');
$to = fopen($tmp, 'w');
stream_copy_to_stream($from, $to);
fclose($from);
fclose($to);

stream_copy_to_stream($from, $to);
fclose($from);
fclose($to);
// make sure it exists
if (!file_exists($path = "{$this->uploadPath}/{$box->path()}/{$version}/")) {
mkdir($path, 0755, true);
}

// make sure it exists
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
// the box name is now {provider}-{architecture}.box, if there is no architecture, then we don't worry
$architecture = $architecture ?? 'unknown';
$boxPath = "$path/{$provider}-{$architecture}.box";

rename($tmp, "$path/{$provider}.box");
}
rename($tmp, $boxPath);

return new Response\Json([]);
return new Response\AllClear();
}
}
Loading
Loading