Skip to content

Commit

Permalink
fix api platform after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Jun 10, 2024
1 parent 0b5afcb commit 5892102
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 35 deletions.
2 changes: 1 addition & 1 deletion databox/api/src/Entity/Core/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
processor: AssetAttributeBatchUpdateProcessor::class,
),
new GetCollection(),
new Post(securityPostValidation: 'is_granted("CREATE", object)'),
new Post(securityPostDenormalize: 'is_granted("CREATE", object)'),
new Post(
uriTemplate: '/assets/multiple',
controller: MultipleAssetCreateAction::class,
Expand Down
2 changes: 1 addition & 1 deletion databox/api/src/Entity/Core/RenditionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
new Put(security: 'is_granted("EDIT", object)'),
new Patch(security: 'is_granted("EDIT", object)'),
new GetCollection(),
new Post(securityPostValidation: 'is_granted("CREATE", object)'),
new Post(securityPostDenormalize: 'is_granted("CREATE", object)'),
],
normalizationContext: [
'groups' => [RenditionClass::GROUP_LIST],
Expand Down
87 changes: 65 additions & 22 deletions databox/api/src/Integration/Phrasea/Expose/ExposeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use App\Asset\FileFetcher;
use App\Elasticsearch\Mapping\IndexMappingUpdater;
use App\Entity\Core\Asset;
use App\Entity\Core\AssetRendition;
use App\Entity\Core\Attribute;
use App\Entity\Integration\IntegrationToken;
use App\Integration\IntegrationConfig;
use App\Integration\Phrasea\PhraseaClientFactory;
use App\Storage\RenditionManager;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final readonly class ExposeClient
Expand All @@ -21,6 +23,7 @@ public function __construct(
private FileFetcher $fileFetcher,
private AssetTitleResolver $assetTitleResolver,
private AttributesResolver $attributesResolver,
private RenditionManager $renditionManager,
) {
}

Expand Down Expand Up @@ -120,37 +123,77 @@ public function postAsset(IntegrationConfig $config, IntegrationToken $integrati
}

$source = $asset->getSource();
$data = array_merge([
'publication_id' => $publicationId,
'asset_id' => $asset->getId(),
'title' => $resolvedTitle,
'description' => $description,
'translations' => $translations,
'upload' => [
'type' => $source->getType(),
'size' => $source->getSize(),
'name' => $source->getOriginalName(),
],
], $extraData);

$pubAsset = $this->create($config, $integrationToken)
->request('POST', '/assets', [
'json' => $data,
])
->toArray()
;

$uploadUrl = $pubAsset['uploadURL'];

$fetchedFilePath = $this->fileFetcher->getFile($source);
try {
$this->uploadClient->request('PUT', $uploadUrl, [
$data = array_merge([
'publication_id' => $publicationId,
'asset_id' => $asset->getId(),
'title' => $resolvedTitle,
'description' => $description,
'translations' => $translations,
'upload' => [
'type' => $source->getType(),
'size' => $source->getSize(),
'name' => $source->getOriginalName(),
],
], $extraData);

$pubAsset = $this->create($config, $integrationToken)
->request('POST', '/assets', [
'json' => $data,
])
->toArray()
;
$exposeAssetId = $pubAsset['id'];

$this->uploadClient->request('PUT', $pubAsset['uploadURL'], [
'headers' => [
'Content-Type' => $source->getType(),
'Content-Length' => filesize($fetchedFilePath),
],
'body' => fopen($fetchedFilePath, 'r'),
]);

foreach ([
'preview',
'thumbnail',
] as $renditionName) {
if (null !== $rendition = $this->renditionManager->getAssetRenditionUsedAs($renditionName, $asset->getId())) {
$file = $rendition->getFile();
$subDefFetchedFile = $this->fileFetcher->getFile($file);
try {
$subDefResponse = $this->create($config, $integrationToken)
->request('POST', '/sub-definitions', [
'json' => [
'asset_id' => $exposeAssetId,
'name' => $renditionName,
'use_as_preview' => 'preview' === $renditionName,
'use_as_thumbnail' => 'thumbnail' === $renditionName,
'use_as_poster' => 'poster' === $renditionName,
'upload' => [
'type' => $file->getType(),
'size' => $file->getSize(),
'name' => $file->getOriginalName(),

]
],
])
->toArray()
;

$this->uploadClient->request('PUT', $subDefResponse['uploadURL'], [
'headers' => [
'Content-Type' => $file->getType(),
'Content-Length' => filesize($subDefFetchedFile),
],
'body' => fopen($subDefFetchedFile, 'r'),
]);
} finally {
@unlink($subDefFetchedFile);
}
}
}
} finally {
@unlink($fetchedFilePath);
}
Expand Down
17 changes: 17 additions & 0 deletions databox/api/src/Storage/RenditionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public function getAssetRenditionByName(string $assetId, string $renditionName):
->getOneOrNullResult();
}

public function getAssetRenditionUsedAs(string $as, string $assetId): ?AssetRendition
{
return $this->em
->createQueryBuilder()
->select('r')
->from(AssetRendition::class, 'r')
->innerJoin('r.definition', 'd')
->andWhere('r.asset = :asset')
->andWhere(sprintf("d.useAs%s = :as", ucfirst($as)))
->setParameters([
'asset' => $assetId,
'as' => true,
])
->getQuery()
->getOneOrNullResult();
}

public function getRenditionDefinitionByName(Workspace $workspace, string $name): RenditionDefinition
{
$definition = $this
Expand Down
12 changes: 2 additions & 10 deletions databox/api/tests/Api/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testCreateAsset(): void
$this->assertMatchesResourceItemJsonSchema(Asset::class);
}

public function testCreateInvalidAsset(): void
public function testCreateAssetIsForbiddenWithoutWorkspace(): void
{
static::createClient()->request('POST', '/assets', [
'headers' => [
Expand All @@ -83,15 +83,7 @@ public function testCreateInvalidAsset(): void
],
]);

$this->assertResponseStatusCodeSame(422);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');

$this->assertJsonContains([
'@context' => '/contexts/ConstraintViolationList',
'@type' => 'ConstraintViolationList',
'hydra:title' => 'An error occurred',
'hydra:description' => 'workspace: This value should not be null.',
]);
$this->assertResponseStatusCodeSame(403);
}

public function testUpdateAsset(): void
Expand Down
2 changes: 1 addition & 1 deletion databox/api/tests/Api/CrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getCases(): array

['POST', '/rendition-classes', KeycloakClientTestMock::USER_UID, [
], [
'code' => 422,
'code' => 403,
]],

['POST', '/rendition-classes', KeycloakClientTestMock::ADMIN_UID, [
Expand Down

0 comments on commit 5892102

Please sign in to comment.