Skip to content

Commit 6dea0fa

Browse files
committed
Auto-detect and fallback to api url generator
1 parent 4ecde67 commit 6dea0fa

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/Factory/Uploadable/MediaObjectFactory.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Silverback\ApiComponentsBundle\Entity\Core\FileInfo;
2424
use Silverback\ApiComponentsBundle\Entity\Utility\ImagineFiltersInterface;
2525
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
26+
use Silverback\ApiComponentsBundle\Flysystem\FilesystemFactory;
2627
use Silverback\ApiComponentsBundle\Flysystem\FilesystemProvider;
2728
use Silverback\ApiComponentsBundle\Helper\Uploadable\FileInfoCacheManager;
2829
use Silverback\ApiComponentsBundle\Imagine\FlysystemDataLoader;
@@ -46,6 +47,7 @@ public function __construct(
4647
private readonly FlysystemDataLoader $flysystemDataLoader,
4748
private readonly RequestStack $requestStack,
4849
private readonly ServiceLocator $urlGenerators,
50+
private readonly FilesystemFactory $filesystemFactory,
4951
private readonly ?FilterService $filterService = null
5052
) {
5153
$this->initRegistry($managerRegistry);
@@ -71,7 +73,20 @@ public function createMediaObjects(object $object): ?ArrayCollection
7173
continue;
7274
}
7375

74-
$urlGenerator = $this->urlGenerators->get($fieldConfiguration->urlGenerator);
76+
// todo: consultation on perhaps attributes which can be configured with environment variables or best way to achieve easier implementation
77+
$urlGeneratorReference = $fieldConfiguration->urlGenerator ?? 'api';
78+
$urlGenerator = $this->urlGenerators->get($urlGeneratorReference);
79+
if ($urlGenerator !== 'api') {
80+
$adapter = $this->filesystemFactory->getAdapter($fieldConfiguration->adapter);
81+
if (
82+
($urlGenerator instanceof TemporaryUrlGenerator && !($adapter instanceof \League\Flysystem\UrlGeneration\TemporaryUrlGenerator)) ||
83+
($urlGenerator instanceof PublicUrlGenerator && !($adapter instanceof \League\Flysystem\UrlGeneration\PublicUrlGenerator))
84+
) {
85+
$urlGeneratorReference = 'api';
86+
$urlGenerator = $this->urlGenerators->get($urlGeneratorReference);
87+
}
88+
}
89+
7590
if (!$urlGenerator instanceof UploadableUrlGeneratorInterface) {
7691
throw new InvalidArgumentException(sprintf('The url generator provided must implement %s', UploadableUrlGeneratorInterface::class));
7792
}

src/Flysystem/FilesystemFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function __construct(private readonly ServiceLocator $adapters)
3131
*/
3232
public function create(string $name, array $config = []): Filesystem
3333
{
34-
return new Filesystem($this->adapters->get($name), $config);
34+
return new Filesystem($this->getAdapter($name), $config);
35+
}
36+
37+
public function getAdapter(string $name) {
38+
return $this->adapters->get($name);
3539
}
3640
}

src/Flysystem/FilesystemProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public function __construct(private readonly ServiceLocator $filesystems)
3333

3434
/**
3535
* @throws ContainerExceptionInterface
36-
* @throws NotFoundExceptionInterface
3736
*/
3837
public function getFilesystem(string $name): Filesystem
3938
{

0 commit comments

Comments
 (0)