-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
AssetProxiesController.php
176 lines (157 loc) · 5.86 KB
/
AssetProxiesController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Neos\Neos\Controller\Service;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Mvc\Exception\StopActionException;
use Neos\Flow\Mvc\Exception\UnsupportedRequestTypeException;
use Neos\Flow\Mvc\View\ViewInterface;
use Neos\FluidAdaptor\View\TemplateView;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
use Neos\Media\Domain\Model\Dto\AssetConstraints;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Media\Domain\Service\AssetSourceService;
use Neos\Media\Domain\Repository\TagRepository;
use Neos\Media\Exception\AssetSourceServiceException;
use Neos\Neos\Controller\BackendUserTranslationTrait;
use Neos\Neos\View\Service\AssetJsonView;
/**
* Rudimentary REST service for assets
*
* @Flow\Scope("singleton")
*/
class AssetProxiesController extends ActionController
{
use BackendUserTranslationTrait;
/**
* @Flow\Inject
* @var AssetRepository
*/
protected $assetRepository;
/**
* @Flow\Inject
* @var AssetSourceService
*/
protected $assetSourceService;
/**
* @Flow\Inject
* @var TagRepository
*/
protected $tagRepository;
/**
* @Flow\InjectConfiguration(package="Neos.Media", path="asyncThumbnails")
* @var boolean
*/
protected $asyncThumbnails;
/**
* @var array
*/
protected $viewFormatToObjectNameMap = [
'html' => TemplateView::class,
'json' => AssetJsonView::class
];
/**
* A list of IANA media types which are supported by this controller
*
* @var array
* @see http://www.iana.org/assignments/media-types/index.html
*/
protected $supportedMediaTypes = [
'text/html',
'application/json'
];
/**
* @param ViewInterface $view
* @return void
*/
public function initializeView(ViewInterface $view)
{
$view->assign('asyncThumbnails', $this->asyncThumbnails);
}
/**
* Shows a list of assets
*
* @param string $searchTerm An optional search term used for filtering the list of assets
* @param int $limit The maximum number of results shown in total
* @return void
*/
public function indexAction(string $searchTerm = '', int $limit = 10): void
{
$assetConstraints = $this->request->hasArgument('constraints') ? AssetConstraints::fromArray($this->request->getArgument('constraints')) : AssetConstraints::create();
$assetSources = $assetConstraints->applyToAssetSources($this->assetSourceService->getAssetSources());
$assetProxyQueryResultsIterator = new \MultipleIterator(\MultipleIterator::MIT_NEED_ANY);
foreach ($assetSources as $assetSource) {
$assetRepository = $assetSource->getAssetProxyRepository();
$assetRepository->filterByType($assetConstraints->applyToAssetTypeFilter());
$assetProxyQueryResult = $assetRepository->findBySearchTerm($searchTerm);
if ($assetProxyQueryResult->valid()) {
$assetProxyQueryResultsIterator->attachIterator($assetProxyQueryResult);
}
}
$totalAddedResults = 0;
$assetProxiesByAssetSource = [];
foreach ($assetProxyQueryResultsIterator as $assetProxies) {
foreach ($assetProxies as $assetProxy) {
if ($assetProxy instanceof AssetProxyInterface) {
$assetProxiesByAssetSource[$assetProxy->getAssetSource()->getIdentifier()][] = $assetProxy;
$totalAddedResults ++;
}
if ($totalAddedResults === $limit) {
break 2;
}
}
}
$this->view->assign('assetProxiesByAssetSource', $assetProxiesByAssetSource);
}
/**
* Shows a specific asset proxy
*
* @param string $assetSourceIdentifier
* @param string $assetProxyIdentifier
* @return void
* @throws StopActionException
* @throws UnsupportedRequestTypeException
*/
public function showAction(string $assetSourceIdentifier, string $assetProxyIdentifier): void
{
$assetSources = $this->assetSourceService->getAssetSources();
if (!isset($assetSources[$assetSourceIdentifier])) {
$this->throwStatus(404, 'Asset source not found');
}
$assetProxyRepository = $assetSources[$assetSourceIdentifier]->getAssetProxyRepository();
$assetProxy = $assetProxyRepository->getAssetProxy($assetProxyIdentifier);
if (!$assetProxy) {
$this->throwStatus(404, 'Asset proxy not found');
}
$this->view->assign('assetProxy', $assetProxy);
}
/**
* @param string $assetSourceIdentifier
* @param string $assetProxyIdentifier
* @return void
* @throws AssetSourceServiceException
* @throws StopActionException
* @throws UnsupportedRequestTypeException
*/
public function importAction(string $assetSourceIdentifier, string $assetProxyIdentifier): void
{
$assetSources = $this->assetSourceService->getAssetSources();
if (!isset($assetSources[$assetSourceIdentifier])) {
$this->throwStatus(404, 'Asset source not found');
}
$importedAsset = $this->assetSourceService->importAsset($assetSourceIdentifier, $assetProxyIdentifier);
$assetProxy = new \stdClass();
$assetProxy->identifier = $assetProxyIdentifier;
$assetProxy->assetSource = $assetSources[$assetSourceIdentifier];
$assetProxy->localAssetIdentifier = $importedAsset->getLocalAssetIdentifier();
$this->view->assign('assetProxy', $assetProxy);
}
}