1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Binary\Loader; |
4
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
6
|
|
|
use Liip\ImagineBundle\Model\FileBinary; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
9
|
|
|
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; |
10
|
|
|
|
11
|
|
|
class FileSystemLoader implements LoaderInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var MimeTypeGuesserInterface |
15
|
|
|
*/ |
16
|
|
|
protected $mimeTypeGuesser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ExtensionGuesserInterface |
20
|
|
|
*/ |
21
|
|
|
protected $extensionGuesser; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $rootPath; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param MimeTypeGuesserInterface $mimeTypeGuesser |
30
|
|
|
* @param ExtensionGuesserInterface $extensionGuesser |
31
|
|
|
* @param string $rootPath |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
MimeTypeGuesserInterface $mimeTypeGuesser, |
35
|
|
|
ExtensionGuesserInterface $extensionGuesser, |
36
|
|
|
$rootPath |
37
|
|
|
) { |
38
|
|
|
$this->mimeTypeGuesser = $mimeTypeGuesser; |
39
|
|
|
$this->extensionGuesser = $extensionGuesser; |
40
|
|
|
|
41
|
|
|
if (empty($rootPath) || !($realRootPath = realpath($rootPath))) { |
42
|
|
|
throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $rootPath)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->rootPath = $realRootPath; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function find($path) |
52
|
|
|
{ |
53
|
|
|
if (!($absolutePath = realpath($this->rootPath.DIRECTORY_SEPARATOR.$path))) { |
54
|
|
|
throw new NotLoadableException(sprintf('Source image not resolvable "%s"', $path)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (0 !== strpos($absolutePath, $this->rootPath)) { |
58
|
|
|
throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path', $absolutePath)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$mimeType = $this->mimeTypeGuesser->guess($absolutePath); |
62
|
|
|
|
63
|
|
|
return new FileBinary( |
64
|
|
|
$absolutePath, |
65
|
|
|
$mimeType, |
66
|
|
|
$this->extensionGuesser->guess($mimeType) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|