1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusCmsPlugin\Uploader; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaInterface; |
14
|
|
|
use Gaufrette\Filesystem; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
final class MediaUploader implements MediaUploaderInterface |
18
|
|
|
{ |
19
|
|
|
/** @var Filesystem */ |
20
|
|
|
private $filesystem; |
21
|
|
|
|
22
|
|
|
public function __construct(Filesystem $filesystem) |
23
|
|
|
{ |
24
|
|
|
$this->filesystem = $filesystem; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function upload(MediaInterface $media, string $pathPrefix): void |
28
|
|
|
{ |
29
|
|
|
if (!$media->hasFile()) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$file = $media->getFile(); |
34
|
|
|
Assert::notNull($file, sprintf('File for media identified by id: "%s" is null', $media->getId())); |
35
|
|
|
if (null !== $media->getPath() && $this->has($media->getPath())) { |
36
|
|
|
$this->remove($media->getPath()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
do { |
40
|
|
|
$hash = bin2hex(random_bytes(16)); |
41
|
|
|
$path = $this->expandPath($hash . '.' . $file->guessExtension(), $pathPrefix); |
42
|
|
|
} while ($this->filesystem->has($path)); |
43
|
|
|
|
44
|
|
|
$media->setPath('/' . $path); |
45
|
|
|
$media->setMimeType($file->getMimeType()); |
46
|
|
|
$file = $media->getFile(); |
47
|
|
|
Assert::notNull($file, sprintf('File for media identified by id: "%s" is null', $media->getId())); |
48
|
|
|
$mimeType = $media->getMimeType(); |
49
|
|
|
if (null !== $mimeType && false !== strpos($mimeType, 'image')) { |
50
|
|
|
[$width, $height] = getimagesize($file->getPathname()); |
51
|
|
|
$media->setWidth($width); |
52
|
|
|
$media->setHeight($height); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$mediaPath = $media->getPath(); |
56
|
|
|
$fileContents = file_get_contents($file->getPathname()); |
57
|
|
|
Assert::notNull($mediaPath, sprintf('Media path for media identified by id: "%s" is null', $media->getId())); |
58
|
|
|
Assert::notFalse($fileContents, sprintf('File contents for file identified by id: "%s" is false', $file->getPath())); |
59
|
|
|
$this->filesystem->write( |
60
|
|
|
$mediaPath, |
61
|
|
|
$fileContents |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function remove(string $path): bool |
66
|
|
|
{ |
67
|
|
|
if ($this->filesystem->has($path)) { |
68
|
|
|
return $this->filesystem->delete($path); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function expandPath(string $path, string $pathPrefix): string |
75
|
|
|
{ |
76
|
|
|
return sprintf( |
77
|
|
|
'%s/%s/%s/%s', |
78
|
|
|
$pathPrefix, |
79
|
|
|
substr($path, 0, 2), |
80
|
|
|
substr($path, 2, 2), |
81
|
|
|
substr($path, 4) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function has(string $path): bool |
86
|
|
|
{ |
87
|
|
|
return $this->filesystem->has($path); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|