Skip to content

Commit f236847

Browse files
authored
Merge pull request #9 from networkteam/feature-sourceuri-eel-helper
Add SourceUri Eel Helper
2 parents ba7ca8f + 0ac61dc commit f236847

3 files changed

Lines changed: 69 additions & 15 deletions

File tree

Classes/Aspects/ThumbnailAspect.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Networkteam\ImageProxy\Aspects;
44

5+
use Neos\Flow\Annotations as Flow;
56
use Neos\Flow\Aop\JoinPointInterface;
67
use Neos\Flow\Mvc\ActionRequest;
7-
use Neos\Flow\ResourceManagement\ResourceManager;
88
use Neos\Media\Domain\Model\Asset;
99
use Neos\Media\Domain\Model\Image;
1010
use Neos\Media\Domain\Model\ImageVariant;
1111
use Neos\Media\Domain\Model\ThumbnailConfiguration;
12-
use Neos\Flow\Annotations as Flow;
12+
use Networkteam\ImageProxy\Eel\SourceUriHelper;
1313
use Networkteam\ImageProxy\ImgproxyBuilder;
1414
use Networkteam\ImageProxy\Model\Dimensions;
1515

@@ -32,9 +32,9 @@ class ThumbnailAspect
3232

3333
/**
3434
* @Flow\Inject
35-
* @var ResourceManager
35+
* @var SourceUriHelper
3636
*/
37-
protected $resourceManager;
37+
protected $sourceUriHelper;
3838

3939
/**
4040
* @Flow\Around("method(Neos\Media\Domain\Service\AssetService->getThumbnailUriAndSizeForAsset())")
@@ -62,17 +62,7 @@ public function generateImgproxyUri(JoinPointInterface $joinPoint): ?array
6262
$this->settings['salt']
6363
);
6464

65-
$sourceUri = '';
66-
67-
$resourceCollection = $this->resourceManager->getCollection($asset->getResource()->getCollectionName());
68-
$resourceStorage = $resourceCollection->getStorage();
69-
if (get_class($resourceStorage) === 'Flownative\Aws\S3\S3Storage') {
70-
$bucketName = $resourceStorage->getBucketName();
71-
$keyPrefix = $resourceStorage->getKeyPrefix();
72-
$sourceUri = sprintf('s3://%s/%s/%s', $bucketName, rtrim($keyPrefix, '/'), $asset->getResource()->getSha1());
73-
} else {
74-
$sourceUri = $this->resourceManager->getPublicPersistentResourceUri($asset->getResource());
75-
}
65+
$sourceUri = $this->sourceUriHelper->sourceUri($asset->getResource());
7666

7767
$targetHeight = $configuration->getHeight() ?? $configuration->getMaximumHeight() ?? 0;
7868
$targetWidth = $configuration->getWidth() ?? $configuration->getMaximumWidth() ?? 0;
@@ -140,4 +130,5 @@ protected function getRequest(JoinPointInterface $joinPoint): ActionRequest
140130
{
141131
return $joinPoint->getMethodArgument('request');
142132
}
133+
143134
}

Classes/Eel/SourceUriHelper.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Networkteam\ImageProxy\Eel;
4+
5+
/***************************************************************
6+
* (c) 2022 networkteam GmbH - all rights reserved
7+
***************************************************************/
8+
9+
use Neos\Eel\ProtectedContextAwareInterface;
10+
use Neos\Flow\Annotations as Flow;
11+
use Neos\Flow\ResourceManagement\PersistentResource;
12+
use Neos\Flow\ResourceManagement\ResourceManager;
13+
use Neos\Media\Domain\Model\Asset;
14+
use Networkteam\ImageProxy\Aspects\ThumbnailAspect;
15+
16+
class SourceUriHelper implements ProtectedContextAwareInterface
17+
{
18+
19+
/**
20+
* @Flow\Inject
21+
* @var ResourceManager
22+
*/
23+
protected $resourceManager;
24+
25+
/**
26+
* Get the source URI for fetching a resource.
27+
*
28+
* @param PersistentResource $resource The resource to generate the URI for
29+
* @return string The source URI of the resource (could be either a http or s3 URL depending on the resource storage)
30+
*/
31+
public function sourceUri(PersistentResource $resource): string
32+
{
33+
$sourceUri = '';
34+
35+
$resourceCollection = $this->resourceManager->getCollection($resource->getCollectionName());
36+
$resourceStorage = $resourceCollection->getStorage();
37+
if (get_class($resourceStorage) === 'Flownative\Aws\S3\S3Storage') {
38+
$bucketName = $resourceStorage->getBucketName();
39+
$keyPrefix = $resourceStorage->getKeyPrefix();
40+
$sourceUri = sprintf('s3://%s/%s/%s', $bucketName, rtrim($keyPrefix, '/'), $resource->getSha1());
41+
} else {
42+
$sourceUri = $this->resourceManager->getPublicPersistentResourceUri($resource);
43+
}
44+
45+
return $sourceUri;
46+
}
47+
48+
/**
49+
* All methods are considered safe, i.e. can be executed from within Eel
50+
*
51+
* @param string $methodName
52+
* @return boolean
53+
*/
54+
public function allowsCallOfMethod($methodName)
55+
{
56+
return true;
57+
}
58+
}

Configuration/Settings.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Neos:
2+
Fusion:
3+
defaultContext:
4+
Networkteam.ImageProxy: Networkteam\ImageProxy\Eel\SourceUriHelper
5+
16
Networkteam:
27
ImageProxy:
38
# The imgproxy base URL

0 commit comments

Comments
 (0)