Skip to content

Commit

Permalink
Adjust the code to behave differently based on folder_mode setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy Čupić committed Sep 17, 2024
1 parent b05d6fe commit ef66ac3
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 26 deletions.
5 changes: 5 additions & 0 deletions bundle/Resources/config/services/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ services:
arguments:
- "@netgen_remote_media.provider.cloudinary.gateway.inner"
- "@netgen_remote_media.cache.pool"
- "%netgen_remote_media.cloudinary.folder_mode%"
- "%netgen_remote_media.cache.ttl%"

netgen_remote_media.provider.cloudinary.gateway.logged:
Expand All @@ -48,6 +49,7 @@ services:
- "@netgen_remote_media.provider.cloudinary.resolver.upload_options"
- "%netgen_remote_media.named_remote_resources%"
- "%netgen_remote_media.named_remote_resource_locations%"
- '%netgen_remote_media.cloudinary.folder_mode%'
- "@?logger"

netgen_remote_media.provider.cloudinary.converter.resource_type:
Expand Down Expand Up @@ -79,6 +81,7 @@ services:
- '@netgen_remote_media.provider.cloudinary.converter.resource_type'
- '@netgen_remote_media.provider.cloudinary.converter.visibility_type'
- '@netgen_remote_media.factory.md5_file_hash'
- '%netgen_remote_media.cloudinary.folder_mode%'

netgen_remote_media.provider.cloudinary.factory.search_result:
class: Netgen\RemoteMedia\Core\Provider\Cloudinary\Factory\SearchResult
Expand All @@ -97,13 +100,15 @@ services:
public: false
arguments:
- '@netgen_remote_media.provider.cloudinary.converter.visibility_type'
- '%netgen_remote_media.cloudinary.folder_mode%'

netgen_remote_media.provider.cloudinary.resolver.search_expression:
class: Netgen\RemoteMedia\Core\Provider\Cloudinary\Resolver\SearchExpression
public: false
arguments:
- '@netgen_remote_media.provider.cloudinary.converter.resource_type'
- '@netgen_remote_media.provider.cloudinary.converter.visibility_type'
- '%netgen_remote_media.cloudinary.folder_mode%'

netgen_remote_media.factory.date_time:
class: Netgen\RemoteMedia\Core\Factory\DateTime
Expand Down
29 changes: 15 additions & 14 deletions lib/Core/Provider/Cloudinary/CloudinaryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public function __construct(
private UploadOptionsResolver $uploadOptionsResolver,
array $namedRemoteResources,
array $namedRemoteResourceLocations,
private string $folderMode,
?LoggerInterface $logger = null,
bool $shouldDeleteFromRemote = false
bool $shouldDeleteFromRemote = false,
) {
parent::__construct(
$registry,
Expand Down Expand Up @@ -121,7 +122,7 @@ public function loadFromRemote(string $remoteId): RemoteResource
{
try {
return $this->gateway->get(
CloudinaryRemoteId::fromRemoteId($remoteId),
CloudinaryRemoteId::fromRemoteId($remoteId, $this->folderMode),
);
} catch (InvalidRemoteIdException $exception) {
$this->logger->notice('[NGRM][Cloudinary] ' . $exception->getMessage());
Expand All @@ -133,7 +134,7 @@ public function loadFromRemote(string $remoteId): RemoteResource
public function deleteFromRemote(RemoteResource $resource): void
{
$this->gateway->delete(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
);
}

Expand Down Expand Up @@ -163,12 +164,12 @@ public function updateOnRemote(RemoteResource $resource): void

if (count($resource->getTags()) === 0) {
$this->gateway->removeAllTagsFromResource(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
);
}

$this->gateway->update(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$options,
);
}
Expand All @@ -181,23 +182,23 @@ public function generateDownloadLink(RemoteResource $resource, array $transforma
}

return $this->gateway->getDownloadLink(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$options,
$resource instanceof AuthenticatedRemoteResource ? $resource->getToken() : null,
);
}

public function authenticateRemoteResource(RemoteResource $resource, AuthToken $token): AuthenticatedRemoteResource
{
$url = $this->gateway->getAuthenticatedUrl(CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()), $token);
$url = $this->gateway->getAuthenticatedUrl(CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode), $token);

return new AuthenticatedRemoteResource($resource, $url, $token);
}

public function authenticateRemoteResourceLocation(RemoteResourceLocation $location, AuthToken $token): RemoteResourceLocation
{
$url = $this->gateway->getAuthenticatedUrl(
CloudinaryRemoteId::fromRemoteId($location->getRemoteResource()->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($location->getRemoteResource()->getRemoteId(), $this->folderMode),
$token,
);

Expand Down Expand Up @@ -257,7 +258,7 @@ protected function internalUpload(ResourceStruct $resourceStruct): RemoteResourc
protected function internalBuildVariation(RemoteResource $resource, array $transformations = []): RemoteResourceVariation
{
$variationUrl = $this->gateway->getVariationUrl(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$transformations,
$resource instanceof AuthenticatedRemoteResource ? $resource->getToken() : null,
);
Expand All @@ -279,7 +280,7 @@ protected function internalBuildVideoThumbnail(RemoteResource $resource, array $
$options['start_offset'] = $startOffset !== null ? $startOffset : 'auto';

$thumbnailUrl = $this->gateway->getVideoThumbnail(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$options,
$resource instanceof AuthenticatedRemoteResource ? $resource->getToken() : null,
);
Expand Down Expand Up @@ -312,7 +313,7 @@ protected function generatePictureTag(RemoteResource $resource, array $transform
}

return $this->gateway->getImageTag(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$options,
$resource instanceof AuthenticatedRemoteResource ? $resource->getToken() : null,
);
Expand Down Expand Up @@ -340,7 +341,7 @@ protected function generateVideoTag(RemoteResource $resource, array $transformat
}

return $this->gateway->getVideoTag(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$options,
$resource instanceof AuthenticatedRemoteResource ? $resource->getToken() : null,
);
Expand Down Expand Up @@ -368,7 +369,7 @@ protected function generateVideoThumbnailTag(RemoteResource $resource, array $tr
}

$thumbnailTag = $this->gateway->getImageTag(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$options,
$resource instanceof AuthenticatedRemoteResource ? $resource->getToken() : null,
);
Expand All @@ -392,7 +393,7 @@ protected function generateAudioTag(RemoteResource $resource, array $transformat
];

$tag = $this->gateway->getVideoTag(
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId()),
CloudinaryRemoteId::fromRemoteId($resource->getRemoteId(), $this->folderMode),
$options,
$resource instanceof AuthenticatedRemoteResource ? $resource->getToken() : null,
);
Expand Down
17 changes: 14 additions & 3 deletions lib/Core/Provider/Cloudinary/CloudinaryRemoteId.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Netgen\RemoteMedia\API\Values\Folder;
use Netgen\RemoteMedia\Exception\Cloudinary\InvalidRemoteIdException;
use Netgen\RemoteMedia\Exception\NotSupportedException;

use function array_pop;
use function count;
Expand All @@ -17,22 +18,24 @@ final class CloudinaryRemoteId
public function __construct(
private string $type,
private string $resourceType,
private string $resourceId
private string $resourceId,
private string $folderMode = CloudinaryProvider::FOLDER_MODE_FIXED,
) {}

public static function fromCloudinaryData(array $data): self
public static function fromCloudinaryData(array $data, string $folderMode = CloudinaryProvider::FOLDER_MODE_FIXED): self
{
return new self(
$data['type'] ?? 'upload',
$data['resource_type'] ?? 'image',
$data['public_id'],
$folderMode,
);
}

/**
* @throws InvalidRemoteIdException
*/
public static function fromRemoteId(string $remoteId): self
public static function fromRemoteId(string $remoteId, string $folderMode = CloudinaryProvider::FOLDER_MODE_FIXED): self
{
$parts = explode('|', $remoteId);

Expand All @@ -44,6 +47,7 @@ public static function fromRemoteId(string $remoteId): self
$parts[0],
$parts[1],
$parts[2],
$folderMode,
);
}

Expand Down Expand Up @@ -75,6 +79,13 @@ public function getResourceId(): string

public function getFolder(): ?Folder
{
if ($this->folderMode !== CloudinaryProvider::FOLDER_MODE_FIXED) {
throw new NotSupportedException(
'Cloudinary',
sprintf('fetching folder from path in "%s" folder mode', $this->folderMode),
);
}

$resourceIdParts = explode('/', $this->resourceId);
array_pop($resourceIdParts);

Expand Down
22 changes: 19 additions & 3 deletions lib/Core/Provider/Cloudinary/Factory/RemoteResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Cloudinary\Asset\Media;
use Netgen\RemoteMedia\API\Factory\FileHash as FileHashFactoryInterface;
use Netgen\RemoteMedia\API\Factory\RemoteResource as RemoteResourceFactoryInterface;
use Netgen\RemoteMedia\API\Values\Folder;
use Netgen\RemoteMedia\API\Values\RemoteResource as RemoteResourceValue;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryRemoteId;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\ResourceType as ResourceTypeConverter;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\VisibilityType as VisibilityTypeConverter;
Expand All @@ -25,14 +27,15 @@ final class RemoteResource implements RemoteResourceFactoryInterface
public function __construct(
private ResourceTypeConverter $resourceTypeConverter,
private VisibilityTypeConverter $visibilityTypeConverter,
private FileHashFactoryInterface $fileHashFactory
private FileHashFactoryInterface $fileHashFactory,
private string $folderMode,
) {}

public function create($data): RemoteResourceValue
{
$this->validateData($data);

$cloudinaryRemoteId = CloudinaryRemoteId::fromCloudinaryData($data);
$cloudinaryRemoteId = CloudinaryRemoteId::fromCloudinaryData($data, $this->folderMode);

return new RemoteResourceValue(
remoteId: $cloudinaryRemoteId->getRemoteId(),
Expand All @@ -43,7 +46,7 @@ public function create($data): RemoteResourceValue
originalFilename: $this->resolveOriginalFilename($data),
version: ($data['version'] ?? null) !== null ? (string) $data['version'] : null,
visibility: $this->resolveVisibility($data),
folder: $cloudinaryRemoteId->getFolder(),
folder: $this->resolveFolder($data),
size: $data['bytes'] ?? 0,
altText: $this->resolveAltText($data),
caption: $this->resolveCaption($data),
Expand Down Expand Up @@ -97,6 +100,19 @@ private function resolveVisibility(array $data): string
return $this->visibilityTypeConverter->fromCloudinaryType($type);
}

private function resolveFolder(array $data): ?Folder
{
if ($this->folderMode === CloudinaryProvider::FOLDER_MODE_FIXED) {
return CloudinaryRemoteId::fromCloudinaryData($data, $this->folderMode)->getFolder();
}

if (($data['asset_folder'] ?? '') === '') {
return null;
}

return Folder::fromPath($data['asset_folder']);
}

private function resolveAltText(array $data): ?string
{
if (($data['context']['custom']['alt_text'] ?? null) !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ final class Psr6CachedGateway implements CacheableGatewayInterface
public function __construct(
private GatewayInterface $gateway,
private CacheItemPoolInterface $cache,
private int $ttl = 7200
private string $folderMode,
private int $ttl = 7200,
) {}

public function usage(): StatusData
Expand Down Expand Up @@ -198,7 +199,7 @@ public function upload(string $fileUri, array $options): RemoteResource
{
$uploadResult = $this->gateway->upload($fileUri, $options);

$this->invalidateResourceCache(CloudinaryRemoteId::fromRemoteId($uploadResult->getRemoteId()));
$this->invalidateResourceCache(CloudinaryRemoteId::fromRemoteId($uploadResult->getRemoteId(), $this->folderMode));
$this->invalidateResourceListCache();
$this->invalidateFoldersCache();
$this->invalidateTagsCache();
Expand Down
8 changes: 6 additions & 2 deletions lib/Core/Provider/Cloudinary/Resolver/SearchExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Netgen\RemoteMedia\API\Search\Query;
use Netgen\RemoteMedia\API\Values\RemoteResource;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryRemoteId;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\ResourceType as ResourceTypeConverter;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\VisibilityType as VisibilityTypeConverter;
Expand All @@ -30,6 +31,7 @@ final class SearchExpression
public function __construct(
private ResourceTypeConverter $resourceTypeConverter,
private VisibilityTypeConverter $visibilityTypeConverter,
private string $folderMode,
) {}

public function resolve(Query $query): string
Expand Down Expand Up @@ -230,7 +232,9 @@ private function resolveFolders(Query $query): ?string
return null;
}

$folders = array_map(static fn ($value) => sprintf('folder:"%s"', $value), $query->getFolders());
$key = $this->folderMode === CloudinaryProvider::FOLDER_MODE_DYNAMIC ? 'asset_folder' : 'folder';

$folders = array_map(static fn ($value) => sprintf('%s:"%s"', $key, $value), $query->getFolders());

return '(' . implode(' OR ', $folders) . ')';
}
Expand All @@ -254,7 +258,7 @@ private function resolveResourceIds(Query $query): ?string

$resourceIds = array_unique(
array_map(
static fn ($remoteId) => CloudinaryRemoteId::fromRemoteId($remoteId)->getResourceId(),
static fn ($remoteId) => CloudinaryRemoteId::fromRemoteId($remoteId, $this->folderMode)->getResourceId(),

Check failure on line 261 in lib/Core/Provider/Cloudinary/Resolver/SearchExpression.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $this
$query->getRemoteIds(),
),
);
Expand Down
12 changes: 10 additions & 2 deletions lib/Core/Provider/Cloudinary/Resolver/UploadOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Netgen\RemoteMedia\API\Upload\FileStruct;
use Netgen\RemoteMedia\API\Upload\ResourceStruct;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\Converter\VisibilityType as VisibilityTypeConverter;
use Netgen\RemoteMedia\Exception\MimeCategoryParseException;
use Netgen\RemoteMedia\Exception\MimeTypeNotFoundException;
Expand All @@ -25,6 +26,7 @@ final class UploadOptions
{
public function __construct(
private VisibilityTypeConverter $visibilityTypeConverter,
private string $folderMode,
private array $noExtensionMimeTypes = ['image', 'video'],
private ?MimeTypesInterface $mimeTypes = null
) {
Expand All @@ -48,11 +50,11 @@ public function resolve(ResourceStruct $resourceStruct): array
$publicId = md5_file($resourceStruct->getFileStruct()->getUri());
}

if ($resourceStruct->getFolder()) {
if ($resourceStruct->getFolder() && $this->folderMode === CloudinaryProvider::FOLDER_MODE_FIXED) {
$publicId = $resourceStruct->getFolder()->getPath() . '/' . $publicId;
}

return [
$options = [
'public_id' => $publicId,
'overwrite' => $resourceStruct->doOverwrite(),
'invalidate' => $resourceStruct->doInvalidate() || $resourceStruct->doOverwrite(),
Expand All @@ -64,6 +66,12 @@ public function resolve(ResourceStruct $resourceStruct): array
'access_control' => $this->visibilityTypeConverter->toCloudinaryAccessControl($resourceStruct->getVisibility()),
'tags' => $resourceStruct->getTags(),
];

if ($resourceStruct->getFolder() && $this->folderMode === CloudinaryProvider::FOLDER_MODE_DYNAMIC) {
$options['folder'] = $resourceStruct->getFolder()->getPath();
}

return $options;
}

private function appendExtension(string $publicId, FileStruct $fileStruct): string
Expand Down

0 comments on commit ef66ac3

Please sign in to comment.