1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Factory; |
4
|
|
|
|
5
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
6
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\FileData; |
9
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\FileInterface; |
10
|
|
|
use Silverback\ApiComponentBundle\Imagine\PathResolver; |
11
|
|
|
use Silverback\ApiComponentBundle\Serializer\ImageMetadata; |
12
|
|
|
use Silverback\ApiComponentBundle\Validator\ImagineSupportedFilePath; |
13
|
|
|
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; |
14
|
|
|
use Symfony\Component\Routing\RouterInterface; |
15
|
|
|
|
16
|
|
|
class FileDataFactory implements ServiceSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
private $router; |
19
|
|
|
private $iriConverter; |
20
|
|
|
private $container; |
21
|
|
|
private $projectDir; |
22
|
|
|
|
23
|
|
|
public function __construct(RouterInterface $router, IriConverterInterface $iriConverter, ContainerInterface $container, string $projectDir) |
24
|
|
|
{ |
25
|
|
|
$this->router = $router; |
26
|
|
|
$this->iriConverter = $iriConverter; |
27
|
|
|
$this->container = $container; |
28
|
|
|
$this->projectDir = $projectDir; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function getSubscribedServices(): array |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
'?' . PathResolver::class, |
35
|
|
|
'?' . CacheManager::class |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function create(FileInterface $file): ?FileData |
40
|
|
|
{ |
41
|
|
|
if (!($filePath = $file->getFilePath()) || !file_exists($filePath)) { |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
if ($file->getFileData()) { |
45
|
|
|
return $file->getFileData(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$publicPath = $this->getPublicPath($file); |
49
|
|
|
$imageData = \exif_imagetype($filePath) ? new ImageMetadata($filePath, $publicPath) : null; |
50
|
|
|
|
51
|
|
|
return new FileData($publicPath, $imageData, $this->getImagineData($file)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getPublicPath(FileInterface $file): string |
55
|
|
|
{ |
56
|
|
|
$objectId = $this->iriConverter->getIriFromItem($file); |
57
|
|
|
return $this->router->generate( |
58
|
|
|
'files_upload', |
59
|
|
|
[ 'field' => 'filePath', 'id' => $objectId ] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getImagineData(FileInterface $file): ?array |
64
|
|
|
{ |
65
|
|
|
$filePath = $file->getFilePath(); |
66
|
|
|
if (!class_exists(CacheManager::class) || !ImagineSupportedFilePath::isValidFilePath($filePath)) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
$imagineData = []; |
70
|
|
|
/** @var PathResolver $pathResolver */ |
71
|
|
|
$pathResolver = $this->container->get(PathResolver::class); |
72
|
|
|
/** @var CacheManager $cacheManager */ |
73
|
|
|
$cacheManager = $this->container->get(CacheManager::class); |
74
|
|
|
foreach ($file::getImagineFilters() as $returnKey => $filter) { |
75
|
|
|
// Strip path root from beginning of string. |
76
|
|
|
// Whatever image roots are set in imagine will be looped and removed from the start of the string |
77
|
|
|
$resolvedPath = $pathResolver->resolve($filePath); |
78
|
|
|
$imagineBrowserPath = $cacheManager->getBrowserPath($resolvedPath, $filter); |
79
|
|
|
$imagineFilePath = ltrim(parse_url( |
80
|
|
|
$imagineBrowserPath, |
81
|
|
|
PHP_URL_PATH |
82
|
|
|
), '/'); |
83
|
|
|
$realPath = sprintf('%s/public/%s', $this->projectDir, $imagineFilePath); |
84
|
|
|
$imagineData[$returnKey] = new ImageMetadata($realPath, $imagineFilePath, $filter); |
85
|
|
|
} |
86
|
|
|
return $imagineData; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|