Skip to content

Commit f6c34e4

Browse files
committed
[FEATURE] Add cloudinary api query by file uid
1 parent f217dca commit f6c34e4

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

Classes/Command/CloudinaryApiCommand.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Output\OutputInterface;
1818
use Symfony\Component\Console\Style\SymfonyStyle;
19+
use TYPO3\CMS\Core\Resource\File;
1920
use TYPO3\CMS\Core\Resource\ResourceFactory;
2021
use TYPO3\CMS\Core\Resource\ResourceStorage;
2122
use TYPO3\CMS\Core\Utility\GeneralUtility;
23+
use Visol\Cloudinary\Services\CloudinaryPathService;
2224
use Visol\Cloudinary\Utility\CloudinaryApiUtility;
2325

2426
class CloudinaryApiCommand extends AbstractCloudinaryCommand
2527
{
2628
protected ResourceStorage $storage;
2729

2830
protected string $help = '
29-
Usage: ./vendor/bin/typo3 cloudinary:api [0-9 storage id]
31+
Usage: ./vendor/bin/typo3 cloudinary:api [storage-uid]
3032
3133
Examples
3234
3335
# Query by public id
3436
typo3 cloudinary:api [0-9] --publicId=\'foo-bar\'
3537
38+
# Query by file uid
39+
typo3 cloudinary:api --fileUid=\'[0-9]\'
40+
3641
# Query with an expression
3742
# @see https://cloudinary.com/documentation/search_api
3843
typo3 cloudinary:api [0-9] --expression=\'public_id:foo-bar\'
@@ -53,9 +58,10 @@ protected function configure()
5358
$message = 'Interact with cloudinary API';
5459
$this->setDescription($message)
5560
->addOption('silent', 's', InputOption::VALUE_OPTIONAL, 'Mute output as much as possible', false)
61+
->addOption('fileUid', '', InputOption::VALUE_OPTIONAL, 'File uid', '')
5662
->addOption('publicId', '', InputOption::VALUE_OPTIONAL, 'Cloudinary public id', '')
5763
->addOption('expression', '', InputOption::VALUE_OPTIONAL, 'Cloudinary search expression', '')
58-
->addArgument('storage', InputArgument::REQUIRED, 'Storage identifier')
64+
->addArgument('storage', InputArgument::OPTIONAL, 'Storage identifier')
5965
->setHelp($this->help);
6066
}
6167

@@ -69,9 +75,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6975
$publicId = $input->getOption('publicId');
7076
$expression = $input->getOption('expression');
7177

78+
// @phpstan-ignore-next-line
79+
$fileUid = (int)$input->getOption('fileUid');
80+
if ($fileUid) {
81+
/** @var ResourceFactory $resourceFactory */
82+
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
83+
$file = $resourceFactory->getFileObject($fileUid);
84+
85+
$this->storage = $file->getStorage(); // just to be sure
86+
$publicId = $this->getPublicIdFromFile($file);
87+
}
88+
7289
$this->initializeApi();
7390
try {
74-
7591
if ($publicId) {
7692
$resource = $this->getApi()->resource($publicId);
7793
$this->log(var_export((array)$resource, true));
@@ -90,6 +106,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
90106
return Command::SUCCESS;
91107
}
92108

109+
protected function getPublicIdFromFile(File $file): string
110+
{
111+
/** @var CloudinaryPathService $cloudinaryPathService */
112+
$cloudinaryPathService = GeneralUtility::makeInstance(
113+
CloudinaryPathService::class,
114+
$file->getStorage()->getConfiguration(),
115+
);
116+
return $cloudinaryPathService->computeCloudinaryPublicId($file->getIdentifier());
117+
}
118+
93119
protected function getApi()
94120
{
95121
// create a new instance upon each API call to avoid driver confusion

0 commit comments

Comments
 (0)