|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components 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\ApiComponentsBundle\Imagine; |
|
15
|
|
|
|
|
16
|
|
|
use Liip\ImagineBundle\Binary\Loader\LoaderInterface; |
|
17
|
|
|
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; |
|
18
|
|
|
use Liip\ImagineBundle\Model\Binary; |
|
19
|
|
|
use Silverback\ApiComponentsBundle\Flysystem\FilesystemProvider; |
|
20
|
|
|
use Symfony\Component\Mime\MimeTypes; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Daniel West <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class FlysystemDataLoader implements LoaderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
protected FilesystemProvider $filesystemProvider; |
|
28
|
|
|
private ?string $adapter = null; |
|
29
|
|
|
|
|
30
|
7 |
|
public function __construct(FilesystemProvider $filesystemProvider) |
|
31
|
|
|
{ |
|
32
|
7 |
|
$this->filesystemProvider = $filesystemProvider; |
|
33
|
7 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function setAdapter(?string $adapter): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->adapter = $adapter; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function find($path) |
|
44
|
|
|
{ |
|
45
|
|
|
$filesystem = $this->filesystemProvider->getFilesystem($this->adapter); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
// This should be finding the file that we have uploaded into a location already - source file locator |
|
48
|
|
|
if (false === $filesystem->fileExists($path)) { |
|
49
|
|
|
throw new NotLoadableException(sprintf('Source image "%s" not found.', $path)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$mimeType = $filesystem->mimeType($path); |
|
53
|
|
|
|
|
54
|
|
|
$extension = MimeTypes::getDefault()->getExtensions($mimeType)[0]; |
|
55
|
|
|
|
|
56
|
|
|
return new Binary($filesystem->read($path), $mimeType, $extension); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|