|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusVendorPlugin\Uploader; |
|
6
|
|
|
|
|
7
|
|
|
use Gaufrette\FilesystemInterface; |
|
8
|
|
|
use Odiseo\SyliusVendorPlugin\Entity\VendorInterface; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
10
|
|
|
use Webmozart\Assert\Assert; |
|
11
|
|
|
|
|
12
|
|
|
final class VendorLogoUploader implements VendorLogoUploaderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var FilesystemInterface */ |
|
15
|
|
|
private $filesystem; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct( |
|
18
|
|
|
FilesystemInterface $filesystem |
|
19
|
|
|
) { |
|
20
|
|
|
$this->filesystem = $filesystem; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function upload(VendorInterface $vendor): void |
|
27
|
|
|
{ |
|
28
|
|
|
if ($vendor->getLogoFile() === null) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** @var File $file */ |
|
33
|
|
|
$file = $vendor->getLogoFile(); |
|
34
|
|
|
|
|
35
|
|
|
if (null !== $vendor->getLogoName() && $this->has($vendor->getLogoName())) { |
|
36
|
|
|
$this->remove($vendor->getLogoName()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
do { |
|
40
|
|
|
$path = $this->name($file); |
|
41
|
|
|
} while ($this->isAdBlockingProne($path) || $this->filesystem->has($path)); |
|
42
|
|
|
|
|
43
|
|
|
$vendor->setLogoName($path); |
|
44
|
|
|
|
|
45
|
|
|
if ($vendor->getLogoName() === null) { |
|
|
|
|
|
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (file_get_contents($file->getPathname()) === false) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->filesystem->write( |
|
54
|
|
|
$vendor->getLogoName(), |
|
55
|
|
|
file_get_contents($file->getPathname()), |
|
56
|
|
|
true |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function remove(string $path): bool |
|
64
|
|
|
{ |
|
65
|
|
|
if ($this->filesystem->has($path)) { |
|
66
|
|
|
return $this->filesystem->delete($path); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
private function has(string $path): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->filesystem->has($path); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param File $file |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
private function name(File $file): string |
|
86
|
|
|
{ |
|
87
|
|
|
$name = \str_replace('.', '', \uniqid('', true)); |
|
88
|
|
|
$extension = $file->guessExtension(); |
|
89
|
|
|
|
|
90
|
|
|
if (\is_string($extension) && '' !== $extension) { |
|
91
|
|
|
$name = \sprintf('%s.%s', $name, $extension); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $name; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $path |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
private function isAdBlockingProne(string $path): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return strpos($path, 'ad') !== false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|