1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Factory\File; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
17
|
|
|
use Silverback\ApiComponentBundle\Dto\File\FileData; |
18
|
|
|
use Silverback\ApiComponentBundle\Dto\File\ImageMetadata; |
19
|
|
|
use Silverback\ApiComponentBundle\Entity\Utility\FileInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\UrlHelper; |
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Daniel West <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class FileDataFactory |
27
|
|
|
{ |
28
|
|
|
private IriConverterInterface $iriConverter; |
29
|
|
|
private RouterInterface $router; |
30
|
|
|
private ImagineMetadataFactory $imagineMetadataFactory; |
31
|
|
|
private UrlHelper $urlHelper; |
32
|
|
|
|
33
|
|
|
public function __construct(IriConverterInterface $iriConverter, RouterInterface $router, ImagineMetadataFactory $imagineMetadataFactory, UrlHelper $urlHelper) |
34
|
|
|
{ |
35
|
|
|
$this->iriConverter = $iriConverter; |
36
|
|
|
$this->router = $router; |
37
|
|
|
$this->imagineMetadataFactory = $imagineMetadataFactory; |
38
|
|
|
$this->urlHelper = $urlHelper; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function create(FileInterface $resource): ?FileData |
42
|
|
|
{ |
43
|
|
|
if (!($filePath = $resource->getFilePath()) || !file_exists($filePath)) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($fileData = $resource->getFileData()) { |
48
|
|
|
return $fileData; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$publicPath = $this->getPublicPath($resource); |
52
|
|
|
|
53
|
|
|
$imageData = self::isImage($filePath) ? new ImageMetadata($filePath, $this->urlHelper->getAbsoluteUrl($publicPath)) : null; |
54
|
|
|
|
55
|
|
|
return new FileData( |
56
|
|
|
$this->urlHelper->getAbsoluteUrl($publicPath), |
57
|
|
|
pathinfo($filePath, PATHINFO_EXTENSION), |
58
|
|
|
filesize($filePath) ?: null, |
59
|
|
|
$imageData, |
60
|
|
|
$this->imagineMetadataFactory->create($resource) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private static function isImage($filePath): bool |
65
|
|
|
{ |
66
|
|
|
return exif_imagetype($filePath) || 'image/svg+xml' === mime_content_type($filePath); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getPublicPath(FileInterface $resource): string |
70
|
|
|
{ |
71
|
|
|
$objectId = $this->iriConverter->getIriFromItem($resource); |
72
|
|
|
|
73
|
|
|
return $this->router->generate( |
74
|
|
|
'api_component_file_upload', |
75
|
|
|
['field' => 'filePath', 'id' => $objectId] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|