Skip to content

Commit

Permalink
Search by url
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasHermanek committed Oct 23, 2024
1 parent c673ca9 commit 2fb2f98
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Requirement\Requirement;

#[Route(path: '/image', name: 'image_')]
final class ImageController extends AbstractImageController
Expand Down Expand Up @@ -77,7 +78,7 @@ public function animation(
path: '/{requestWidth}{requestHeight}{regionOfInterestId}{quality}/{imageId}.jpg',
name: 'get_one_file_name',
requirements: [
'imageId' => '[0-9a-zA-Z-]+',
'imageId' => Requirement::UUID,
'requestWidth' => 'w\d+',
'requestHeight' => '-h\d+',
'regionOfInterestId' => '(-c\d+)|',
Expand Down
7 changes: 3 additions & 4 deletions src/Elasticsearch/QueryFactory/AssetQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use AnzuSystems\CoreDamBundle\Entity\AssetLicence;
use AnzuSystems\CoreDamBundle\Entity\CustomFormElement;
use AnzuSystems\CoreDamBundle\Entity\ExtSystem;
use AnzuSystems\CoreDamBundle\Helper\UuidHelper;

final class AssetQueryFactory extends AbstractQueryFactory
{
Expand Down Expand Up @@ -41,7 +40,7 @@ protected function getMust(SearchDtoInterface $searchDto, ExtSystem $extSystem):
$customDataFields = array_unique($customDataFields);
$customDataFields = array_merge($customDataFields, ['title']);

if (UuidHelper::isUuid($searchDto->getText())) {
if (is_string($searchDto->getIdInText())) {
return parent::getMust($searchDto, $extSystem);
}

Expand Down Expand Up @@ -70,8 +69,8 @@ protected function getFilter(SearchDtoInterface $searchDto): array
$this->applyLicenceCollectionFilter($filter, $searchDto);
}

if (UuidHelper::isUuid($searchDto->getText())) {
$filter[] = $this->getAssetIdAndMainFileIdFilter([$searchDto->getText()]);
if (is_string($searchDto->getIdInText())) {
$filter[] = $this->getAssetIdAndMainFileIdFilter([$searchDto->getIdInText()]);

// other filters should not be applied
return $filter;
Expand Down
23 changes: 23 additions & 0 deletions src/Elasticsearch/SearchDto/AssetAdmSearchDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use AnzuSystems\CommonBundle\Exception\ValidationException;
use AnzuSystems\CoreDamBundle\Entity\Asset;
use AnzuSystems\CoreDamBundle\Entity\AssetLicence;
use AnzuSystems\CoreDamBundle\Helper\UrlHelper;
use AnzuSystems\CoreDamBundle\Helper\UuidHelper;
use AnzuSystems\CoreDamBundle\Model\Enum\AssetStatus;
use AnzuSystems\CoreDamBundle\Model\Enum\AssetType;
use AnzuSystems\CoreDamBundle\Model\Enum\ImageOrientation;
Expand Down Expand Up @@ -179,6 +181,8 @@ class AssetAdmSearchDto extends AbstractSearchDto

#[Serialize]
private ?DateTimeImmutable $createdAtUntil = null;
private ?string $idInText = null;
private bool $resolvedIdInText = false;

public function getIndexName(): string
{
Expand Down Expand Up @@ -698,4 +702,23 @@ public function setAssetAndMainFileIds(array $assetAndMainFileIds): self

return $this;
}

public function getIdInText(): ?string
{
if (false === $this->resolvedIdInText) {
$this->idInText = $this->resolveIdFromText();
$this->resolvedIdInText = true;
}

return $this->idInText;
}

private function resolveIdFromText(): ?string
{
if (UuidHelper::isUuid($this->getText())) {
return $this->getText();
}

return UrlHelper::getImageIdFromUrl($this->getText());
}
}
9 changes: 9 additions & 0 deletions src/Helper/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
namespace AnzuSystems\CoreDamBundle\Helper;

use AnzuSystems\CoreDamBundle\Model\ValueObject\Url;
use Symfony\Component\Routing\Requirement\Requirement;

final class UrlHelper
{
public static function getImageIdFromUrl(string $url): ?string
{
$path = (string) parse_url($url, PHP_URL_PATH);
preg_match('/^\/image\/w\d+\-h\d+(-c\d+)?(-q\d+)?\/(?<imageId>' . Requirement::UUID . ')\.jpg$/', $path, $matches);

return $matches['imageId'] ?? null;
}

public static function concatPathWithDomain(string $domain, string $path): string
{
if (str_ends_with($domain, '/')) {
Expand Down

0 comments on commit 2fb2f98

Please sign in to comment.