-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTestController.php
More file actions
155 lines (125 loc) · 5.45 KB
/
TestController.php
File metadata and controls
155 lines (125 loc) · 5.45 KB
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
<?php declare(strict_types=1);
namespace Frosh\ThumbnailProcessor\Controller\Api;
use Frosh\ThumbnailProcessor\Core\Media\ExtendedUrlParam;
use Frosh\ThumbnailProcessor\Core\Media\ExtendedUrlParams;
use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
use Shopware\Core\Content\Media\Core\Application\AbstractMediaUrlGenerator;
use Shopware\Core\Content\Media\File\FileFetcher;
use Shopware\Core\Content\Media\File\FileSaver;
use Shopware\Core\Content\Media\MediaCollection;
use Shopware\Core\Content\Media\MediaEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\PlatformRequest;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderCollection;
#[Route(defaults: ['_routeScope' => ['api']])]
class TestController
{
public const REQUEST_ATTRIBUTE_TEST_ACTIVE = 'FroshPlatformThumbnailProcessorTestActive';
public const TEST_FILE_PATH = __DIR__ . '/../../Resources/data/froshthumbnailprocessortestimage.jpg';
/**
* @param EntityRepository<MediaCollection> $mediaRepository
* @param EntityRepository<MediaFolderCollection> $mediaFolderRepository
*/
public function __construct(
private readonly AbstractMediaUrlGenerator $urlGenerator,
private readonly EntityRepository $mediaRepository,
private readonly EntityRepository $mediaFolderRepository,
private readonly FileSaver $fileSaver,
private readonly FileFetcher $fileFetcher
) {
}
#[Route(path: '/api/_action/thumbnail-processor-test/get-sample-image')]
public function check(Request $request, RequestDataBag $dataBag): JsonResponse
{
if (!$dataBag->has('salesChannelId')) {
return new JsonResponse(['success' => false]);
}
$testFile = \realpath(self::TEST_FILE_PATH);
if (!\is_string($testFile) || !\is_file($testFile) || !\is_readable($testFile)) {
throw new \RuntimeException(\sprintf('Test file at "%s" is missing or not readable', $testFile));
}
$request->attributes->set(self::REQUEST_ATTRIBUTE_TEST_ACTIVE, '1');
$salesChannelId = $dataBag->get('salesChannelId');
if (\is_string($salesChannelId)) {
$request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID, $salesChannelId);
}
$media = $this->getSampleMedia($testFile);
$thumbnail = new MediaThumbnailEntity();
$thumbnail->assign($media->getVars());
$thumbnail->setMediaId($media->getId());
$thumbnail->setWidth(200);
$thumbnail->setHeight(200);
$urlParam = ExtendedUrlParams::fromMedia($media);
$thumbnail->setTranslated(['mediaUrlParam' => ExtendedUrlParam::fromUrlParams($urlParam)]);
return new JsonResponse([
'url' => current($this->urlGenerator->generate([ExtendedUrlParams::fromThumbnail($thumbnail)])),
]);
}
private function getProductFolderId(Context $context): string
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('media_folder.defaultFolder.entity', 'product'))
->addAssociation('defaultFolder')
->setLimit(1);
$id = $this->mediaFolderRepository
->searchIds($criteria, $context)
->getIds()[0] ?? null;
if (\is_string($id)) {
return $id;
}
throw new \RuntimeException('Media folder for product could not have been found!');
}
private function getSampleMedia(string $testFile): MediaEntity
{
$fileContent = \file_get_contents($testFile);
\assert(\is_string($fileContent));
$context = Context::createDefaultContext();
$mediaId = \hash('xxh128', $fileContent);
$pathInfo = pathinfo($testFile);
$existingMedia = $this->getMediaById($pathInfo['filename'], $context);
if ($existingMedia !== null) {
return $existingMedia;
}
$mediaFolderId = $this->getProductFolderId($context);
$this->mediaRepository->upsert(
[
[
'id' => $mediaId,
'mediaFolderId' => $mediaFolderId,
],
],
$context
);
$uploadedFile = $this->fileFetcher->fetchBlob(
$fileContent,
'jpg',
'image/jpg'
);
$this->fileSaver->persistFileToMedia(
$uploadedFile,
$pathInfo['filename'],
$mediaId,
$context
);
$existingMedia = $this->getMediaById($pathInfo['filename'], $context);
if ($existingMedia !== null) {
return $existingMedia;
}
throw new \RuntimeException('Media has not been saved!');
}
private function getMediaById(string $fileName, Context $context): ?MediaEntity
{
$criteria = new Criteria();
// we use the fileName filter to add backward compatibility
$criteria->addFilter(new EqualsFilter('fileName', $fileName));
$entities = $this->mediaRepository->search($criteria, $context)->getEntities();
return $entities->first();
}
}