|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Binary\Loader; |
|
13
|
|
|
|
|
14
|
|
|
use League\Flysystem\FilesystemException; |
|
15
|
|
|
use League\Flysystem\FilesystemOperator; |
|
16
|
|
|
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; |
|
17
|
|
|
use Liip\ImagineBundle\Model\Binary; |
|
18
|
|
|
use Symfony\Component\Mime\MimeTypesInterface; |
|
19
|
|
|
|
|
20
|
|
|
class FlysystemV2Loader implements LoaderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var FilesystemOperator |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $filesystem; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var MimeTypesInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $extensionGuesser; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
MimeTypesInterface $extensionGuesser, |
|
34
|
|
|
FilesystemOperator $filesystem |
|
35
|
|
|
) { |
|
36
|
|
|
$this->extensionGuesser = $extensionGuesser; |
|
37
|
|
|
$this->filesystem = $filesystem; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function find($path) |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
|
|
$mimeType = $this->filesystem->mimeType($path); |
|
47
|
|
|
|
|
48
|
|
|
$extension = $this->getExtension($mimeType); |
|
49
|
|
|
|
|
50
|
|
|
return new Binary( |
|
51
|
|
|
$this->filesystem->read($path), |
|
52
|
|
|
$mimeType, |
|
53
|
|
|
$extension |
|
54
|
|
|
); |
|
55
|
|
|
} catch (FilesystemException $exception) { |
|
56
|
|
|
throw new NotLoadableException(sprintf('Source image "%s" not found.', $path), null, $exception); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function getExtension(?string $mimeType): ?string |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|