1 | <?php |
||
8 | class ImageUploader implements ImageUploaderInterface |
||
9 | { |
||
10 | protected $filesystem; |
||
11 | |||
12 | public function __construct(Filesystem $filesystem) |
||
16 | |||
17 | /** |
||
18 | * @param ImageInterface $image |
||
19 | */ |
||
20 | public function upload(ImageInterface $image) |
||
21 | { |
||
22 | if (!$image->hasFile()) { |
||
23 | return; |
||
24 | } |
||
25 | |||
26 | // remove older path |
||
27 | if (null !== $image->getPath() && $this->filesystem->has($image->getPath())) { |
||
28 | $this->remove($image->getPath()); |
||
29 | } |
||
30 | |||
31 | do { |
||
32 | $hash = md5(uniqid(mt_rand(), true)); |
||
33 | $path = $this->expandPath($hash.'.'.$image->getFile()->getExtension()); |
||
34 | } while ($this->filesystem->has($path)); |
||
35 | |||
36 | $image->setPath($path); |
||
37 | |||
38 | $this->filesystem->write( |
||
39 | $image->getPath(), |
||
40 | file_get_contents($image->getFile()->getPathname()) |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param $path |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function remove($path) |
||
53 | |||
54 | /** |
||
55 | * @param $path |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | private function expandPath($path) |
||
68 | } |
||
69 |